@@ -72,10 +72,10 @@ |
* @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>()); |
// |
@@ -123,24 +123,24 @@ |
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"; |
- |
+ |
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.176 54532d51c311 2017-04-10 od, built at 2017-04-10 15:06 by od$"); |
+ put(CSIP_VERSION, "$version: 2.1.177 af2572bb0c10 2017-04-10 od, built at 2017-04-14 13:28 by od$"); |
|
/* |
* The runtime architecture. |
@@ -311,37 +311,42 @@ |
|
// internal |
put("vm.port", "8080"); |
+ |
+ update(); |
+ } |
+ |
|
- update(); |
+ public static void register(Set<Class<?>> service) { |
+ registry().register(service); |
} |
|
/** |
* |
*/ |
public static class Registry { |
- |
+ |
String context; |
List<Class<?>> s; |
Set<Class<?>> regServ; |
- |
- |
+ |
+ |
void setContext(String context) { |
this.context = context; |
} |
- |
- |
+ |
+ |
String getContext() { |
return context; |
} |
- |
- |
+ |
+ |
public void register(Set<Class<?>> service) { |
if (regServ != null) { |
return; |
} |
regServ = service; |
s = new ArrayList<>(); |
- |
+ |
service.forEach(c -> { |
if ((c.getCanonicalName().startsWith("m.") // model service |
|| c.getCanonicalName().startsWith("d."))) { // data service |
@@ -354,8 +359,8 @@ |
-> getServiceName(o1).compareTo(getServiceName(o2))); |
LOG.info(">>>>>>>> Registered " + s.size() + " CSIP services."); |
} |
- |
- |
+ |
+ |
public void unregister() { |
if (regServ == null) { |
return; |
@@ -367,8 +372,8 @@ |
}); |
s.clear(); |
} |
- |
- |
+ |
+ |
static void callStaticMethodIfExist(Class c, String method) { |
try { |
Method m = c.getMethod(method); |
@@ -380,32 +385,32 @@ |
LOG.log(Level.SEVERE, null, ex); |
} |
} |
- |
- |
+ |
+ |
List<Class<?>> getServices() { |
return s; |
} |
- |
- |
+ |
+ |
String getServicePath(Class<?> i) { |
Path p = (Path) i.getAnnotation(Path.class); |
return (p == null) ? "" : p.value(); |
} |
- |
- |
+ |
+ |
String getServiceName(Class<?> i) { |
Name p = (Name) i.getAnnotation(Name.class); |
return (p == null) ? "" : p.value(); |
} |
- |
- |
+ |
+ |
String getServiceDescription(Class<?> i) { |
Description p = (Description) i.getAnnotation(Description.class); |
return (p == null) ? "" : p.value(); |
} |
} |
- |
- |
+ |
+ |
public static Registry registry() { |
return reg; |
} |
@@ -432,8 +437,8 @@ |
* @throws Exception |
*/ |
ModelSession getSession(String suid) throws Exception; |
- |
- |
+ |
+ |
boolean hasSession(String suid) throws Exception; |
|
|
@@ -482,25 +487,25 @@ |
* Archive Store |
*/ |
interface ArchiveStore { |
- |
+ |
static String ARCHIVE_TOO_BIG = "the archive was too big to store."; |
- |
- |
+ |
+ |
void archiveSession(String sid, ModelArchive ma, File f) throws Exception; |
- |
- |
+ |
+ |
ModelArchive getArchive(String suid) throws Exception; |
- |
- |
+ |
+ |
boolean hasArchive(String suid) throws Exception; |
- |
- |
+ |
+ |
void removeArchive(String suid); |
- |
- |
+ |
+ |
byte[] getFile(String suid, String filename) throws Exception; |
- |
- |
+ |
+ |
void shutdown() throws Exception; |
|
|
@@ -509,61 +514,61 @@ |
* @return the number of elements |
*/ |
long getCount(); |
- |
- |
+ |
+ |
Set<String> keys(int skip, int limit, String sortby, boolean sortAsc); |
- |
+ |
ArchiveStore NONE = new ArchiveStore() { |
Set<String> keys = new HashSet<>(); |
- |
- |
+ |
+ |
@Override |
public void archiveSession(String sid, ModelArchive ma, File f) throws Exception { |
} |
- |
- |
+ |
+ |
@Override |
public ModelArchive getArchive(String suid) throws Exception { |
return null; |
} |
- |
- |
+ |
+ |
@Override |
public void removeArchive(String suid) { |
} |
- |
- |
+ |
+ |
@Override |
public byte[] getFile(String suid, String filename) throws Exception { |
return null; |
} |
- |
- |
+ |
+ |
@Override |
public void shutdown() throws Exception { |
} |
- |
- |
+ |
+ |
@Override |
public Set<String> keys(int skip, int limit, String sortby, boolean sortAsc) { |
return keys; |
} |
- |
- |
+ |
+ |
@Override |
public long getCount() { |
return keys.size(); |
} |
- |
- |
+ |
+ |
@Override |
public boolean hasArchive(String suid) throws Exception { |
return false; |
} |
}; |
- |
+ |
} |
- |
+ |
interface ResultStore { |
|
/** |
@@ -612,44 +617,44 @@ |
*/ |
default void shutdown() throws Exception { |
} |
- |
+ |
ResultStore NONE = new ResultStore() { |
- |
+ |
@Override |
public String getResult(String request) { |
return null; |
} |
- |
- |
+ |
+ |
@Override |
public void putResult(String digest, String results) { |
} |
- |
- |
+ |
+ |
@Override |
public String getDigest(String request) { |
return null; |
} |
- |
- |
+ |
+ |
@Override |
public void purge() { |
} |
- |
+ |
}; |
- |
+ |
} |
|
/** |
* |
*/ |
static class MongoResultStore implements ResultStore { |
- |
+ |
MongoClient mongo; |
MongoDatabase db; |
static final String RESULTS = "results"; |
- |
- |
+ |
+ |
MongoResultStore(String uri) { |
MongoClientURI u = new MongoClientURI(uri); |
String dbname = u.getDatabase(); |
@@ -660,8 +665,8 @@ |
db = mongo.getDatabase(dbname); |
LOG.info("Connected to mongo result store : " + uri); |
} |
- |
- |
+ |
+ |
@Override |
public synchronized String getResult(String digest) { |
FindIterable<Document> r = db.getCollection(RESULTS).find(new Document("_id", digest)); |
@@ -673,29 +678,29 @@ |
return null; |
} |
} |
- |
- |
+ |
+ |
@Override |
public synchronized void putResult(String digest, String result) { |
Document doc = new Document(); |
doc.append("_id", digest); |
doc.append("result", result); |
- |
+ |
UpdateOptions opt = new UpdateOptions(); |
opt.upsert(true); |
MongoCollection<Document> c = db.getCollection(RESULTS); |
c.replaceOne(new Document("_id", digest), doc, opt); |
- |
+ |
LOG.info("put result: " + digest + " " + doc.toJson()); |
} |
- |
- |
+ |
+ |
@Override |
public synchronized void purge() { |
db.getCollection(RESULTS).drop(); |
} |
- |
- |
+ |
+ |
@Override |
public void shutdown() throws Exception { |
mongo.close(); |
@@ -706,59 +711,59 @@ |
* This is a local session store. One instance only, no failover. |
*/ |
private static class LocalStore implements SessionStore { |
- |
+ |
Map<String, ModelSession> m = Collections.synchronizedMap(new HashMap<String, ModelSession>()); |
- |
- |
+ |
+ |
public LocalStore() { |
LOG.info("Using local session store."); |
} |
- |
- |
+ |
+ |
@Override |
public void shutdown() throws Exception { |
m.clear(); |
} |
- |
- |
+ |
+ |
@Override |
public void setSession(String key, ModelSession session) throws Exception { |
m.put(key, session); |
} |
- |
- |
+ |
+ |
@Override |
public ModelSession getSession(String key) throws Exception { |
return m.get(key); |
} |
- |
- |
+ |
+ |
@Override |
public void removeSession(String key) { |
m.remove(key); |
} |
- |
- |
+ |
+ |
@Override |
public Set<String> keys(int skip, int limit, String sortby, boolean sortAscending) { |
return m.keySet(); |
} |
- |
- |
+ |
+ |
@Override |
public long getCount() { |
return m.size(); |
} |
- |
- |
+ |
+ |
@Override |
public void ping() { |
if (m == null) { |
throw new NullPointerException("Illegal hashtable store."); |
} |
} |
- |
- |
+ |
+ |
@Override |
public boolean hasSession(String suid) throws Exception { |
return m.containsKey(suid); |
@@ -769,23 +774,23 @@ |
* |
*/ |
private static class SQLSessionStore implements SessionStore { |
- |
+ |
static final String jdbc_session_id = "jdbc_session"; |
- |
+ |
SessionLogger l = new SessionLogger(null, "SQLSessionLog", ""); |
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); |
boolean isMSSQL = false; |
- |
- |
+ |
+ |
SQLSessionStore(String url) { |
Map<String, String> m = new HashMap<>(); |
m.put("defaultAutoCommit", "true"); |
Binaries.addToJDBCPool(jdbc_session_id, url, m); |
- |
+ |
createTableIfNeeded(); |
} |
- |
- |
+ |
+ |
@Override |
public synchronized void setSession(String suid, ModelSession session) throws Exception { |
try (Connection c = Binaries.getConnection(jdbc_session_id, l)) { |
@@ -823,8 +828,8 @@ |
LOG.log(Level.SEVERE, null, ex); |
} |
} |
- |
- |
+ |
+ |
@Override |
public synchronized ModelSession getSession(String suid) throws Exception { |
try (Connection c = Binaries.getConnection(jdbc_session_id, l)) { |
@@ -833,7 +838,7 @@ |
l.info("getSession() " + sql); |
ResultSet rs = st.executeQuery(sql); |
rs.next(); |
- |
+ |
ModelSession s = new ModelSession(); |
s.setTstamp(rs.getString(1)); |
s.setExpDate(rs.getString(2)); |
@@ -855,8 +860,8 @@ |
} |
return null; |
} |
- |
- |
+ |
+ |
@Override |
public synchronized boolean hasSession(String suid) throws Exception { |
try (Connection c = Binaries.getConnection(jdbc_session_id, l)) { |
@@ -872,8 +877,8 @@ |
} |
return false; |
} |
- |
- |
+ |
+ |
@Override |
public void removeSession(String suid) { |
try (Connection c = Binaries.getConnection(jdbc_session_id, l)) { |
@@ -887,14 +892,14 @@ |
LOG.log(Level.SEVERE, null, ex); |
} |
} |
- |
- |
+ |
+ |
@Override |
public void shutdown() throws Exception { |
// should shut down at the end. |
} |
- |
- |
+ |
+ |
@Override |
public synchronized Set<String> keys(int skip, int limit, String sortby, boolean sortAsc) { |
if (sortby == null) { |
@@ -919,8 +924,8 @@ |
} |
return keys; |
} |
- |
- |
+ |
+ |
@Override |
public synchronized long getCount() { |
try (Connection c = Binaries.getConnection(jdbc_session_id, l)) { |
@@ -937,13 +942,13 @@ |
} |
return -1; |
} |
- |
- |
+ |
+ |
@Override |
public void ping() throws Exception { |
} |
- |
- |
+ |
+ |
private void createTableIfNeeded() { |
try (Connection c = Binaries.getConnection(jdbc_session_id, l)) { |
String driver = c.getMetaData().getDriverName(); |
@@ -994,12 +999,12 @@ |
* |
*/ |
private static class MongoDBSessionStore implements SessionStore { |
- |
+ |
MongoClient mongo; |
MongoDatabase db; |
static final String coll = "session"; |
- |
- |
+ |
+ |
MongoDBSessionStore(String uri) { |
MongoClientURI u = new MongoClientURI(uri); |
String dbname = u.getDatabase(); |
@@ -1010,8 +1015,8 @@ |
db = mongo.getDatabase(dbname); |
LOG.info("Connected to mongo session csip store : " + uri); |
} |
- |
- |
+ |
+ |
@Override |
public synchronized void setSession(String suid, ModelSession session) throws Exception { |
Document doc = new Document(); |
@@ -1025,18 +1030,18 @@ |
.append("cpu", session.getCputime().isEmpty() ? -1 : Integer.parseInt(session.getCputime())) |
.append("pro", session.getProgress() == null ? "" |
: session.getProgress()); |
- |
+ |
String[] a = session.getAttachments(); |
doc.append("att", String.join(",", a)); |
- |
+ |
UpdateOptions opt = new UpdateOptions(); |
opt.upsert(true); |
MongoCollection<Document> c = db.getCollection(coll); |
c.replaceOne(new Document("_id", suid), doc, opt); |
LOG.info("added: " + suid + " " + doc.toJson()); |
} |
- |
- |
+ |
+ |
@Override |
public synchronized ModelSession getSession(String suid) throws Exception { |
FindIterable<Document> r = db.getCollection(coll).find(new Document("_id", suid)); |
@@ -1061,14 +1066,14 @@ |
return null; |
} |
} |
- |
- |
+ |
+ |
@Override |
public synchronized boolean hasSession(String suid) throws Exception { |
return db.getCollection(coll).count(new Document("_id", suid)) == 1; |
} |
- |
- |
+ |
+ |
@Override |
public synchronized void removeSession(String suid) { |
DeleteResult r = db.getCollection(coll).deleteOne(new Document("_id", suid)); |
@@ -1076,14 +1081,14 @@ |
throw new RuntimeException("Not deleted: " + suid); |
} |
} |
- |
- |
+ |
+ |
@Override |
public synchronized void shutdown() throws Exception { |
mongo.close(); |
} |
- |
- |
+ |
+ |
@Override |
public synchronized Set<String> keys(int skip, int limit, String sortby, boolean sortAsc) { |
if (sortby == null) { |
@@ -1098,18 +1103,18 @@ |
} |
return l; |
} |
- |
- |
+ |
+ |
@Override |
public synchronized long getCount() { |
return db.getCollection(coll).count(); |
} |
- |
- |
+ |
+ |
@Override |
public void ping() throws Exception { |
MongoCollection<Document> c = db.getCollection(coll + "_test"); |
- |
+ |
String id = "test_id_1234"; |
|
// insert document |
@@ -1143,12 +1148,12 @@ |
* |
*/ |
private static class MongoDBArchive implements ArchiveStore { |
- |
+ |
MongoClient mongo; |
String dbname; |
static final String fsColl = "fs"; |
- |
- |
+ |
+ |
MongoDBArchive(String uri) { |
// "mongodb://user:pass@host:port/db" |
MongoClientURI u = new MongoClientURI(uri); |
@@ -1159,8 +1164,8 @@ |
mongo = new MongoClient(u); |
LOG.info("Connected to archive csip store : " + uri); |
} |
- |
- |
+ |
+ |
public synchronized void removeAll() { |
DB db = mongo.getDB(dbname); |
|
@@ -1172,22 +1177,22 @@ |
GridFS fileStore = new GridFS(db, fsColl); |
fileStore.remove((DBObject) null); |
} |
- |
- |
+ |
+ |
@Override |
public synchronized void archiveSession(String sid, ModelArchive ma, File f) throws Exception { |
DB db = mongo.getDB(dbname); |
|
//Let's store the standard data in regular collection |
DBCollection collection = db.getCollection(fsColl); |
- |
+ |
String max = getString(CSIP_ARCHIVE_MAX_FILE_SIZE); |
long limit = Binaries.parseByteSize(max); |
- |
+ |
String filename = null; |
long filesize = 0; |
InputStream is = null; |
- |
+ |
if (f.length() <= limit) { |
filename = f.getName(); |
filesize = f.length(); |
@@ -1227,13 +1232,13 @@ |
LOG.warning("Unable to archive record with ID: " + sid + " as record already exists!!!"); |
} |
} |
- |
- |
+ |
+ |
@Override |
public synchronized ModelArchive getArchive(String suid) throws Exception { |
DB db = mongo.getDB(dbname); |
DBCollection collection = db.getCollection(fsColl); |
- |
+ |
BasicDBObject findQuery = new BasicDBObject("_id", suid); |
DBObject doc = collection.findOne(findQuery); |
if (doc != null) { |
@@ -1247,20 +1252,20 @@ |
} |
return null; |
} |
- |
- |
+ |
+ |
@Override |
public synchronized void removeArchive(String suid) { |
DB db = mongo.getDB(dbname); |
DBCollection collection = db.getCollection(fsColl); |
BasicDBObject q = new BasicDBObject("_id", suid); |
WriteResult res = collection.remove(q); |
- |
+ |
GridFS fileStore = new GridFS(db, fsColl); |
fileStore.remove(q); |
} |
- |
- |
+ |
+ |
@Override |
public synchronized byte[] getFile(String suid, String filename) throws Exception { |
DB db = mongo.getDB(dbname); |
@@ -1274,14 +1279,14 @@ |
} |
return null; |
} |
- |
- |
+ |
+ |
@Override |
public synchronized void shutdown() throws Exception { |
mongo.close(); |
} |
- |
- |
+ |
+ |
@Override |
public synchronized long getCount() { |
DB db = mongo.getDB(dbname); |
@@ -1297,7 +1302,7 @@ |
} |
BasicDBObject sort = new BasicDBObject(sortby, sortAsc ? 1 : -1); |
Set<String> l = new LinkedHashSet<>(); |
- |
+ |
DB db = mongo.getDB(dbname); |
DBCollection c = db.getCollection(fsColl); |
DBCursor cursor = c.find().sort(sort).skip(skip).limit(limit); |
@@ -1307,8 +1312,8 @@ |
} |
return l; |
} |
- |
- |
+ |
+ |
@Override |
public synchronized boolean hasArchive(String suid) throws Exception { |
DB db = mongo.getDB(dbname); |
@@ -1316,18 +1321,18 @@ |
return c.count(new BasicDBObject("_id", suid)) == 1; |
} |
} |
- |
- |
+ |
+ |
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; |
@@ -1355,8 +1360,8 @@ |
} |
return session; |
} |
- |
- |
+ |
+ |
static synchronized ArchiveStore getArchiveStore() { |
if (archive == null) { |
if (!isArchiveEnabled()) { |
@@ -1380,8 +1385,8 @@ |
} |
return archive; |
} |
- |
- |
+ |
+ |
static synchronized ResultStore getResultStore() { |
if (resultStore == null) { |
if (!isResultStoreEnabled()) { |
@@ -1401,24 +1406,24 @@ |
} |
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; |
} |
@@ -1443,9 +1448,9 @@ |
for (ModelDataService.Task t : tasks) { |
t.cancel(); |
} |
- |
+ |
reg.unregister(); |
- |
+ |
if (exec != null) { |
LOG.info("Shutting down ExecutorService"); |
exec.shutdownNow(); |
@@ -1458,7 +1463,7 @@ |
} |
session = null; |
} |
- |
+ |
if (archive != null) { |
try { |
archive.shutdown(); |
@@ -1467,7 +1472,7 @@ |
} |
archive = null; |
} |
- |
+ |
if (resultStore != null) { |
try { |
resultStore.shutdown(); |
@@ -1479,7 +1484,7 @@ |
if (timer != null) { |
timer.cancel(); |
} |
- |
+ |
Binaries.shutdownJDBC(); |
} |
|
@@ -1490,99 +1495,99 @@ |
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 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); |
} |
- |
- |
+ |
+ |
public static void main(String[] args) { |
// System.out.println(System.getenv()); |
String a = resolve("${test.port} abc ${java.home} ///${PATH}/// def ${csip.bin.dir}"); |
System.out.println(a); |
- |
+ |
String b = "d".replace("___", "-").replace("__", "."); |
System.out.println(b); |
} |
@@ -1617,7 +1622,7 @@ |
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("__", "."); |
@@ -1658,8 +1663,8 @@ |
} |
return str; |
} |
- |
- |
+ |
+ |
static void checkRemoteAccessACL(HttpServletRequest req) { |
String reqIp = req.getHeader("X-Forwarded-For"); |
if (reqIp == null) { |
@@ -1671,8 +1676,8 @@ |
} |
LOG.log(Level.INFO, req.getMethod() + " " + req.getRequestURI() + ", OK for " + reqIp); |
} |
- |
- |
+ |
+ |
static boolean checkRemoteAccessACL(String ip) { |
// default is "localhost" only. |
String acls = Config.getString(CSIP_REMOTE_ACL, "127.0.0.1/32"); |
@@ -1690,8 +1695,8 @@ |
} |
return false; |
} |
- |
- |
+ |
+ |
static boolean isInSubnet(String ip, String subnet) { |
try { |
SubnetUtils utils = new SubnetUtils(subnet); |
@@ -1702,68 +1707,68 @@ |
return false; |
} |
} |
- |
+ |
public static class PostgresChunk { |
- |
+ |
String name; |
double minLong; |
double maxLong; |
Connection[] connections; |
int currentConnection; |
- |
- |
+ |
+ |
public PostgresChunk(String name, double minLong, double maxLong) { |
this.name = name; |
this.minLong = minLong; |
this.maxLong = maxLong; |
} |
- |
- |
+ |
+ |
public boolean equals(Object obj) { |
return this.name.equals(((PostgresChunk) obj).getName()); |
} |
- |
- |
+ |
+ |
public String getName() { |
return name; |
} |
- |
- |
+ |
+ |
public double getMinLong() { |
return minLong; |
} |
- |
- |
+ |
+ |
public double getMaxLong() { |
return maxLong; |
} |
- |
- |
+ |
+ |
public void setConnections(Connection[] connections) { |
this.connections = connections; |
} |
- |
- |
+ |
+ |
public Connection[] getConnections() { |
return connections; |
} |
- |
- |
+ |
+ |
public void connectionInc() { |
currentConnection++; |
} |
- |
- |
+ |
+ |
public int getCurrentConnectionIdx() { |
return currentConnection; |
} |
- |
- |
+ |
+ |
public void setCurrentConnectionIdx(int currentConnection) { |
this.currentConnection = currentConnection; |
} |
- |
- |
+ |
+ |
public Connection getCurrentConnection() { |
if ((connections != null) && (connections.length >= currentConnection)) { |
return connections[currentConnection]; |
@@ -1771,8 +1776,8 @@ |
return null; |
} |
} |
- |
- |
+ |
+ |
public JSONObject getJSON() { |
try { |
JSONObject pgchunk = new JSONObject(); |
@@ -11,22 +11,12 @@ |
*/ |
package csip; |
|
-import csip.utils.Services; |
import java.io.File; |
-import java.io.FileInputStream; |
-import java.io.FileNotFoundException; |
-import java.io.IOException; |
-import java.io.InputStream; |
-import java.util.Enumeration; |
-import java.util.Properties; |
import java.util.logging.Logger; |
import javax.servlet.ServletContext; |
import javax.servlet.ServletContextEvent; |
import javax.servlet.ServletContextListener; |
import javax.servlet.annotation.WebListener; |
-import org.apache.commons.io.FileUtils; |
-import org.apache.commons.io.IOUtils; |
-import org.codehaus.jettison.json.JSONException; |
|
/** |
* Context Registration. |
@@ -39,83 +29,32 @@ |
static Logger l = Logger.getLogger(ServletContextHooks.class.getName()); |
|
|
- /** |
- * Apply an initial configuration if present. |
- * |
- * @param ctx |
- */ |
- private void applyConfig(ServletContext ctx, String file) { |
- InputStream is = ctx.getResourceAsStream(file); |
- if (is != null) { |
- try { |
- l.info("Applied bundled context config '" + file + "'"); |
- if (file.endsWith(".json")) { |
- ControlService.updateConfig(IOUtils.toString(is)); |
- } else if (file.endsWith(".properties")) { |
- Properties p = new Properties(); |
- p.load(is); |
- ControlService.updateConfig(p); |
- } |
- } catch (IOException | JSONException ex) { |
- ex.printStackTrace(System.err); |
- } |
- try { |
- is.close(); |
- } catch (IOException ex) { |
- } |
- } |
- } |
- |
- |
- private void applyContextInitParams(ServletContext ctx) { |
- Enumeration<String> params = ctx.getInitParameterNames(); |
- Properties p = new Properties(); |
- while (params.hasMoreElements()) { |
- String key = (String) params.nextElement(); |
- String value = ctx.getInitParameter(key); |
- p.setProperty(key, value); |
- } |
- if (!p.isEmpty()) { |
- l.info("Applied servlet context parameter'" + p.toString() + "'"); |
- ControlService.updateConfig(p); |
- } |
- } |
- |
- |
@Override |
public void contextInitialized(ServletContextEvent e) { |
ServletContext ctx = e.getServletContext(); |
Config.startup(ctx); |
- applyConfig(ctx, "/WEB-INF/csip-defaults.json"); |
- applyConfig(ctx, "/WEB-INF/csip-defaults.properties"); |
- applyConfig(ctx, "/META-INF/csip-conf.json"); // deprecated |
|
- applyContextInitParams(ctx); |
+ ContextConfig cc = new ContextConfig(); |
|
- File c = Services.getContextFile(ctx, ".json"); |
- if (c != null) { |
- try { |
- ControlService.updateConfig(FileUtils.readFileToString(c)); |
- l.info("Applied config '" + c + "'"); |
- } catch (FileNotFoundException ex) { |
- // ignore |
- } catch (JSONException | IOException ex) { |
- ex.printStackTrace(System.err); |
- } |
+ // Context internal settings. |
+ cc.load(ctx, "/WEB-INF/csip-defaults.json"); |
+ cc.load(ctx, "/META-INF/csip-conf.json"); // deprecated |
+ |
+ // Servlet init params. |
+ cc.load(ctx); |
+ |
+ // Tomcat/conf folder as <context>.yaml |
+ cc.load(new File(System.getProperty("catalina.home") + File.separatorChar + "conf" + ctx.getContextPath() + ".yaml")); |
+ |
+ // webapp folder |
+ cc.load(ctx, ".json"); |
+ cc.load(ctx, ".properties"); |
+ cc.load(ctx, ".yaml"); |
+ |
+ if (!cc.getConfig().isEmpty()) { |
+ ControlService.updateConfig(cc.getConfig()); |
} |
- c = Services.getContextFile(ctx, ".properties"); |
- if (c != null) { |
- try { |
- Properties p = new Properties(); |
- p.load(new FileInputStream(c)); |
- ControlService.updateConfig(p); |
- l.info("Applied config '" + c + "'"); |
- } catch (FileNotFoundException ex) { |
- // ignore |
- } catch (IOException ex) { |
- ex.printStackTrace(System.err); |
- } |
- } |
+ |
l.info("CSIP Context Created: " + ctx.getContextPath()); |
} |
|