From f7e3baca07baa831b438490b8effb869146d7351 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 23 Oct 2020 10:34:38 +0200 Subject: [PATCH] Fixing handlers --- .../resourceregistry/EServiceHandler.java | 83 ++++++++++--------- .../resourceregistry/HostingNodeHandler.java | 52 +++++------- .../resourcemanager/EServiceManager.java | 51 ++++++------ .../resourcemanager/HostingNodeManager.java | 3 +- 4 files changed, 92 insertions(+), 97 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 db4b4ed..36f70a1 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceHandler.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/EServiceHandler.java @@ -1,7 +1,6 @@ package org.gcube.smartgears.handler.resourceregistry; import static org.gcube.common.events.Observes.Kind.resilient; -import static org.gcube.smartgears.handler.resourceregistry.Constants.RESOURCE_MANAGEMENT; import static org.gcube.smartgears.handlers.ProfileEvents.addToContext; import static org.gcube.smartgears.handlers.ProfileEvents.removeFromContext; import static org.gcube.smartgears.lifecycle.application.ApplicationLifecycle.activation; @@ -24,7 +23,7 @@ import org.gcube.resourcemanagement.model.reference.entities.resources.EService; import org.gcube.smartgears.context.Property; import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.smartgears.handler.resourceregistry.resourcemanager.EServiceManager; -import org.gcube.smartgears.handlers.application.ApplicationLifecycleEvent; +import org.gcube.smartgears.handlers.application.ApplicationLifecycleEvent.Start; import org.gcube.smartgears.handlers.application.ApplicationLifecycleHandler; import org.gcube.smartgears.lifecycle.application.ApplicationLifecycle; import org.gcube.smartgears.lifecycle.application.ApplicationState; @@ -47,7 +46,7 @@ import org.slf4j.LoggerFactory; * * @author Luca Frosini */ -@XmlRootElement(name = RESOURCE_MANAGEMENT) +@XmlRootElement(name = Constants.RESOURCE_MANAGEMENT) public class EServiceHandler extends ApplicationLifecycleHandler { private static final Logger logger = LoggerFactory.getLogger(EServiceHandler.class); @@ -62,14 +61,14 @@ public class EServiceHandler extends ApplicationLifecycleHandler { } @Override - public void onStart(ApplicationLifecycleEvent.Start e) { + public void onStart(Start event) { try { logger.info("{} onStart started", this.getClass().getSimpleName()); - this.applicationContext = e.context(); + this.applicationContext = event.context(); init(); registerObservers(); schedulePeriodicUpdates(); - logger.info("{} onStart finished", this.getClass().getSimpleName()); + logger.info("{} onStart terminated", this.getClass().getSimpleName()); } catch (Throwable re) { logger.error("onStart failed", re); } @@ -86,16 +85,16 @@ public class EServiceHandler extends ApplicationLifecycleHandler { ContextUtility.setContextFromToken(token); try { if (create) { - eServiceManager = new EServiceManager(); - eServiceManager.createEService(applicationContext); + eServiceManager = new EServiceManager(applicationContext); + eServiceManager.createEService(); applicationContext.properties() .add(new Property(Constants.ESERVICE_MANAGER_PROPERTY, eServiceManager)); create = false; } else { - eServiceManager.addToContext(applicationContext); + eServiceManager.addToContext(); } } catch (Exception e) { - logger.error("Unable to add {} to current context ({})", eServiceManager.geteService(), + logger.error("Unable to add {} to current context ({})", eServiceManager.getEService(), ContextUtility.getContextName(token), e); } } @@ -116,18 +115,18 @@ public class EServiceHandler extends ApplicationLifecycleHandler { @Observes({ activation }) void onChanged(ApplicationLifecycle lc) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); - String currentToken = SecurityTokenProvider.instance.get(); - if (currentToken == null) { - currentToken = applicationContext.configuration().startTokens().iterator().next(); + String previousToken = SecurityTokenProvider.instance.get(); + if (previousToken == null) { + ContextUtility + .setContextFromToken(applicationContext.configuration().startTokens().iterator().next()); } - ContextUtility.setContextFromToken(currentToken); - try { Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); - eServiceManager.updateServiceStateFacet(applicationContext); + eServiceManager.updateServiceStateFacet(); } catch (Exception e) { logger.error("Failed to update {} State", EService.NAME, e); } finally { + ContextUtility.setContextFromToken(previousToken); Thread.currentThread().setContextClassLoader(contextCL); } @@ -136,18 +135,18 @@ public class EServiceHandler extends ApplicationLifecycleHandler { @Observes({ stop, failure }) void onStop(ApplicationLifecycle lc) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); - String currentToken = SecurityTokenProvider.instance.get(); - if (currentToken == null) { - currentToken = applicationContext.configuration().startTokens().iterator().next(); + String previousToken = SecurityTokenProvider.instance.get(); + if (previousToken == null) { + ContextUtility + .setContextFromToken(applicationContext.configuration().startTokens().iterator().next()); } - ContextUtility.setContextFromToken(currentToken); - try { Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); - eServiceManager.removeEService(applicationContext); + eServiceManager.removeEService(); } catch (Exception e) { - logger.error("Failed to update Service State", e); + logger.error("Failed to update {} State", EService.NAME, e); } finally { + ContextUtility.setContextFromToken(previousToken); Thread.currentThread().setContextClassLoader(contextCL); } @@ -157,12 +156,16 @@ public class EServiceHandler extends ApplicationLifecycleHandler { void addTo(String token) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); String previousToken = SecurityTokenProvider.instance.get(); + if (previousToken == null) { + ContextUtility + .setContextFromToken(applicationContext.configuration().startTokens().iterator().next()); + } try { Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); ContextUtility.setContextFromToken(token); - eServiceManager.addToContext(applicationContext); + eServiceManager.addToContext(); } catch (Exception e) { - logger.error("Failed to add HostingNode to current context ({})", + logger.error("Failed to add {} to current context ({})", EService.NAME, ContextUtility.getCurrentContextName(), e); } finally { ContextUtility.setContextFromToken(previousToken); @@ -174,13 +177,16 @@ public class EServiceHandler extends ApplicationLifecycleHandler { void removeFrom(String token) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); String previousToken = SecurityTokenProvider.instance.get(); + if (previousToken == null) { + ContextUtility + .setContextFromToken(applicationContext.configuration().startTokens().iterator().next()); + } try { Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); - ContextUtility.setContextFromToken(token); eServiceManager.removeFromContext(); } catch (Exception e) { - logger.error("Failed to remove HostingNode from current context ({})", - ContextUtility.getCurrentContextName(), e); + logger.error("Failed to remove {} from current context ({})", + EService.NAME, ContextUtility.getCurrentContextName(), e); } finally { ContextUtility.setContextFromToken(previousToken); Thread.currentThread().setContextClassLoader(contextCL); @@ -218,16 +224,17 @@ public class EServiceHandler extends ApplicationLifecycleHandler { final Runnable updateTask = new Runnable() { public void run() { + String previousToken = SecurityTokenProvider.instance.get(); + if (previousToken == null) { + ContextUtility.setContextFromToken( + applicationContext.configuration().startTokens().iterator().next()); + } try { - String currentToken = SecurityTokenProvider.instance.get(); - if (currentToken == null) - currentToken = applicationContext.configuration().startTokens().iterator() - .next(); - - ContextUtility.setContextFromToken(currentToken); - eServiceManager.updateServiceStateFacet(applicationContext); + eServiceManager.updateServiceStateFacet(); } catch (Exception e) { logger.error("Cannot complete periodic update of EService", e); + } finally { + ContextUtility.setContextFromToken(previousToken); } } }; @@ -242,14 +249,14 @@ public class EServiceHandler extends ApplicationLifecycleHandler { synchronized void cancelPeriodicUpdates(ContainerLifecycle ignore) { if (periodicUpdates != null) { - logger.trace("stopping periodic updates of application {} EService", + logger.trace("stopping periodic updates of EService for application {} ", applicationContext.name()); try { periodicUpdates.cancel(true); periodicUpdates = null; } catch (Exception e) { - logger.warn("could not stop periodic updates of application {}", + logger.warn("could not stop periodic updates of EService for application {}", applicationContext.name(), e); } } @@ -261,7 +268,7 @@ public class EServiceHandler extends ApplicationLifecycleHandler { @Override public String toString() { - return RESOURCE_MANAGEMENT; + return Constants.RESOURCE_MANAGEMENT; } } 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 de99328..909639b 100644 --- a/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeHandler.java +++ b/src/main/java/org/gcube/smartgears/handler/resourceregistry/HostingNodeHandler.java @@ -70,7 +70,7 @@ public class HostingNodeHandler extends ContainerHandler { public void onStart(Start event) { try { logger.info("{} onStart started", this.getClass().getSimpleName()); - containerContext = event.context(); + this.containerContext = event.context(); init(); registerObservers(); schedulePeriodicUpdates(); @@ -121,12 +121,10 @@ public class HostingNodeHandler extends ContainerHandler { @Observes({ activation, part_activation}) void onChanged(ContainerLifecycle cl) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); - String currentToken = SecurityTokenProvider.instance.get(); - if (currentToken == null) { - currentToken = containerContext.configuration().startTokens().iterator().next(); + String previousToken = SecurityTokenProvider.instance.get(); + if (previousToken == null) { + ContextUtility.setContextFromToken(containerContext.configuration().startTokens().iterator().next()); } - ContextUtility.setContextFromToken(currentToken); - try { Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); ContainerState containerState = cl.state(); @@ -142,6 +140,7 @@ public class HostingNodeHandler extends ContainerHandler { } catch (Exception e) { logger.error("Failed to update {} State", HostingNode.NAME, e); } finally { + ContextUtility.setContextFromToken(previousToken); Thread.currentThread().setContextClassLoader(contextCL); } } @@ -149,17 +148,18 @@ public class HostingNodeHandler extends ContainerHandler { @Observes({ shutdown, stop, failure }) void onShutdown(ContainerLifecycle cl) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); - String currentToken = SecurityTokenProvider.instance.get(); - if (currentToken == null) - currentToken = containerContext.configuration().startTokens().iterator().next(); - - ContextUtility.setContextFromToken(currentToken); + String previousToken = SecurityTokenProvider.instance.get(); + if (previousToken == null) { + ContextUtility + .setContextFromToken(containerContext.configuration().startTokens().iterator().next()); + } try { Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); hostingNodeManager.updateFacets(); } catch (Exception e) { logger.error("cannot complete periodic update of {}", HostingNode.NAME, e); } finally { + ContextUtility.setContextFromToken(previousToken); Thread.currentThread().setContextClassLoader(contextCL); } } @@ -167,18 +167,18 @@ public class HostingNodeHandler extends ContainerHandler { @Observes(value = addToContext) void addTo(String token) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); - String currentToken = SecurityTokenProvider.instance.get(); - if (currentToken == null) { - currentToken = containerContext.configuration().startTokens().iterator().next(); + String previousToken = SecurityTokenProvider.instance.get(); + if (previousToken == null) { + ContextUtility + .setContextFromToken(containerContext.configuration().startTokens().iterator().next()); } - ContextUtility.setContextFromToken(currentToken); - try { Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); hostingNodeManager.addToContext(); } catch (Exception e) { logger.error("Failed to update Service State", e); } finally { + ContextUtility.setContextFromToken(previousToken); Thread.currentThread().setContextClassLoader(contextCL); } } @@ -186,12 +186,11 @@ public class HostingNodeHandler extends ContainerHandler { @Observes(value = removeFromContext) void removeFrom(String token) { ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); - String currentToken = SecurityTokenProvider.instance.get(); - if (currentToken == null) { - currentToken = containerContext.configuration().startTokens().iterator().next(); + String previousToken = SecurityTokenProvider.instance.get(); + if (previousToken == null) { + ContextUtility + .setContextFromToken(containerContext.configuration().startTokens().iterator().next()); } - ContextUtility.setContextFromToken(currentToken); - try { Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); hostingNodeManager.removeFromContext(); @@ -262,17 +261,6 @@ public class HostingNodeHandler extends ContainerHandler { logger.warn("could not stop periodic updates of {}", HostingNode.NAME, e); } } - - String currentToken = SecurityTokenProvider.instance.get(); - if (currentToken == null) - currentToken = containerContext.configuration().startTokens().iterator().next(); - - ContextUtility.setContextFromToken(currentToken); - try { - hostingNodeManager.updateFacets(); - } catch (Exception e) { - logger.error("cannot complete periodic update of {}", HostingNode.NAME, e); - } } }); 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 a77f89c..3b88f77 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 @@ -58,16 +58,18 @@ public class EServiceManager { private EService eService; private ServiceStateFacet serviceStateFacet; - public EServiceManager() { + private ApplicationContext applicationContext; + + public EServiceManager(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); } - public EService geteService() { + public EService getEService() { return eService; } - public void addToContext(ApplicationContext applicationContext) - throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { + public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { HostingNode hostingNode = applicationContext.container().properties() .lookup(Constants.HOSTING_NODE_MANAGER_PROPERTY).value(HostingNodeManager.class).getHostingNode(); boolean added = resourceRegistryPublisher.addResourceToCurrentContext(hostingNode); @@ -92,31 +94,31 @@ public class EServiceManager { } } - private String getBaseAddress(ApplicationContext context) { - ApplicationConfiguration configuration = context.configuration(); - ContainerConfiguration container = context.container().configuration(); + private String getBaseAddress() { + ApplicationConfiguration configuration = applicationContext.configuration(); + ContainerConfiguration container = applicationContext.container().configuration(); String baseAddress; if (configuration.proxied()) { String protocol = configuration.proxyAddress().protocol(); String port = configuration.proxyAddress().port() != null ? ":" + configuration.proxyAddress().port() : ""; baseAddress = String.format("%s://%s%s%s", protocol, configuration.proxyAddress().hostname(), port, - context.application().getContextPath()); + applicationContext.application().getContextPath()); } else { String protocol = container.protocol(); int port = container.port(); baseAddress = String.format("%s://%s:%d%s", protocol, container.hostname(), port, - context.application().getContextPath()); + applicationContext.application().getContextPath()); } return baseAddress; } - public String getState(ApplicationContext applicationContext) { + public String getState() { return applicationContext.lifecycle().state().remoteForm().toLowerCase(); } - private EService instantiateEService(ApplicationContext applicationContext) { + private EService instantiateEService() { logger.info("Creating {} for {}", EService.NAME, applicationContext.name()); ApplicationConfiguration applicationConfiguration = applicationContext.configuration(); @@ -137,7 +139,7 @@ public class EServiceManager { eService, softwareFacet); eService.addFacet(isIdentifiedBy); - String baseAddress = getBaseAddress(applicationContext); + String baseAddress = getBaseAddress(); for (ServletRegistration servlet : applicationContext.application().getServletRegistrations().values()) { if (!servletExcludes.contains(servlet.getName())) { for (String mapping : servlet.getMappings()) { @@ -159,14 +161,14 @@ public class EServiceManager { } serviceStateFacet = new ServiceStateFacetImpl(); - String state = getState(applicationContext); + String state = getState(); serviceStateFacet.setValue(state); eService.addFacet(serviceStateFacet); return eService; } - public EService createEService(ApplicationContext applicationContext) throws ResourceRegistryException { + public EService createEService() throws ResourceRegistryException { try { ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); UUID uuid = UUID.fromString(applicationContext.id()); @@ -179,28 +181,28 @@ public class EServiceManager { } catch (NotFoundException e) { // Fine we are going to create it again } - eService = instantiateEService(applicationContext); + eService = instantiateEService(); // eService = resourceRegistryPublisher.createResource(eService); - activates = createActivatesRelation(applicationContext, eService); + activates = createActivatesRelation(eService); } catch (AvailableInAnotherContextException | AlreadyPresentException e) { // resourceRegistryPublisher.delete(eService); // eService = resourceRegistryPublisher.createResource(eService); resourceRegistryPublisher.delete(activates); - activates = createActivatesRelation(applicationContext, eService); + activates = createActivatesRelation(eService); } eService = activates.getTarget(); serviceStateFacet = eService.getFacets(ServiceStateFacet.class).get(0); return eService; } - public void updateServiceStateFacet(ApplicationContext applicationContext) throws ResourceRegistryException { - String state = getState(applicationContext); + public void updateServiceStateFacet() throws ResourceRegistryException { + String state = getState(); serviceStateFacet.setValue(state); serviceStateFacet = resourceRegistryPublisher.updateFacet(serviceStateFacet); } - private Activates createActivatesRelation(ApplicationContext applicationContext, - EService eService) throws ResourceRegistryException { + private Activates createActivatesRelation(EService eService) + throws ResourceRegistryException { HostingNode hostingNode = applicationContext.container().properties() .lookup(Constants.HOSTING_NODE_MANAGER_PROPERTY).value(HostingNodeManager.class).getHostingNode(); @@ -226,13 +228,12 @@ public class EServiceManager { } - public void removeEService(ApplicationContext applicationContext) throws ResourceRegistryException { + public void removeEService() throws ResourceRegistryException { try { resourceRegistryPublisher.delete(eService); } catch (ResourceRegistryException e) { - logger.error("Unable to delete {}. Going to set the state to {}", applicationContext.name(), - getState(applicationContext)); - updateServiceStateFacet(applicationContext); + logger.error("Unable to delete {}. Going to set the state to {}", applicationContext.name(), getState()); + updateServiceStateFacet(); } } 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 a8d0745..a097668 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 @@ -70,7 +70,6 @@ import org.gcube.smartgears.configuration.container.Site; import org.gcube.smartgears.configuration.library.SmartGearsConfiguration; import org.gcube.smartgears.context.container.ContainerContext; import org.gcube.smartgears.handler.resourceregistry.ContextUtility; -import org.gcube.smartgears.lifecycle.container.ContainerLifecycle; import org.gcube.smartgears.provider.ProviderFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -188,7 +187,7 @@ public class HostingNodeManager { return hostingNode; } - public HostingNode updateStatus(ContainerLifecycle cl) throws ResourceRegistryException { + public HostingNode updateStatus() throws ResourceRegistryException { logger.debug("Setting {} down", HostingNode.NAME); ContainerStateFacet containerStateFacet = null;