gFeed/gCat-Feeder/src/main/java/org/gcube/data/publishing/gCatFeeder/service/engine/impl/CollectorsManagerImpl.java

84 lines
2.8 KiB
Java

package org.gcube.data.publishing.gCatFeeder.service.engine.impl;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import javax.inject.Singleton;
import org.gcube.data.publishing.gCatFeeder.catalogues.CataloguePlugin;
import org.gcube.data.publishing.gCatFeeder.service.engine.CollectorsManager;
import org.gcube.data.publishing.gCatFeeder.service.model.fault.CataloguePluginNotFound;
import org.gcube.data.publishing.gCatFeeder.service.model.fault.CollectorNotFound;
import org.gcube.data.publishing.gCatFeeder.service.model.fault.InternalError;
import org.gcube.data.publishing.gCatFeeder.utils.ContextUtils;
import org.gcube.data.publishing.gCatfeeder.collectors.CollectorPlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Singleton
public class CollectorsManagerImpl implements CollectorsManager {
private static final Logger log= LoggerFactory.getLogger(CollectorsManagerImpl.class);
private ServiceLoader<CollectorPlugin> collectorPluginsLoader = null;
private ConcurrentHashMap<String,CollectorPlugin> availablePlugins=null;
public CollectorsManagerImpl() {
//load plugins
log.debug("Loading collector plugins...");
collectorPluginsLoader=ServiceLoader.load(CollectorPlugin.class);
for(CollectorPlugin plugin:collectorPluginsLoader) {
log.debug("Loading {} ",plugin.getClass());
log.debug("Descriptor {} ",plugin.getDescriptor());
availablePlugins.put(plugin.getDescriptor().getName(), plugin);
}
log.trace("Loaded {} collector plugins ",availablePlugins.size());
}
@Override
public Set<String> getAvailableCollectors() {
return availablePlugins.keySet();
}
@Override
public CollectorPlugin<?> getPluginById(String collectorId) throws CollectorNotFound {
if(availablePlugins.containsKey(collectorId)) return availablePlugins.get(collectorId);
else throw new CollectorNotFound("Collector plugin "+collectorId+" not available.");
}
@Override
public void init() throws InternalError {
log.trace("Static initialization...");
for(Entry<String,CollectorPlugin> entry:availablePlugins.entrySet()) {
log.debug("Static initialization for : {} ",entry.getKey());
try {
entry.getValue().init();
}catch(Throwable t) {
log.error("Unexpected exception while initializing {} ",entry.getKey(),t);
}
}
}
@Override
public void initInScope() throws InternalError {
log.trace("Initialization under scope {} ",ContextUtils.getCurrentScope());
for(Entry<String,CollectorPlugin> entry:availablePlugins.entrySet()) {
log.debug("Scope initialization for : {} ",entry.getKey());
try {
entry.getValue().initInScope();
}catch(Throwable t) {
log.error("Unexpected exception while initializing {} ",entry.getKey(),t);
}
}
}
}