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

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.model.relation.consistsof.HasVolatileMemory;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; 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.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.api.exceptions.entity.resource.ResourceNotFoundException;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient; import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory; import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
@ -271,7 +272,8 @@ public class HostingNodeManager extends ContainerHandler {
hostingNode = resourceRegistryPublisher hostingNode = resourceRegistryPublisher
.createResource(HostingNode.class, .createResource(HostingNode.class,
hostingNode); hostingNode);
share(hostingNode);
// TODO Add a Reference to Site // TODO Add a Reference to Site
/* /*
* node.profile().newSite().country(cfg.site().country * node.profile().newSite().country(cfg.site().country
@ -288,26 +290,16 @@ public class HostingNodeManager extends ContainerHandler {
hostingNode = registryClient.getInstance( hostingNode = registryClient.getInstance(
HostingNode.class, hostingNode.getHeader() HostingNode.class, hostingNode.getHeader()
.getUUID()); .getUUID());
} catch (ResourceAvailableInAnotherContextException e) {
addToContext(hostingNode, token);
} }
create = false; create = false;
} else { } else {
boolean added = resourceRegistryPublisher addToContext(hostingNode, token);
.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));
}
} }
share(hostingNode);
} catch (Exception e) { } catch (Exception e) {
logger.error("Unable to add {} to current context ({})", logger.error("Unable to add {} to current context ({})",
hostingNode, getCurrentContextName(token), e); hostingNode, getCurrentContextName(token), e);