From 615dcd90c875f0cfe5d6432ec48810e6650cf366 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 9 Jun 2022 16:07:22 +0200 Subject: [PATCH] Fixing handlers --- .../resourceregistry/EServiceHandler.java | 11 +-- .../resourceregistry/HostingNodeHandler.java | 18 ++-- .../resourcemanager/EServiceManager.java | 83 ++++++++++++++----- .../resourcemanager/HostingNodeManager.java | 36 ++++++-- 4 files changed, 111 insertions(+), 37 deletions(-) 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 f2a99df..c353a7b 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceHandler.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceHandler.java @@ -105,6 +105,7 @@ public class EServiceHandler extends ApplicationLifecycleHandler { Set startContextsUUID = new HashSet<>(); for (String token : startTokens) { UUID contextUUID = ContextUtility.getContextUUID(token); + startContextsUUID.add(contextUUID); if (create) { ContextUtility.setContextFromToken(token); eServiceManager = new EServiceManager(applicationContext); @@ -120,7 +121,6 @@ public class EServiceHandler extends ApplicationLifecycleHandler { throw e; } } - startContextsUUID.add(contextUUID); } Set resourceContextsUUID = eServiceManager.getContextsUUID().keySet(); @@ -162,13 +162,14 @@ public class EServiceHandler extends ApplicationLifecycleHandler { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); String previousToken = SecurityTokenProvider.instance.get(); if (previousToken == null) { - ContextUtility - .setContextFromToken(applicationContext.configuration().startTokens().iterator().next()); + previousToken = applicationContext.configuration().startTokens().iterator().next(); +// ContextUtility.setContextFromToken(previousToken); } try { Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); ContextUtility.setContextFromToken(token); - eServiceManager.addToCurrentContext(); + UUID contextUUID = ContextUtility.getContextUUID(token); + eServiceManager.addToContext(contextUUID); } catch (Exception e) { logger.error("Failed to add {} to current context ({})", EService.NAME, ContextUtility.getCurrentContextName(), e); @@ -188,7 +189,7 @@ public class EServiceHandler extends ApplicationLifecycleHandler { } try { Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); - eServiceManager.removeFromContext(); + eServiceManager.removeFromCurrentContext(); } catch (Exception e) { logger.error("Failed to remove {} from current context ({})", EService.NAME, ContextUtility.getCurrentContextName(), e); 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 23acdfb..433004f 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeHandler.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeHandler.java @@ -105,6 +105,7 @@ public class HostingNodeHandler extends ContainerHandler { Set startContextsUUID = new HashSet<>(); for (String token : startTokens) { UUID contextUUID = ContextUtility.getContextUUID(token); + startContextsUUID.add(contextUUID); if (create) { ContextUtility.setContextFromToken(token); hostingNodeManager = new HostingNodeManager(containerContext); @@ -120,7 +121,6 @@ public class HostingNodeHandler extends ContainerHandler { throw e; } } - startContextsUUID.add(contextUUID); } Set resourceContextsUUID = hostingNodeManager.getContextsUUID().keySet(); @@ -171,12 +171,14 @@ public class HostingNodeHandler extends ContainerHandler { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); String previousToken = SecurityTokenProvider.instance.get(); if (previousToken == null) { - ContextUtility - .setContextFromToken(containerContext.configuration().startTokens().iterator().next()); + previousToken = containerContext.configuration().startTokens().iterator().next(); +// ContextUtility.setContextFromToken(previousToken); } try { Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); - hostingNodeManager.addToCurrentContext(); + ContextUtility.setContextFromToken(token); + UUID contextUUID = ContextUtility.getContextUUID(token); + hostingNodeManager.addToContext(contextUUID); } catch (Exception e) { logger.error("Failed to update Service State", e); } finally { @@ -190,12 +192,14 @@ public class HostingNodeHandler extends ContainerHandler { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); String previousToken = SecurityTokenProvider.instance.get(); if (previousToken == null) { - ContextUtility - .setContextFromToken(containerContext.configuration().startTokens().iterator().next()); + previousToken = containerContext.configuration().startTokens().iterator().next(); +// ContextUtility.setContextFromToken(previousToken); } try { Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); - hostingNodeManager.removeFromContext(); + ContextUtility.setContextFromToken(token); + UUID contextUUID = ContextUtility.getContextUUID(token); + hostingNodeManager.removeFromContext(contextUUID); } catch (Exception e) { logger.error("Failed to update Service State", e); } finally { 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 607dc19..11188fe 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 @@ -4,12 +4,15 @@ import java.net.URI; import java.util.Arrays; import java.util.Calendar; import java.util.Date; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.UUID; import javax.servlet.ServletRegistration; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.informationsystem.model.impl.properties.HeaderImpl; import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; import org.gcube.informationsystem.model.reference.properties.Header; @@ -78,22 +81,64 @@ public class EServiceManager { } public void addEServiceToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { + String currentToken = SecurityTokenProvider.instance.get(); + UUID contextUUID = ContextUtility.getContextUUID(currentToken); + + /* Trying to get a context which is not the current to properly invoke the addToContext without forcing the operation using addResourceToCurrentContext */ + boolean anotherContextSet = false; + Set startTokens = applicationContext.configuration().startTokens(); + for (String token : startTokens) { + UUID anotherContextUUID = ContextUtility.getContextUUID(token); + if(anotherContextUUID.compareTo(contextUUID)!=0) { + ContextUtility.setContextFromToken(token); + anotherContextSet = true; + break; + } + } + UUID uuid = UUID.fromString(applicationContext.id()); try { - resourceRegistryPublisher.addResourceToCurrentContext(EService.NAME, uuid, false); + if(anotherContextSet) { + resourceRegistryPublisher.addResourceToContext(EService.NAME, uuid, contextUUID, false); + }else { + resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false); + } logger.info("{} with UUID {} successfully added to current context ({})", EService.NAME, uuid, ContextUtility.getCurrentContextName()); } catch (Exception e) { logger.error("Unable to add {} with UUID {} to current context ({})", EService.NAME, uuid, ContextUtility.getCurrentContextName(), e); + }finally { + ContextUtility.setContextFromToken(currentToken); } } - public void addToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { + public void addHostingNodeToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { + String currentToken = SecurityTokenProvider.instance.get(); + UUID contextUUID = ContextUtility.getContextUUID(currentToken); + + /* Trying to get a context which is not the current to properly invoke the addToContext without forcing the operation using addResourceToCurrentContext */ + boolean anotherContextSet = false; + Set startTokens = applicationContext.configuration().startTokens(); + for (String token : startTokens) { + UUID anotherContextUUID = ContextUtility.getContextUUID(token); + if(anotherContextUUID.compareTo(contextUUID)!=0) { + ContextUtility.setContextFromToken(token); + anotherContextSet = true; + break; + } + } + UUID uuid = UUID.fromString(applicationContext.container().id()); try { - resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false); + if(anotherContextSet) { + resourceRegistryPublisher.addResourceToContext(HostingNode.NAME, uuid, contextUUID, false); + }else { + resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false); + } logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName()); } catch (Exception e) { logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e); + }finally { + ContextUtility.setContextFromToken(currentToken); } } @@ -107,7 +152,7 @@ public class EServiceManager { } } - public void removeFromContext() + public void removeFromCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { UUID uuid = UUID.fromString(applicationContext.container().id()); try { @@ -226,20 +271,20 @@ public class EServiceManager { createActivatesRelation(eService); stateFacet = eService.getFacets(StateFacet.class).get(0); } catch (AvailableInAnotherContextException e) { -// addToContext(); -// try { -// eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); -// } catch (AvailableInAnotherContextException ex) { -// addEServiceToCurrentContext(); -// eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); -// // addToContext() is executed on HostingNode. -// // If the EService is still not available we need to create activates -// // relation because does not exists otherwise the EService should -// // already be in the context due to propagation constraint. -// createActivatesRelation(eService); -// } -// stateFacet = eService.getFacets(StateFacet.class).get(0); -// updateServiceStateFacet(); + addHostingNodeToCurrentContext(); + try { + eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); + } catch (AvailableInAnotherContextException ex) { + addEServiceToCurrentContext(); + eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); + // addToContext() is executed on HostingNode. + // If the EService is still not available we need to create activates + // relation because does not exists otherwise the EService should + // already be in the context due to propagation constraint. + createActivatesRelation(eService); + } + stateFacet = eService.getFacets(StateFacet.class).get(0); + updateServiceStateFacet(); } catch (ResourceRegistryException e) { throw e; } @@ -289,7 +334,7 @@ public class EServiceManager { activates = resourceRegistryPublisher.createIsRelatedTo(activates); this.eService = activates.getTarget(); } catch (NotFoundException e) { - logger.error("THIS IS REALLY STRANGE. YOU SHOULD NE BE HERE. Error while creating {}.", activates, e); + logger.error("THIS IS REALLY STRANGE. YOU SHOULD NOT BE HERE. Error while creating {}.", activates, e); throw e; } catch (ResourceRegistryException e) { logger.error("Error while creating {}", activates, e); 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 890004d..466e6d9 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 @@ -32,6 +32,7 @@ import javax.management.ObjectName; import javax.management.ReflectionException; import org.gcube.com.fasterxml.jackson.databind.JsonNode; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.informationsystem.model.impl.properties.HeaderImpl; import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Resource; @@ -111,9 +112,28 @@ public class HostingNodeManager { } public void addToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { + String currentToken = SecurityTokenProvider.instance.get(); + UUID contextUUID = ContextUtility.getContextUUID(currentToken); + + /* Trying to get a context which is not the current to properly invoke the addToContext without forcing the operation using addResourceToCurrentContext */ + boolean anotherContextSet = false; + List startTokens = containerContext.configuration().startTokens(); + for (String token : startTokens) { + UUID anotherContextUUID = ContextUtility.getContextUUID(token); + if(anotherContextUUID.compareTo(contextUUID)!=0) { + ContextUtility.setContextFromToken(token); + anotherContextSet = true; + break; + } + } + UUID uuid = UUID.fromString(containerContext.id()); try { - resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false); + if(anotherContextSet) { + resourceRegistryPublisher.addToContext(HostingNode.NAME, uuid, contextUUID, false); + }else { + resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false); + } logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName()); } catch (Exception e) { logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e); @@ -130,7 +150,7 @@ public class HostingNodeManager { } } - public void removeFromContext() + public void removeFromCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { UUID uuid = UUID.fromString(containerContext.id()); try { @@ -210,8 +230,10 @@ public class HostingNodeManager { consistsOfList.addAll(consistsOfToRemove); hostingNode = resourceRegistryPublisher.createResource(hostingNode); } catch (AvailableInAnotherContextException e) { - addToCurrentContext(); - hostingNode = resourceRegistryPublisher.updateResource(hostingNode); +// addToCurrentContext(); +// hostingNode = resourceRegistryPublisher.updateResource(hostingNode); + logger.error("", e); + throw e; } catch (ResourceRegistryException e) { logger.error("error trying to publish hosting node", e); } @@ -274,8 +296,10 @@ public class HostingNodeManager { consistsOfList.addAll(consistsOfToRemove); hostingNode = resourceRegistryPublisher.createResource(hostingNode); } catch (AvailableInAnotherContextException e) { - addToCurrentContext(); - hostingNode = resourceRegistryPublisher.updateResource(hostingNode); +// addToCurrentContext(); +// hostingNode = resourceRegistryPublisher.updateResource(hostingNode); + logger.error("", e); + throw e; } catch (ResourceRegistryException e) { logger.error("error trying to publish hosting node", e); }