dataminer/src/main/java/org/gcube/dataanalysis/wps/statisticalmanager/synchserver/utils/AlgorithmManager.java

91 lines
3.1 KiB
Java

package org.gcube.dataanalysis.wps.statisticalmanager.synchserver.utils;
import java.util.Set;
import net.opengis.wps.x100.ProcessDescriptionType;
import org.n52.wps.algorithm.annotation.Algorithm;
import org.n52.wps.server.IAlgorithm;
import org.reflections.Reflections;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AlgorithmManager {
private static long UPDATE_TIME_IN_MILLIS = 60000;
private static Logger log = LoggerFactory.getLogger(AlgorithmManager.class);
private static AlgorithmManager instance= new AlgorithmManager();
private Long lastUpdate = 0l;
private Reflections reflection;
public static synchronized AlgorithmManager getInstance(){
instance.updateRepository();
return instance;
}
public AlgorithmManager(){
updateRepository();
}
public ProcessDescriptionType getProcessDescription(String identifier) throws Exception{
log.info("getProcessDescription with identifier {} ",identifier);
Set<Class<?>> classes = reflection.getTypesAnnotatedWith(Algorithm.class);
for (Class<?> _class: classes){
if (_class.getAnnotation(Algorithm.class).identifier().equals(identifier)){
return ((IAlgorithm)_class.newInstance()).getDescription();
}
}
throw new Exception(String.format("Algorithm with process id %s not found", identifier));
}
public boolean containsAlgorithm(String identifier) {
log.info("containsAlgorithm with identifier {} ",identifier);
Set<Class<?>> classes = reflection.getTypesAnnotatedWith(Algorithm.class);
for (Class<?> _class: classes){
if (_class.getAnnotation(Algorithm.class).identifier().equals(identifier)){
return true;
}
}
return false;
}
public IAlgorithm getAlgorithm(String identifier) throws Exception{
log.info("getAlgorithm with identifier {} ",identifier);
Set<Class<?>> classes = reflection.getTypesAnnotatedWith(Algorithm.class);
for (Class<?> _class: classes){
if (_class.getAnnotation(Algorithm.class).identifier().equals(identifier)){
if (IAlgorithm.class.isAssignableFrom(_class)){
return (IAlgorithm)_class.newInstance();
} else {
log.warn("found algorothm class {} is no assignable from {}",_class.getName(), IAlgorithm.class.getName());
break;
}
}
}
throw new Exception(String.format("Algorithm with id %s not found", identifier));
}
public Set<Class<?>> getAllAlgorithms() {
return reflection.getTypesAnnotatedWith(Algorithm.class);
}
private synchronized void updateRepository(){
if ((System.currentTimeMillis()-lastUpdate)>UPDATE_TIME_IN_MILLIS){
log.info("update time passed, updating repository");
String packageToFind = "org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mappedclasses";
ConfigurationBuilder confBuilder = new ConfigurationBuilder()
.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(packageToFind)))
.setUrls(ClasspathHelper.forClassLoader());
reflection = new Reflections(confBuilder);
lastUpdate = System.currentTimeMillis();
}
}
}