Implementing new IS publisher

This commit is contained in:
Luca Frosini 2020-09-30 16:18:46 +02:00
parent 90512acf7b
commit 36a4e6684d
3 changed files with 88 additions and 45 deletions

View File

@ -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

View File

@ -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,18 +40,18 @@ 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<String, Class<? extends Plugin>> availablePlugins) throws Exception {
ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create();
ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
for(String pluginName : availablePlugins.keySet()) {
@SuppressWarnings("unused")
protected RunningPlugin publishRunningPluginWithRelations(String pluginName) throws Exception {
Plugin plugin = PluginManager.getPlugin(pluginName);
RunningPlugin runningPlugin = new RunningPluginImpl();
@ -68,7 +71,6 @@ public class RestISPublisher extends ISPublisher {
runningPlugin.addFacet(simplePropertyFacet);
}
EService smartExecutorEService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
PropagationConstraint usesPropagationConstraint = new PropagationConstraintImpl();
usesPropagationConstraint.setAddConstraint(AddConstraint.propagate);
@ -96,6 +98,32 @@ public class RestISPublisher extends ISPublisher {
}
}
return runningPlugin;
}
@Override
public void publishPlugins(Map<String, Class<? extends Plugin>> availablePlugins) throws Exception {
UUID contextUUID = resourceRegistryClient.getCurrentContext().getHeader().getUUID();
for(String pluginName : availablePlugins.keySet()) {
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;
}
}
}

View File

@ -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<String, Class<? extends Plugin>> availablePlugins;
private Map<String, UUID> 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<String, Class<? extends Plugin>>();
this.availablePlugins = new HashMap<>();
this.uuids = new HashMap<>();
ServiceLoader<Plugin> 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<String, Class<? extends Plugin>> getAvailablePlugins() {
return PluginManager.getInstance().availablePlugins;
}
}