diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java index 3dcc16e..245e848 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java @@ -33,6 +33,9 @@ public class SmartExecutorInitializator implements ApplicationManager { public static final long JOIN_TIMEOUT = 1000; + protected static GCoreISPublisher gCoreISPublisher; + protected static RestISPublisher restISPublisher; + /** * {@inheritDoc} * The method discover the plugins available on classpath and their own diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java index 579a16f..2a81fc1 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java @@ -3,11 +3,14 @@ package org.gcube.vremanagement.executor.ispublisher; import java.util.Map; import java.util.UUID; +import org.gcube.informationsystem.model.impl.properties.HeaderImpl; import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; import org.gcube.informationsystem.model.reference.entities.Resource; 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.exceptions.AvailableInAnotherContextException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient; import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory; @@ -37,63 +40,88 @@ public class RestISPublisher extends ISPublisher { private static Logger logger = LoggerFactory.getLogger(RestISPublisher.class); protected final UUID eServiceUUID; + protected ResourceRegistryClient resourceRegistryClient; + protected ResourceRegistryPublisher resourceRegistryPublisher; public RestISPublisher(ApplicationContext applicationContext) { super(applicationContext); this.eServiceUUID = UUID.fromString(applicationContext.id()); + this.resourceRegistryClient = ResourceRegistryClientFactory.create(); + this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); } - @Override - public void publishPlugins(Map> availablePlugins) throws Exception { - ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); - ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); + @SuppressWarnings("unused") + protected RunningPlugin publishRunningPluginWithRelations(String pluginName) throws Exception { + Plugin plugin = PluginManager.getPlugin(pluginName); - for(String pluginName : availablePlugins.keySet()) { - Plugin plugin = PluginManager.getPlugin(pluginName); - - RunningPlugin runningPlugin = new RunningPluginImpl(); - - SoftwareFacet softwareFacet = new SoftwareFacetImpl(); - softwareFacet.setGroup(plugin.getGroup()); - softwareFacet.setName(pluginName); - softwareFacet.setVersion(plugin.getVersion()); - softwareFacet.setDescription(plugin.getDescription()); - runningPlugin.addFacet(softwareFacet); - - Map pluginCapabilities = plugin.getSupportedCapabilities(); - for(String capabilityName : pluginCapabilities.keySet()) { - SimplePropertyFacet simplePropertyFacet = new SimplePropertyFacetImpl(); - simplePropertyFacet.setName(capabilityName); - simplePropertyFacet.setValue(pluginCapabilities.get(capabilityName)); - runningPlugin.addFacet(simplePropertyFacet); - } - - - EService smartExecutorEService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); - PropagationConstraint usesPropagationConstraint = new PropagationConstraintImpl(); - usesPropagationConstraint.setAddConstraint(AddConstraint.propagate); - usesPropagationConstraint.setRemoveConstraint(RemoveConstraint.cascade); - - Uses uses = new UsesImpl(smartExecutorEService, runningPlugin, usesPropagationConstraint); + RunningPlugin runningPlugin = new RunningPluginImpl(); + + SoftwareFacet softwareFacet = new SoftwareFacetImpl(); + softwareFacet.setGroup(plugin.getGroup()); + softwareFacet.setName(pluginName); + softwareFacet.setVersion(plugin.getVersion()); + softwareFacet.setDescription(plugin.getDescription()); + runningPlugin.addFacet(softwareFacet); + + Map pluginCapabilities = plugin.getSupportedCapabilities(); + for(String capabilityName : pluginCapabilities.keySet()) { + SimplePropertyFacet simplePropertyFacet = new SimplePropertyFacetImpl(); + simplePropertyFacet.setName(capabilityName); + simplePropertyFacet.setValue(pluginCapabilities.get(capabilityName)); + runningPlugin.addFacet(simplePropertyFacet); + } + + EService smartExecutorEService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); + PropagationConstraint usesPropagationConstraint = new PropagationConstraintImpl(); + usesPropagationConstraint.setAddConstraint(AddConstraint.propagate); + usesPropagationConstraint.setRemoveConstraint(RemoveConstraint.cascade); + + Uses uses = new UsesImpl(smartExecutorEService, runningPlugin, usesPropagationConstraint); + try { + resourceRegistryPublisher.createIsRelatedTo(uses); + } catch (ResourceRegistryException e) { + logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, pluginName); + throw e; + } + + org.gcube.resourcemanagement.model.reference.entities.resources.Plugin pluginResource = null; + if(pluginResource!=null) { // The if allows not commenting the following code in the meanwhile the pluginResource retrieving is properly coded + PropagationConstraint enablesPropagationConstraint = new PropagationConstraintImpl(); + enablesPropagationConstraint.setAddConstraint(AddConstraint.propagate); + enablesPropagationConstraint.setRemoveConstraint(RemoveConstraint.keep); + Enables enables = new EnablesImpl(runningPlugin, pluginResource, enablesPropagationConstraint); try { - resourceRegistryPublisher.createIsRelatedTo(uses); + resourceRegistryPublisher.createIsRelatedTo(enables); } catch (ResourceRegistryException e) { logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, pluginName); throw e; } + } + + return runningPlugin; + } + + @Override + public void publishPlugins(Map> availablePlugins) throws Exception { + + UUID contextUUID = resourceRegistryClient.getCurrentContext().getHeader().getUUID(); + + for(String pluginName : availablePlugins.keySet()) { - org.gcube.resourcemanagement.model.reference.entities.resources.Plugin pluginResource = null; - if(pluginResource!=null) { // The if allows not commenting the following code in the meanwhile the pluginResource retrieving is properly coded - PropagationConstraint enablesPropagationConstraint = new PropagationConstraintImpl(); - enablesPropagationConstraint.setAddConstraint(AddConstraint.propagate); - enablesPropagationConstraint.setRemoveConstraint(RemoveConstraint.keep); - Enables enables = new EnablesImpl(runningPlugin, pluginResource, enablesPropagationConstraint); - try { - resourceRegistryPublisher.createIsRelatedTo(enables); - } catch (ResourceRegistryException e) { - logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, pluginName); - throw e; - } + UUID uuid = PluginManager.getPluginUUID(pluginName); + + RunningPlugin runningPlugin; + + try { + runningPlugin = resourceRegistryClient.getInstance(RunningPlugin.class, uuid); + } catch (NotFoundException e) { + runningPlugin = publishRunningPluginWithRelations(pluginName); + } catch (AvailableInAnotherContextException e) { + runningPlugin = new RunningPluginImpl(); + runningPlugin.setHeader(new HeaderImpl(uuid)); + resourceRegistryPublisher.addToContext(contextUUID, runningPlugin); + } catch (ResourceRegistryException e) { + throw e; } } diff --git a/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java b/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java index 91a7839..f109c97 100644 --- a/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java +++ b/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java @@ -3,6 +3,7 @@ package org.gcube.vremanagement.executor.pluginmanager; import java.util.HashMap; import java.util.Map; import java.util.ServiceLoader; +import java.util.UUID; import org.gcube.vremanagement.executor.exception.ExecutorException; import org.gcube.vremanagement.executor.exception.PluginNotFoundException; @@ -34,6 +35,8 @@ public class PluginManager { */ private Map> availablePlugins; + private Map uuids; + /** * Get the singleton instance of {@link #PluginManager}. * The first time this function is invoked the instance is null @@ -53,13 +56,15 @@ public class PluginManager { */ protected PluginManager(){ logger.debug("Loading plugins available on classpath"); - this.availablePlugins = new HashMap>(); + this.availablePlugins = new HashMap<>(); + this.uuids = new HashMap<>(); ServiceLoader serviceLoader = ServiceLoader.load(Plugin.class); for (Plugin plugin : serviceLoader) { try { logger.debug(String.format("%s plugin found", plugin.getName())); String name = plugin.getName(); this.availablePlugins.put(name, plugin.getClass()); + this.uuids.put(name, UUID.randomUUID()); } catch (Exception e) { logger.debug(String.format("%s not initialized correctly. It will not be used", plugin.getName())); } @@ -84,10 +89,17 @@ public class PluginManager { } } + + public static UUID getPluginUUID(String pluginName) { + return PluginManager.getInstance().uuids.get(pluginName); + } + /** * @return the availablePlugins */ public static Map> getAvailablePlugins() { return PluginManager.getInstance().availablePlugins; } + + }