Changed handler behaviour

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/resource-registry-handlers@146554 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-04-04 14:18:14 +00:00
parent 853ae8a37d
commit 5f53e49248
2 changed files with 140 additions and 67 deletions

View File

@ -53,6 +53,7 @@ import org.gcube.informationsystem.model.relation.IsRelatedTo;
import org.gcube.informationsystem.model.relation.isrelatedto.Hosts;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
import org.gcube.informationsystem.resourceregistry.client.Direction;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
@ -113,7 +114,7 @@ public class EServiceManager extends ApplicationLifecycleHandler {
ScopeProvider.instance.reset();
} else {
SecurityTokenProvider.instance.set(token);
String scope = getCurrentContextName(token);
String scope = getContextName(token);
ScopeProvider.instance.set(scope);
}
@ -150,14 +151,17 @@ public class EServiceManager extends ApplicationLifecycleHandler {
@Observes(value = addToContext)
void addTo(String token) {
EService eService = applicationContext.properties().lookup(Constants.ESERVICE_PROPERTY).value(EService.class);
addToContext(eService, token);
//EService eService = applicationContext.properties().lookup(Constants.ESERVICE_PROPERTY).value(EService.class);
//addToContext(eService, token);
addHostingNodeToContext(token);
}
@Observes(value = removeFromContext)
void removeFrom(String token) {
EService eService = applicationContext.properties().lookup(Constants.ESERVICE_PROPERTY).value(EService.class);
removeFromContext(eService, token);
// EService eService = applicationContext.properties().lookup(Constants.ESERVICE_PROPERTY).value(EService.class);
// removeFromContext(eService, token);
removeHostingNodeFromContext(token);
}
});
}
@ -237,10 +241,10 @@ public class EServiceManager extends ApplicationLifecycleHandler {
}
@SuppressWarnings("unchecked")
private void createHostsRelation(EService eService,
ResourceRegistryPublisher resourceRegistryPublisher) {
HostingNode hostingNode = applicationContext.container().properties().lookup(Constants.HOSTING_NODE_PROPERTY).value(HostingNode.class);
if (hostingNode != null) {
@ -267,11 +271,10 @@ public class EServiceManager extends ApplicationLifecycleHandler {
}
} catch (Exception e) {
logger.error("Error while trying to retrieve Hosts relation", e);
logger.warn("Error while trying to retrieve Hosts relation", e);
}
if (hosts == null) {
PropagationConstraint propagationConstraint = new PropagationConstraintImpl();
propagationConstraint
.setRemoveConstraint(RemoveConstraint.cascade);
@ -296,27 +299,36 @@ public class EServiceManager extends ApplicationLifecycleHandler {
private EService createEServiceAndHosts(
ResourceRegistryPublisher resourceRegistryPublisher,
EService eService) throws ResourceRegistryException {
try {
eService = resourceRegistryPublisher.createResource(EService.class,
eService);
} catch (ResourceAlreadyPresentException e) {
ResourceRegistryClient registryClient = ResourceRegistryClientFactory
.create();
eService = registryClient.getInstance(EService.class, eService
.getHeader().getUUID());
}
try {
createHostsRelation(eService, resourceRegistryPublisher);
} catch (Exception ex) {
logger.error(
"Unable to Create {} relation from {} to {} ({} : {})",
String error = String.format(
"Unable to Create %s relation from % to % (%s : %s)",
Hosts.NAME, HostingNode.NAME, EService.NAME,
applicationContext.name(), eService.getHeader().getUUID(),
ex);
applicationContext.name(), eService.getHeader().getUUID());
logger.error(error, ex);
throw new ResourceRegistryException(error, ex);
}
} catch (ResourceAlreadyPresentException e) {
ResourceRegistryClient registryClient = ResourceRegistryClientFactory.create();
eService = registryClient.getInstance(EService.class, eService.getHeader().getUUID());
} catch (ResourceAvailableInAnotherContextException e) {
// Adding the HostingNode I also add the EService thanks to propagationConstraint
addHostingNodeToCurrentContext();
ResourceRegistryClient registryClient = ResourceRegistryClientFactory.create();
eService = registryClient.getInstance(EService.class, eService.getHeader().getUUID());
}
return eService;
}
return eService;
private void shareHostingNode(HostingNode hostingNode) {
logger.trace("sharing {} {}", HostingNode.NAME, Resource.NAME);
applicationContext.container().properties().add(
new Property(Constants.HOSTING_NODE_PROPERTY, hostingNode));
}
private EService publishEservice(EService eService) {
@ -328,8 +340,7 @@ public class EServiceManager extends ApplicationLifecycleHandler {
boolean create = true;
Set<String> startTokens = applicationContext.configuration()
.startTokens();
Set<String> startTokens = applicationContext.configuration().startTokens();
for (String token : startTokens) {
setContextFromToken(token);
@ -341,29 +352,17 @@ public class EServiceManager extends ApplicationLifecycleHandler {
eService = createEServiceAndHosts(
resourceRegistryPublisher, eService);
share(eService);
create = false;
} else {
boolean added = resourceRegistryPublisher
.addResourceToContext(eService);
if (added) {
logger.info(
"{} successfully added to current context ({})",
eService, getCurrentContextName(token));
share(eService);
} else {
logger.error(
"Unable to add {} to current context ({})",
eService, getCurrentContextName(token));
addHostingNodeToContext(token);
}
}
share(eService);
} catch (ResourceRegistryException e) {
logger.error("Unable to add {} to current context ({})",
eService, getCurrentContextName(token), e);
eService, getContextName(token), e);
}
}
@ -379,7 +378,12 @@ public class EServiceManager extends ApplicationLifecycleHandler {
}
private String getCurrentContextName(String token) {
private String getCurrentContextName() {
String token = SecurityTokenProvider.instance.get();
return getContextName(token);
}
private String getContextName(String token) {
try {
return this.authorizationProxy.get(token).getContext();
} catch (Exception e) {
@ -389,6 +393,82 @@ public class EServiceManager extends ApplicationLifecycleHandler {
}
}
private boolean addHostingNodeToCurrentContext(){
String token = SecurityTokenProvider.instance.get();
return addHostingNodeToContext(token);
}
private boolean addHostingNodeToContext(String token){
HostingNode hostingNode = applicationContext.container().properties().lookup(Constants.HOSTING_NODE_PROPERTY).value(HostingNode.class);
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get();
try {
Thread.currentThread().setContextClassLoader(
EServiceManager.class.getClassLoader());
setContextFromToken(token);
ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
resourceRegistryPublisher.addResourceToContext(hostingNode);
logger.info("{} successfully added to current context ({})",
hostingNode, getContextName(token));
shareHostingNode(hostingNode);
return true;
} catch (Exception e) {
logger.error("Unable to add {} to current context ({})", hostingNode,
getCurrentContextName(), e);
rethrowUnchecked(e);
return false;
} finally {
setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
}
/*
private boolean removeHostingNodeFromContext() {
String token = SecurityTokenProvider.instance.get();
return removeHostingNodeFromContext(token);
}
*/
private boolean removeHostingNodeFromContext(String token) {
HostingNode hostingNode = applicationContext.container().properties().lookup(Constants.HOSTING_NODE_PROPERTY).value(HostingNode.class);
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get();
try {
Thread.currentThread().setContextClassLoader(
EServiceManager.class.getClassLoader());
setContextFromToken(token);
ResourceRegistryPublisher resourceRegistryPublisher =
ResourceRegistryPublisherFactory.create();
resourceRegistryPublisher
.removeResourceFromContext(hostingNode);
logger.info("{} successfully removed from current context ({})",
hostingNode, getContextName(token));
shareHostingNode(hostingNode);
return true;
} catch (Exception e) {
logger.error("Unable to remove {} from current context ({})",
hostingNode, getContextName(token), e);
rethrowUnchecked(e);
return false;
} finally {
setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
}
/*
private void removeFromContext(EService eService, String token) {
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
String previousToken = SecurityTokenProvider.instance.get();
@ -407,18 +487,18 @@ public class EServiceManager extends ApplicationLifecycleHandler {
if (removed) {
logger.info(
"{} successfully removed from current context ({})",
eService, getCurrentContextName(token));
eService, getContextName(token));
share(eService);
} else {
logger.error("Unable to remove {} from current context ({})",
eService, getCurrentContextName(token));
eService, getContextName(token));
}
share(eService);
} catch (Exception e) {
logger.error("Unable to remove {} from current context ({})",
eService, getCurrentContextName(token), e);
eService, getContextName(token), e);
rethrowUnchecked(e);
} finally {
setContextFromToken(previousToken);
@ -441,24 +521,25 @@ public class EServiceManager extends ApplicationLifecycleHandler {
if (added) {
logger.info("{} successfully added to current context ({})",
eService, getCurrentContextName(token));
eService, getContextName(token));
share(eService);
} else {
logger.error("Unable to add {} to current context ({})",
eService, getCurrentContextName(token));
eService, getContextName(token));
}
share(eService);
} catch (Exception e) {
logger.error("Unable to add {} to current context ({})", eService,
getCurrentContextName(token), e);
getContextName(token), e);
rethrowUnchecked(e);
} finally {
setContextFromToken(previousToken);
Thread.currentThread().setContextClassLoader(contextCL);
}
}
*/
private void createOrUpdateServiceStateFacet(EService eService, String state) {

View File

@ -68,6 +68,7 @@ import org.gcube.informationsystem.model.relation.consistsof.HasPersistentMemory
import org.gcube.informationsystem.model.relation.consistsof.HasVolatileMemory;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
@ -271,6 +272,7 @@ public class HostingNodeManager extends ContainerHandler {
hostingNode = resourceRegistryPublisher
.createResource(HostingNode.class,
hostingNode);
share(hostingNode);
// TODO Add a Reference to Site
/*
@ -288,25 +290,15 @@ public class HostingNodeManager extends ContainerHandler {
hostingNode = registryClient.getInstance(
HostingNode.class, hostingNode.getHeader()
.getUUID());
} catch (ResourceAvailableInAnotherContextException e) {
addToContext(hostingNode, token);
}
create = false;
} else {
boolean added = resourceRegistryPublisher
.addResourceToContext(hostingNode);
if (added) {
logger.info(
"{} successfully added to current context ({})",
hostingNode, getCurrentContextName(token));
share(hostingNode);
} else {
logger.error(
"Unable to add {} to current context ({})",
hostingNode, getCurrentContextName(token));
addToContext(hostingNode, token);
}
}
share(hostingNode);
} catch (Exception e) {
logger.error("Unable to add {} to current context ({})",