From 5f53e4924849dfbd886b82ecc0657ec2f8754821 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 4 Apr 2017 14:18:14 +0000 Subject: [PATCH] Changed handler behaviour git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/resource-registry-handlers@146554 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../resourceregistry/EServiceManager.java | 185 +++++++++++++----- .../resourceregistry/HostingNodeManager.java | 22 +-- 2 files changed, 140 insertions(+), 67 deletions(-) diff --git a/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceManager.java b/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceManager.java index e631a6b..0d0ae8f 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceManager.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceManager.java @@ -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) { @@ -266,12 +270,11 @@ public class EServiceManager extends ApplicationLifecycleHandler { } } - }catch (Exception e) { - logger.error("Error while trying to retrieve Hosts relation", e); + } catch (Exception e) { + logger.warn("Error while trying to retrieve Hosts relation", e); } if (hosts == null) { - PropagationConstraint propagationConstraint = new PropagationConstraintImpl(); propagationConstraint .setRemoveConstraint(RemoveConstraint.cascade); @@ -296,29 +299,38 @@ public class EServiceManager extends ApplicationLifecycleHandler { private EService createEServiceAndHosts( ResourceRegistryPublisher resourceRegistryPublisher, EService eService) throws ResourceRegistryException { + try { eService = resourceRegistryPublisher.createResource(EService.class, 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) { - ResourceRegistryClient registryClient = ResourceRegistryClientFactory - .create(); - eService = registryClient.getInstance(EService.class, eService - .getHeader().getUUID()); + 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()); } - - 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; } - + + 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) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); String previousToken = SecurityTokenProvider.instance.get(); @@ -328,8 +340,7 @@ public class EServiceManager extends ApplicationLifecycleHandler { boolean create = true; - Set startTokens = applicationContext.configuration() - .startTokens(); + Set 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); } } @@ -378,8 +377,13 @@ public class EServiceManager extends ApplicationLifecycleHandler { return eService; } - - 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) { @@ -388,7 +392,83 @@ public class EServiceManager extends ApplicationLifecycleHandler { 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) { 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,25 +521,26 @@ 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) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); diff --git a/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeManager.java b/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeManager.java index 5ff9aa4..6509838 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeManager.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeManager.java @@ -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,7 +272,8 @@ public class HostingNodeManager extends ContainerHandler { hostingNode = resourceRegistryPublisher .createResource(HostingNode.class, hostingNode); - + share(hostingNode); + // TODO Add a Reference to Site /* * node.profile().newSite().country(cfg.site().country @@ -288,26 +290,16 @@ 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 ({})", hostingNode, getCurrentContextName(token), e);