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 5c78a81..d59c79f 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceManager.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceManager.java @@ -335,16 +335,6 @@ public class EServiceManager extends ApplicationLifecycleHandler { 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()); @@ -352,11 +342,27 @@ public class EServiceManager extends ApplicationLifecycleHandler { logger.debug("Moving app {} to {}", applicationContext.name(), state); createOrUpdateServiceStateFacet(eService, state); } catch (ResourceAvailableInAnotherContextException e) { - // Adding the HostingNode I also add the EService thanks to propagationConstraint + // Adding the HostingNode I also add the EService thanks to propagationConstraint + String token = SecurityTokenProvider.instance.get(); + // If hosts was not created we need to add eService and HostingNode, otherwise + // addHostingnode is sufficient. Because we cannot be sure of it we invoke both + addToContext(eService, token); addHostingNodeToCurrentContext(); ResourceRegistryClient registryClient = ResourceRegistryClientFactory.create(); eService = registryClient.getInstance(EService.class, eService.getHeader().getUUID()); } + + 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); + } + return eService; } @@ -540,6 +546,7 @@ public class EServiceManager extends ApplicationLifecycleHandler { Thread.currentThread().setContextClassLoader(contextCL); } } + */ private void addToContext(EService eService, String token) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); @@ -574,7 +581,6 @@ public class EServiceManager extends ApplicationLifecycleHandler { Thread.currentThread().setContextClassLoader(contextCL); } } - */ @SuppressWarnings("unchecked")