Backends.java [src/csip] Revision: Date:
/*
* $Id$
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* a Model-as-a-Service framework, API and application suite.
*
* 2012-2022, 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 static csip.Config.CSIP_ACCESSLOG_BACKEND;
import static csip.Config.CSIP_ACCESSLOG_MONGODB_COL;
import static csip.Config.CSIP_ACCESSLOG_MONGODB_URI;
import static csip.Config.CSIP_ARCHIVE_BACKEND;
import static csip.Config.CSIP_ARCHIVE_MONGODB_URI;
import static csip.Config.CSIP_PUBLISHER_BACKEND;
import static csip.Config.CSIP_RESULTSTORE_BACKEND;
import static csip.Config.CSIP_RESULTSTORE_LIMIT;
import static csip.Config.CSIP_RESULTSTORE_MONGODB_URI;
import static csip.Config.CSIP_SESSION_BACKEND;
import static csip.Config.CSIP_SESSION_MONGODB_URI;
import static csip.Config.CSIP_TOKEN_AUTHENTICATION;
import static csip.Config.LOCAL;
import static csip.Config.MONGODB;
import static csip.Config.NONE;
import static csip.Config.SQL;
import static csip.Config.WEBHOOK;
import static csip.Config.getInt;
import csip.utils.SimpleCache;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author od
*/
class Backends {
SimpleCache<String, AutoCloseable> backends = new SimpleCache<>();
Logger log;
Backends(Logger log) {
this.log = log;
}
synchronized SessionStore getSessionStore() {
return (SessionStore) backends.get(CSIP_SESSION_BACKEND, key -> {
switch (Config.getString(key)) {
case LOCAL:
return new LocalSessionStore();
case MONGODB:
String uri = Config.getString(CSIP_SESSION_MONGODB_URI);
if (uri == null)
throw new RuntimeException("missing uri configuration entry 'csip.session.mongodb.uri'");
return new MongoSessionStore(uri);
case SQL:
String uri1 = Config.getString("csip.session.sql.uri");
if (uri1 == null)
throw new RuntimeException("missing uri configuration entry 'csip.session.sql.uri'");
return new SQLSessionStore(uri1);
default:
throw new RuntimeException("unknown session backend: " + Config.getString(key));
}
});
}
synchronized ArchiveStore getArchiveStore() {
return (ArchiveStore) backends.get(CSIP_ARCHIVE_BACKEND, key -> {
switch (Config.getString(key)) {
case NONE:
return ArchiveStore.NONE;
case MONGODB:
String uri = Config.getString(CSIP_ARCHIVE_MONGODB_URI);
if (uri == null)
throw new RuntimeException("missing uri configuration entry 'csip.archive.mongodb.uri'");
return new MongoArchiveStore1(uri);
default:
throw new RuntimeException("unknown archive backend: " + Config.getString(key));
}
});
}
synchronized AccessLog getAccessLog() {
return (AccessLog) backends.get(CSIP_ACCESSLOG_BACKEND, key -> {
switch (Config.getString(key)) {
case NONE:
return AccessLog.NONE;
case MONGODB:
String uri = Config.getString(CSIP_ACCESSLOG_MONGODB_URI);
if (uri == null)
throw new RuntimeException("missing configuration entry 'csip.accesslog.mongodb.uri'");
String col = Config.getString(CSIP_ACCESSLOG_MONGODB_COL);
if (col == null)
throw new RuntimeException("missing configuration entry 'csip.accesslog.mongodb.col'");
return new MongoAccessLog(log, uri, col);
case "posthog":
String host = Config.getString("csip.accesslog.posthog.uri");
if (host == null)
throw new RuntimeException("missing configuration entry 'csip.accesslog.posthog.uri'");
String apikey = Config.getString("csip.accesslog.posthog.apikey");
if (apikey == null)
throw new RuntimeException("missing configuration entry 'csip.accesslog.posthog.apikey'");
return new PostHogAccessLog(log, host, apikey);
default:
throw new RuntimeException("unknown access log backend: " + Config.getString(key));
}
});
}
synchronized Publisher getPublisher() {
return (Publisher) backends.get(CSIP_PUBLISHER_BACKEND, key -> {
switch (Config.getString(key)) {
case NONE:
return Publisher.NONE;
case WEBHOOK:
return new WebhookPublisher(log);
default:
throw new RuntimeException("unknown publisher backend: " + Config.getString(key));
}
});
}
synchronized ResultStore getResultStore() {
return (ResultStore) backends.get(CSIP_RESULTSTORE_BACKEND, key -> {
switch (Config.getString(key)) {
case NONE:
return ResultStore.NONE;
case MONGODB:
return new MongoResultStore(Config.getString(CSIP_RESULTSTORE_MONGODB_URI));
case LOCAL:
return new MemResultStore(getInt(CSIP_RESULTSTORE_LIMIT, 64));
default:
throw new RuntimeException("unknown resultstore backend: " + Config.getString(key));
}
});
}
synchronized TokenAuthentication getTokenAuthentication() {
return (TokenAuthentication) backends.get(CSIP_TOKEN_AUTHENTICATION, key -> {
switch (Config.getString(key)) {
case NONE:
return TokenAuthentication.NONE;
case "property":
return new PropertyTokenAuthentication(Config.getString("csip.auth.tokens"));
case "jwt":
return new JWTAuthentication(Config.getString("csip.jwk.provider.url"));
default:
throw new RuntimeException("Unknown Authentication backend: " + Config.getString(key));
}
});
}
void closeAll() {
backends.forEach((name, backend) -> {
try {
log.info("Shutting down backend '" + name + "'");
backend.close();
} catch (Exception ex) {
log.log(Level.SEVERE, null, ex);
}
});
}
}