From 754ab7ec867d542f7794b1becd5c7b7f2250f367 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 10 Nov 2020 15:52:46 +0100 Subject: [PATCH] Implementing code to remove the resource from no more available contexts at startup --- .../resourceregistry/ContextUtility.java | 15 +++++++++++++++ .../resourceregistry/EServiceHandler.java | 2 +- .../resourceregistry/HostingNodeHandler.java | 2 +- .../resourcemanager/EServiceManager.java | 17 ++++++++++++++++- .../resourcemanager/HostingNodeManager.java | 5 +++++ 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/gcube/smartgears/handler/resourceregistry/ContextUtility.java b/src/main/java/org/gcube/smartgears/handler/resourceregistry/ContextUtility.java index 9279a67..326e1e8 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/ContextUtility.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/ContextUtility.java @@ -1,5 +1,9 @@ package org.gcube.smartgears.handler.resourceregistry; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + import org.gcube.common.authorization.client.proxy.AuthorizationProxy; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.scope.api.ScopeProvider; @@ -46,5 +50,16 @@ public class ContextUtility { return null; } } + + public static SortedSet getContextFullNamesFromTokens(Set tokens){ + SortedSet contextFullNames = new TreeSet<>(); + for(String token : tokens) { + String contextFullName = getContextName(token); + contextFullNames.add(contextFullName); + } + return contextFullNames; + } + + } diff --git a/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceHandler.java b/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceHandler.java index 6a5f8d7..d73b36e 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceHandler.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceHandler.java @@ -80,12 +80,12 @@ public class EServiceHandler extends ApplicationLifecycleHandler { try { Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); boolean create = true; + eServiceManager = new EServiceManager(applicationContext); Set startTokens = applicationContext.configuration().startTokens(); for (String token : startTokens) { ContextUtility.setContextFromToken(token); try { if (create) { - eServiceManager = new EServiceManager(applicationContext); eServiceManager.createEService(); applicationContext.properties() .add(new Property(Constants.ESERVICE_MANAGER_PROPERTY, eServiceManager)); diff --git a/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeHandler.java b/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeHandler.java index 833f33a..acba236 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeHandler.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeHandler.java @@ -86,12 +86,12 @@ public class HostingNodeHandler extends ContainerHandler { try { Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); boolean create = true; + hostingNodeManager = new HostingNodeManager(containerContext); List startTokens = containerContext.configuration().startTokens(); for (String token : startTokens) { ContextUtility.setContextFromToken(token); try { if (create) { - hostingNodeManager = new HostingNodeManager(containerContext); hostingNodeManager.createHostingNode(); containerContext.properties() .add(new Property(Constants.HOSTING_NODE_MANAGER_PROPERTY, hostingNodeManager)); diff --git a/src/main/java/org/gcube/smartgears/handler/resourceregistry/resourcemanager/EServiceManager.java b/src/main/java/org/gcube/smartgears/handler/resourceregistry/resourcemanager/EServiceManager.java index 261e63d..b2c3e08 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/resourcemanager/EServiceManager.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/resourcemanager/EServiceManager.java @@ -3,6 +3,8 @@ package org.gcube.smartgears.handler.resourceregistry.resourcemanager; import java.net.URI; import java.util.Arrays; import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.UUID; import javax.servlet.ServletRegistration; @@ -13,6 +15,7 @@ import org.gcube.informationsystem.model.reference.properties.Header; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint; +import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; @@ -178,8 +181,9 @@ public class EServiceManager { public EService createEService() throws ResourceRegistryException { ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); UUID eServiceUUID = UUID.fromString(applicationContext.id()); + Set contexts; try { - resourceRegistryClient.exists(EService.class, eServiceUUID); + ResourceRegistryClientFactory.includeContextsInInstanceHeader(true); eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); serviceStateFacet = eService.getFacets(ServiceStateFacet.class).get(0); if(serviceStateFacet==null) { @@ -194,6 +198,7 @@ public class EServiceManager { } catch (NotFoundException e) { eService = instantiateEService(); createActivatesRelation(eService); + } catch (AvailableInAnotherContextException e) { addToContext(); try { @@ -219,6 +224,16 @@ public class EServiceManager { } catch (ResourceRegistryException e) { throw e; } + + // TODO Remove contexts not present in start contexts + Set startTokens = applicationContext.configuration().startTokens(); + Set contextFullNames = ContextUtility.getContextFullNamesFromTokens(startTokens); + ContextCache contextCache = ContextCache.getInstance(); + + + + Map contextFullNameToUUID = contextCache.getContextFullNameToUUIDAssociation(); + return eService; } diff --git a/src/main/java/org/gcube/smartgears/handler/resourceregistry/resourcemanager/HostingNodeManager.java b/src/main/java/org/gcube/smartgears/handler/resourceregistry/resourcemanager/HostingNodeManager.java index 7966347..44dce0c 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/resourcemanager/HostingNodeManager.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/resourcemanager/HostingNodeManager.java @@ -15,6 +15,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -334,6 +335,10 @@ public class HostingNodeManager { addToContext(); hostingNode = resourceRegistryClient.getInstance(HostingNode.class, uuid); } + + // TODO Remove contexts not present in start contexts + List startTokens = containerContext.configuration().startTokens(); + return hostingNode; }