Config.java [src/csip] Revision: f27b0b136832f2e9c4a345cbdef5967b2442f743 Date: Fri Apr 21 16:24:27 MDT 2017
/*
* $Id$
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* a Model-as-a-Service framework, API and application suite.
*
* 2012-2017, Olaf David and others, OMSLab, Colorado State University.
*
* OMSLab licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
*/
package csip;
import csip.ModelDataService.Task;
import csip.utils.Binaries;
import csip.utils.SimpleCache;
import java.io.File;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Global properties, changeable at runtime.
*
* @author od
*/
public class Config {
private static final Properties p = new Properties();
private static final Properties allProps = new Properties();
private static final LinkedList<PostgresChunk> pcs = new LinkedList<>();
private static final List<Task> tasks = Collections.synchronizedList(new ArrayList<Task>());
//
//
private static ArchiveStore archive;
private static SessionStore session;
private static ResultStore resultStore;
//
private static ExecutorService exec;
private static Timer timer;
private static final Registry reg = new Registry();
static final Logger LOG = Logger.getLogger(Config.class.getName());
//
public static final String CSIP_VERSION = "csip.version";
public static final String CSIP_ARCH = "csip.arch";
public static final String CSIP_REMOTE_ACL = "csip.remote.acl";
public static final String CSIP_TIMEZONE = "csip.timezone";
public static final String CSIP_LOGGING_LEVEL = "csip.logging.level";
public static final String CSIP_RESPONSE_STACKTRACE = "csip.response.stacktrace";
//
public static final String CSIP_SESSION_BACKEND = "csip.session.backend";
public static final String CSIP_SESSION_TTL = "csip.session.ttl";
public static final String CSIP_SESSION_MONGODB_URI = "csip.session.mongodb.uri";
public static final String CSIP_SESSION_TTL_FAILED = "csip.session.ttl.failed";
public static final String CSIP_SESSION_TTL_CANCELLED = "csip.session.ttl.cancelled";
public static final String CSIP_ARCHIVE_BACKEND = "csip.archive.backend";
public static final String CSIP_ARCHIVE_MONGODB_URI = "csip.archive.mongodb.uri";
public static final String CSIP_ARCHIVE_MAX_FILE_SIZE = "csip.archive.max.filesize";
public static final String CSIP_ARCHIVE_TTL = "csip.archive.ttl";
public static final String CSIP_ARCHIVE_FAILEDONLY = "csip.archive.failedonly";
public static final String CSIP_RESULTSTORE_BACKEND = "csip.resultstore.backend";
public static final String CSIP_RESULTSTORE_MONGODB_URI = "csip.resultstore.mongodb.uri";
//
public static final String CSIP_DIR = "csip.dir";
public static final String CSIP_BIN_DIR = "csip.bin.dir";
public static final String CSIP_WORK_DIR = "csip.work.dir";
public static final String CSIP_RESULTS_DIR = "csip.results.dir";
public static final String CSIP_CACHE_DIR = "csip.cache.dir";
public static final String CSIP_DATA_DIR = "csip.data.dir";
//
public static final String CSIP_LOGGING_STRMAX = "csip.logging.strmax";
public static final String CSIP_SNAPSHOT = "csip.snapshot";
public static final String CSIP_KEEPWORKSPACE = "csip.keepworkspace";
public static final String CSIP_JDBC_CHECKVALID = "csip.jdbc.checkvalid";
// values for session/archive/result
public static final String MONGODB = "mongodb";
public static final String LOCAL = "local";
public static final String SQL = "sql";
public static final String NONE = "none";
public static Map<Object, Object> properties() {
return Collections.unmodifiableMap(p);
}
//
static SimpleCache<File, ReentrantLock> wsFileLocks = new SimpleCache<>();
static {
/*
The CSIP version
*/
put(CSIP_VERSION, "$version: 2.1.182 4b223e4206b6 2017-04-21 od, built at 2017-04-21 16:22 by od$");
/*
* The runtime architecture.
*/
put(CSIP_ARCH, Binaries.getArch());
// for legacy settings only.
put("arch", Binaries.getArch());
// remote access acl
/*
remote access for UI and config. Provide a list of IPs or
subnets that are allows to connect. This ACL is not for the
services, just service management. The default is localhost only.
Example: "csip.remoteaccess.acl": "127.0.0.1/32 10.2.222.0/24"
*/
put(CSIP_REMOTE_ACL, "127.0.0.1/32");
// session
/*
The backend to use for session management. valid choices are
"mongodb", "sql", "local".
*/
put(CSIP_SESSION_BACKEND, LOCAL);
/*
The mongodb connecion string.
*/
put(CSIP_SESSION_MONGODB_URI, "mongodb://localhost:27017/csip");
/*
The default time in seconds for a session to stay active after the
model finishes. All model results will be available for that period.
After this period expires the session will be removed and the model results
will be removed or archived. This value can be altered using
the "keep_results" metainfo value of a request.
see duration string examples: https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#parse-java.lang.CharSequence-
*/
put(CSIP_SESSION_TTL, "PT30S"); // 30 sec ttl
put(CSIP_SESSION_TTL_FAILED, "PT30S");
put(CSIP_SESSION_TTL_CANCELLED, "PT30S");
/*
The default csip timezone to be used for time management.
*/
put(CSIP_TIMEZONE, "MST7MDT");
// archive
/*
defines the archive backend implementation: "mongodb" or "none" are
possible.
"none" means disabled.
*/
put(CSIP_ARCHIVE_BACKEND, NONE);
/*
The mongodb connection uri, if the backend is set to "mongodb"
*/
put(CSIP_ARCHIVE_MONGODB_URI, "mongodb://localhost:27017/csip");
/*
The max file size for an attachment to be archived.
*/
put(CSIP_ARCHIVE_MAX_FILE_SIZE, "10MB");
/*
The default time in seconds for an entry to stay in the archive.
All archived model results will be available for that period after
the session expired. After this period expires the archive will be removed.
see duration string examples: https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#parse-java.lang.CharSequence-.
*/
put(CSIP_ARCHIVE_TTL, "P1D"); // one day.
/*
If the archive is enabled, only archive failed runs, default: false
*/
put(CSIP_ARCHIVE_FAILEDONLY, "false");
// logger
/*
The log level for service logging. This is only used if "csip.logging.enabled"
is set to true. All java.util.logging.Level log levels are usable.
*/
put(CSIP_LOGGING_LEVEL, "INFO");
/*
control if the stack trace should be part of the response metadata
if the logic fails. default is true.
*/
put(CSIP_RESPONSE_STACKTRACE, "true");
/* Result store handling
*/
put(CSIP_RESULTSTORE_BACKEND, NONE);
put(CSIP_RESULTSTORE_MONGODB_URI, "mongodb://localhost:27017/csip");
/*
The csip root directory
*/
put(CSIP_DIR, "/tmp/csip");
/*
The csip directories for executables.
*/
put(CSIP_BIN_DIR, "${csip.dir}/bin");
/*
The csip directory for sessions.
*/
put(CSIP_WORK_DIR, "${csip.dir}/work");
/*
The csip directory to store results.
*/
put(CSIP_RESULTS_DIR, "${csip.dir}/results");
/*
The csip cache file directory.
*/
put(CSIP_CACHE_DIR, "${csip.dir}/cache");
/*
The csip data directory.
*/
put(CSIP_DATA_DIR, "${csip.dir}/data");
/*
External url pats These
properties can be set to force a public scheme/host/protocol for
result file downloads and catalog listing. These properties
are not set per default. They can be set independently from
each other to change only selective parts of the URL.
If none of the propeties below are set the incomming URL is
being used to construct downloads and catalogs.
*/
// put("csip.public.scheme", ""); // e.g. https
// put("csip.public.host", ""); // e.g. csip.org
// put("csip.public.port", ""); // e.g. 8080 (-1 will remove the port in the url)
///////////////////////////////////////////////////////
//// more auxiliary properties
// thread management
put("codebase.threadpool", "32"); // 10 concurrent model runs.
put("codebase.url", "http://localhost:8080");
put("codebase.port", "8085");
put("codebase.localport", "8081");
put("codebase.servicename", "csip-vmscaler");
// postgis
put("pg.chunks", "0");
put("pg.url", "jdbc:postgresql://oms-db.engr.colostate.edu:5432/r2gis");
put("pg.connpool", "16"); // 16 default connections for db connection pool
// wine
put("wine.path", "/usr/bin/wine");
//rusle2/weps
put("r2.path", "/od/projects/csip.services/bin/RomeShell.exe"); // the path is the parent directory.
put("r2.db", "http://oms-db.engr.colostate.edu/r2");
put("weps.db", "http://oms-db.engr.colostate.edu/weps");
//oms related props
put("oms.java.home", "/usr/bin");
put("oms.esp.threads", "4");
// internal
put("vm.port", "8080");
update();
}
public static void register(Set<Class<?>> service) {
getRegistry().register(service);
}
/**
* @return @deprecated use register() instead.
*/
@Deprecated
public static Registry registry() {
return reg;
}
static Registry getRegistry() {
return reg;
}
static boolean isArchiveEnabled() {
return !getString(CSIP_ARCHIVE_BACKEND, NONE).equals(NONE);
}
static boolean isResultStoreEnabled() {
return !getString(CSIP_RESULTSTORE_BACKEND, NONE).equals(NONE);
}
static synchronized SessionStore getSessionStore() {
if (session == null) {
String uri = null;
switch (getString(CSIP_SESSION_BACKEND)) {
case MONGODB:
uri = getString(CSIP_SESSION_MONGODB_URI);
if (uri == null) {
throw new RuntimeException("missing uri configuration entry 'csip.session.mongodb.uri'");
}
session = new MongoSessionStore(uri);
break;
case SQL:
uri = getString("csip.session.sql.uri");
if (uri == null) {
throw new RuntimeException("missing uri configuration entry 'csip.session.sql.uri'");
}
session = new SQLSessionStore(uri);
break;
case LOCAL:
session = new LocalSessionStore();
break;
default:
throw new RuntimeException("unknown session backend: " + getString(CSIP_SESSION_BACKEND));
}
}
return session;
}
static synchronized ArchiveStore getArchiveStore() {
if (archive == null) {
if (!isArchiveEnabled()) {
archive = ArchiveStore.NONE;
} else {
switch (getString(CSIP_ARCHIVE_BACKEND)) {
case MONGODB:
String uri = getString(CSIP_ARCHIVE_MONGODB_URI);
if (uri == null) {
throw new RuntimeException("missing uri configuration entry 'csip.archive.mongodb.uri'");
}
archive = new MongoArchiveStore(uri);
break;
case NONE:
archive = ArchiveStore.NONE;
break;
default:
throw new RuntimeException("unknown archive backend: " + getString(CSIP_ARCHIVE_BACKEND));
}
}
}
return archive;
}
static synchronized ResultStore getResultStore() {
if (resultStore == null) {
if (!isResultStoreEnabled()) {
resultStore = ResultStore.NONE;
} else {
switch (getString(CSIP_RESULTSTORE_BACKEND)) {
case MONGODB:
resultStore = new MongoResultStore(getString(CSIP_RESULTSTORE_MONGODB_URI));
break;
case NONE:
resultStore = ResultStore.NONE;
break;
default:
throw new RuntimeException("unknown resultstore backend: " + getString(CSIP_RESULTSTORE_BACKEND));
}
}
}
return resultStore;
}
static synchronized Timer getTimer() {
if (timer == null) {
timer = new Timer();
}
return timer;
}
static synchronized ExecutorService getExecutorService() {
if (exec == null) {
exec = Executors.newCachedThreadPool();
}
return exec;
}
static List<ModelDataService.Task> getModelTasks() {
return tasks;
}
/**
* Start up the servlet.
*
* @param context
*/
static void startup() {
}
/**
* Shut down the servlet.
*
* @param context
*/
static void shutdown() {
for (ModelDataService.Task t : tasks) {
t.cancel();
}
reg.unregister();
if (exec != null) {
LOG.info("Shutting down ExecutorService");
exec.shutdownNow();
}
if (session != null) {
try {
session.shutdown();
} catch (Exception ex) {
LOG.log(Level.SEVERE, "Exception", ex);
}
session = null;
}
if (archive != null) {
try {
archive.shutdown();
} catch (Exception ex) {
LOG.log(Level.SEVERE, "Exception", ex);
}
archive = null;
}
if (resultStore != null) {
try {
resultStore.shutdown();
} catch (Exception ex) {
LOG.log(Level.SEVERE, "Exception", ex);
}
resultStore = null;
}
if (timer != null) {
timer.cancel();
}
Binaries.shutdownJDBC();
}
/*
This is being called upon configuration update.
*/
static void update() {
rehashProperties();
}
static public Collection<PostgresChunk> getPostgresChunks() {
return pcs;
}
public static boolean hasProperty(String key) {
return allProps.containsKey(key);
}
public static boolean isString(String key, String str) {
String s = getString(key);
return (s != null) && s.equals(str);
}
public static String getString(String key, String def) {
return getP(key, def);
}
public static String getString(String key) {
return getP(key, null);
}
public static boolean getBoolean(String key, boolean def) {
return Boolean.parseBoolean(getP(key, Boolean.toString(def)));
}
public static boolean getBoolean(String key) {
return Boolean.parseBoolean(getP(key, "false"));
}
public static int getInt(String key, int def) {
return Integer.parseInt(getP(key, Integer.toString(def)));
}
public static int getInt(String key) {
return Integer.parseInt(getP(key, "0"));
}
public static long getLong(String key, long def) {
return Long.parseLong(getP(key, Long.toString(def)));
}
public static long getLong(String key) {
return Long.parseLong(getP(key, "0L"));
}
public static double getDouble(String key, double def) {
return Double.parseDouble(getP(key, Double.toString(def)));
}
public static double getDouble(String key) {
return Double.parseDouble(getP(key, "0.0"));
}
private static String getP(String key, String def) {
return Utils.resolve(allProps.getProperty(key, def));
}
static Properties getProperties() {
return p;
}
static Properties getMergedProperties() {
return allProps;
}
private static void put(String key, String value) {
p.setProperty(key, value);
}
/**
* Called when the properties are updated.
*
*/
private static void rehashProperties() {
allProps.clear();
allProps.putAll(Config.properties());
allProps.putAll(System.getProperties());
Map<String, String> env = System.getenv();
for (String key : env.keySet()) {
String newKey = key.replace("___", "-").replace("__", ".");
allProps.put(newKey, env.get(key));
}
}
}