2017-06-16 11:12:58 +02:00
|
|
|
package org.gcube.data.analysis.wps;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
2017-09-19 17:19:03 +02:00
|
|
|
import java.util.HashSet;
|
2017-06-16 11:12:58 +02:00
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
|
|
import org.gcube.data.analysis.wps.repository.GcubeAlgorithmRepository;
|
2017-10-20 18:09:10 +02:00
|
|
|
import org.gcube.dataanalysis.wps.statisticalmanager.synchserver.algorithms.AlgorithmClassification;
|
2017-06-16 11:12:58 +02:00
|
|
|
import org.gcube.dataanalysis.wps.statisticalmanager.synchserver.infrastructure.InfrastructureDialoguer;
|
|
|
|
import org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mapping.ConfigurationManager;
|
|
|
|
import org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mapping.TokenManager;
|
|
|
|
import org.n52.wps.commons.WPSConfig;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
public class GetCapabilitiesBuilder {
|
|
|
|
|
2017-10-20 18:09:10 +02:00
|
|
|
public static final String processString = "<wps:Process wps:processVersion=\"1.1.0\">\n\t<ows:Identifier>#CLASS#</ows:Identifier>\n\t<ows:Title>#TITLE#</ows:Title>\n</wps:Process>";
|
|
|
|
|
2017-06-16 11:12:58 +02:00
|
|
|
private static final Logger LOGGER= LoggerFactory.getLogger(GetCapabilitiesBuilder.class);
|
2017-10-20 18:09:10 +02:00
|
|
|
|
|
|
|
|
2017-06-16 11:12:58 +02:00
|
|
|
public String buildGetCapabilities(Map<String, String[]> parameters) throws Exception {
|
|
|
|
|
|
|
|
LinkedHashMap<String, Object> basicInputs = new LinkedHashMap<String, Object>();
|
|
|
|
ConfigurationManager configManager = new ConfigurationManager();
|
|
|
|
TokenManager tokenm = new TokenManager();
|
|
|
|
tokenm.getCredentials();
|
|
|
|
String scope = tokenm.getScope();
|
|
|
|
String username = tokenm.getUserName();
|
|
|
|
String token = tokenm.getToken();
|
|
|
|
basicInputs.put(ConfigurationManager.scopeParameter, scope);
|
|
|
|
basicInputs.put(ConfigurationManager.usernameParameter, username);
|
|
|
|
basicInputs.put(ConfigurationManager.tokenParameter, token);
|
2017-10-20 18:09:10 +02:00
|
|
|
|
2017-06-16 11:12:58 +02:00
|
|
|
configManager.configAlgorithmEnvironment(basicInputs);
|
|
|
|
LOGGER.debug("Initializing Capabilities Skeleton in scope " + configManager.getScope() + " with user " + configManager.getUsername());
|
|
|
|
InputStream is = this.getClass().getClassLoader().getResourceAsStream("templates/wpsCapabilitiesSkeleton.xml");
|
|
|
|
String stringTemplate = IOUtils.toString(is, "UTF-8");
|
2017-10-20 18:09:10 +02:00
|
|
|
|
2017-06-16 11:12:58 +02:00
|
|
|
//TODO: GET HOSTNAME AND PORT from container
|
|
|
|
String host = WPSConfig.getInstance().getWPSConfig().getServer().getHostname();
|
|
|
|
String port = WPSConfig.getInstance().getWPSConfig().getServer().getHostport();
|
|
|
|
stringTemplate = stringTemplate.replace("#HOST#", host).replace("#PORT#", port);
|
|
|
|
|
|
|
|
LOGGER.debug("Host: " + host + " Port: " + port);
|
|
|
|
|
|
|
|
LOGGER.debug("Getting algorithms from the infrastructure");
|
|
|
|
InfrastructureDialoguer dialoguer = new InfrastructureDialoguer(configManager.getScope());
|
|
|
|
List<String> algorithmsInScope = dialoguer.getAlgorithmsInScope();
|
|
|
|
LOGGER.debug("Found {} algorithms in scope {} ",algorithmsInScope.size() ,ScopeProvider.instance.get());
|
|
|
|
StringBuffer capabilities = new StringBuffer();
|
2017-10-20 18:09:10 +02:00
|
|
|
|
2017-09-19 17:19:03 +02:00
|
|
|
//TO eliminate duplicate coming from IS
|
|
|
|
Set<String> algorithmsSet = new HashSet<String>(algorithmsInScope);
|
2017-10-20 18:09:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
LOGGER.info("using classloader class {} ",Thread.currentThread().getContextClassLoader().getClass().getSimpleName());
|
|
|
|
|
|
|
|
Set<Class<?>> algorithmsClass = GcubeAlgorithmRepository.getAllAlgorithms();
|
|
|
|
|
|
|
|
LOGGER.info("class found with annotation Algorithm are {}",algorithmsClass.size());
|
|
|
|
|
|
|
|
for (Class<?> classfound : algorithmsClass) {
|
|
|
|
org.n52.wps.algorithm.annotation.Algorithm algorithmInfo = classfound.getAnnotation(org.n52.wps.algorithm.annotation.Algorithm.class);
|
|
|
|
if (algorithmInfo != null) {
|
|
|
|
LOGGER.debug("Retrieving local declared Algorithm: " + algorithmInfo.title());
|
|
|
|
String classification = "Others";
|
|
|
|
if (classfound.isAnnotationPresent(AlgorithmClassification.class))
|
|
|
|
classification = classfound.getAnnotation(AlgorithmClassification.class).value();
|
|
|
|
|
|
|
|
if (algorithmsSet.contains(algorithmInfo.title())){
|
|
|
|
String algorithmTitle = String.format("%s:%s", classification, algorithmInfo.title());
|
|
|
|
capabilities.append(processString.replace("#TITLE#", algorithmTitle).replace("#CLASS#", classfound.getName()));
|
|
|
|
}
|
2017-06-16 11:12:58 +02:00
|
|
|
}
|
2017-10-20 18:09:10 +02:00
|
|
|
|
|
|
|
|
2017-06-16 11:12:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stringTemplate = stringTemplate.replace("#PROCESSES#", capabilities.toString());
|
|
|
|
LOGGER.debug("Get capabilities built");
|
|
|
|
return stringTemplate;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|