2016-04-01 13:09:40 +02:00
|
|
|
package org.gcube.dataanalysis.wps.statisticalmanager.synchserver.mapping;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import org.gcube.dataanalysis.ecoengine.configuration.AlgorithmConfiguration;
|
|
|
|
import org.n52.wps.commons.WPSConfig;
|
2017-05-05 10:08:44 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2016-04-01 13:09:40 +02:00
|
|
|
|
|
|
|
public class ConfigurationManager {
|
|
|
|
|
2017-05-05 10:08:44 +02:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ConfigurationManager.class);
|
|
|
|
|
2016-04-01 13:09:40 +02:00
|
|
|
public static String serviceUserNameParameterVariable = "ServiceUserName";
|
|
|
|
public static String processingSessionVariable = "Session";
|
|
|
|
public static String webpathVariable = "WebPath";
|
|
|
|
public static String webPersistencePathVariable = "";
|
|
|
|
public static String usernameParameter = "user.name";
|
|
|
|
public static String scopeParameter = "scope";
|
2016-09-27 15:44:14 +02:00
|
|
|
public static String tokenParameter = "usertoken";
|
2016-04-01 13:09:40 +02:00
|
|
|
public static String defaultScope= "/gcube/devsec";
|
|
|
|
public static String defaultUsername= "statistical.wps";
|
|
|
|
|
|
|
|
private static Integer maxComputations = null;
|
|
|
|
private static Boolean useStorage = null;
|
|
|
|
static boolean simulationMode = false;
|
|
|
|
|
2019-04-17 17:36:38 +02:00
|
|
|
EnvironmentVariableManager env = null;
|
|
|
|
|
2016-04-01 13:09:40 +02:00
|
|
|
public static synchronized Integer getMaxComputations(){
|
|
|
|
return maxComputations;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static synchronized Boolean useStorage(){
|
|
|
|
return useStorage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static synchronized Boolean isSimulationMode(){
|
|
|
|
return simulationMode;
|
|
|
|
}
|
|
|
|
|
2018-10-12 15:53:36 +02:00
|
|
|
@Deprecated
|
2016-04-01 13:09:40 +02:00
|
|
|
public void getInitializationProperties() {
|
2018-10-12 15:53:36 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void inizializePropertiesUsingTemplateFile() {
|
2016-04-01 13:09:40 +02:00
|
|
|
try {
|
|
|
|
if (maxComputations == null) {
|
|
|
|
Properties options = new Properties();
|
|
|
|
InputStream is = this.getClass().getClassLoader().getResourceAsStream("templates/setup.cfg");
|
|
|
|
options.load(is);
|
|
|
|
is.close();
|
|
|
|
maxComputations = Integer.parseInt(options.getProperty("maxcomputations"));
|
2018-06-14 11:29:15 +02:00
|
|
|
logger.info("setting max computation to {}",maxComputations);
|
2016-04-01 13:09:40 +02:00
|
|
|
useStorage = Boolean.parseBoolean(options.getProperty("saveond4sstorage"));
|
|
|
|
simulationMode=Boolean.parseBoolean(options.getProperty("simulationMode"));
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2017-05-05 10:08:44 +02:00
|
|
|
logger.error("error initializing properties",e);
|
2016-04-01 13:09:40 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-12 15:53:36 +02:00
|
|
|
|
2016-04-01 13:09:40 +02:00
|
|
|
private AlgorithmConfiguration config;
|
|
|
|
private String scope;
|
|
|
|
private String username;
|
2016-09-27 15:44:14 +02:00
|
|
|
private String token;
|
2016-04-01 13:09:40 +02:00
|
|
|
|
|
|
|
public String getScope() {
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getUsername() {
|
|
|
|
return username;
|
|
|
|
}
|
|
|
|
|
2018-10-12 15:53:36 +02:00
|
|
|
public ConfigurationManager(EnvironmentVariableManager env) {
|
|
|
|
if (env == null) inizializePropertiesUsingTemplateFile();
|
2019-04-17 17:36:38 +02:00
|
|
|
else {
|
|
|
|
maxComputations = env.getMaxComputation();
|
|
|
|
useStorage = env.isSaveOnStorage();
|
|
|
|
simulationMode = env.isSimulationMode();
|
|
|
|
}
|
2016-04-01 13:09:40 +02:00
|
|
|
}
|
2018-10-12 15:53:36 +02:00
|
|
|
|
2016-04-01 13:09:40 +02:00
|
|
|
public AlgorithmConfiguration getConfig() {
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setComputationId(String computationId){
|
|
|
|
config.setTaskID(computationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void configAlgorithmEnvironment(LinkedHashMap<String, Object> inputs) throws Exception {
|
|
|
|
// set config container
|
|
|
|
config = new AlgorithmConfiguration();
|
2017-09-19 17:19:27 +02:00
|
|
|
config.setAlgorithmClassLoader(Thread.currentThread().getContextClassLoader());
|
2016-04-01 13:09:40 +02:00
|
|
|
String webperspath = WPSConfig.getConfigDir() + "../persistence/";
|
|
|
|
// selecting persistence path
|
|
|
|
// String persistencePath = File.createTempFile("wpsstatcheck", ".sm").getParent() + "/../cfg/";
|
2017-09-19 18:53:28 +02:00
|
|
|
|
|
|
|
//TODO: REMOVE this shit (the persistence must be the persistence dir of the webapp)
|
2016-04-01 13:09:40 +02:00
|
|
|
String persistencePath = WPSConfig.getConfigDir() + "../ecocfg/";
|
|
|
|
String configPath = persistencePath;
|
|
|
|
if (!new File(configPath).isDirectory()) {
|
|
|
|
configPath = "./cfg/";
|
|
|
|
persistencePath = "./";
|
|
|
|
}
|
2017-05-05 10:08:44 +02:00
|
|
|
logger.debug("Taking configuration from " + (new File(configPath).getAbsolutePath()) + " and persistence in " + persistencePath);
|
2016-04-01 13:09:40 +02:00
|
|
|
// setting configuration and logger
|
|
|
|
config.setPersistencePath(persistencePath);
|
|
|
|
config.setConfigPath(configPath);
|
|
|
|
config.setNumberOfResources(1);
|
|
|
|
// setting application paths
|
|
|
|
String webapp = WPSConfig.getInstance().getWPSConfig().getServer().getWebappPath();
|
|
|
|
String host = WPSConfig.getInstance().getWPSConfig().getServer().getHostname();
|
|
|
|
String port = WPSConfig.getInstance().getWPSConfig().getServer().getHostport();
|
2017-05-05 10:08:44 +02:00
|
|
|
logger.debug("Host: " + host + " Port: " + port + " Webapp: " + webapp + " ");
|
|
|
|
logger.debug("Web persistence path: " + webperspath);
|
2016-04-01 13:09:40 +02:00
|
|
|
|
|
|
|
String webPath = "http://" + host + ":" + port + "/" + webapp + "/persistence/";
|
|
|
|
|
2017-05-05 10:08:44 +02:00
|
|
|
// logger.debug("Env Vars: \n"+System.getenv());
|
|
|
|
logger.debug("Web app path: " + webPath);
|
2016-04-01 13:09:40 +02:00
|
|
|
|
|
|
|
// retrieving scope
|
|
|
|
scope = (String) inputs.get(scopeParameter);
|
2017-05-05 10:08:44 +02:00
|
|
|
logger.debug("Retrieved scope: " + scope);
|
2016-04-01 13:09:40 +02:00
|
|
|
if (scope == null)
|
|
|
|
throw new Exception("Error: scope parameter (scope) not set! This violates e-Infrastructure security policies");
|
|
|
|
if (!scope.startsWith("/"))
|
|
|
|
scope = "/" + scope;
|
|
|
|
|
|
|
|
username = (String) inputs.get(usernameParameter);
|
2016-09-27 15:44:14 +02:00
|
|
|
token = (String) inputs.get(tokenParameter);
|
|
|
|
|
2017-05-05 10:08:44 +02:00
|
|
|
logger.debug("User name used by the client: " + username);
|
|
|
|
logger.debug("User token used by the client: " + token);
|
2016-09-27 15:44:14 +02:00
|
|
|
|
2016-04-01 13:09:40 +02:00
|
|
|
if (username == null || username.trim().length() == 0)
|
|
|
|
throw new Exception("Error: user name parameter (user.name) not set! This violates e-Infrastructure security policies");
|
|
|
|
|
2016-09-27 15:44:14 +02:00
|
|
|
if (token == null || token.trim().length() == 0)
|
|
|
|
throw new Exception("Error: token parameter not set! This violates e-Infrastructure security policies");
|
|
|
|
|
2016-04-01 13:09:40 +02:00
|
|
|
config.setGcubeScope(scope);
|
2016-09-27 15:44:14 +02:00
|
|
|
config.setGcubeUserName(username);
|
|
|
|
config.setGcubeToken(token);
|
2016-04-01 13:09:40 +02:00
|
|
|
// DONE get username from request
|
|
|
|
config.setParam(serviceUserNameParameterVariable, username);
|
|
|
|
config.setParam(processingSessionVariable, "" + UUID.randomUUID());
|
|
|
|
config.setParam(webpathVariable, webPath);
|
|
|
|
config.setParam(webPersistencePathVariable, webperspath);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|