mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-11-06 02:52:22 +00:00
introduced new property mongo.uri which allows users to provide a full-fledged, replica set-aware mongodb:// URI
Change-Id: Ie02631b2217333629e5aa7ca16346734a06819cb
This commit is contained in:
1 parent
51508b660e
commit
867cd6c84e
7 files changed
+122
-78
No files matched your search
+2
-2
@@ -14,8 +14,8 @@ import com.sap.sse.mongodb.MongoDBConfiguration;
|
||||
import com.sap.sse.mongodb.MongoDBService;
|
||||
|
||||
public abstract class AbstractMongoDBTest {
|
||||
protected Mongo mongo;
|
||||
protected DB db;
|
||||
protected final Mongo mongo;
|
||||
protected final DB db;
|
||||
private final MongoDBConfiguration dbConfiguration;
|
||||
private MongoDBService service;
|
||||
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@ public abstract class AbstractMongoDBTest {
|
||||
}
|
||||
|
||||
protected Mongo newMongo() throws UnknownHostException, MongoException {
|
||||
return new MongoClient(dbConfiguration.getHostName(), dbConfiguration.getPort());
|
||||
return new MongoClient(dbConfiguration.getMongoClientURI());
|
||||
}
|
||||
|
||||
@Before
|
||||
|
||||
+3
-4
@@ -66,16 +66,15 @@ public class TestStoringAndRetrievingWindTracksTest extends AbstractTracTracLive
|
||||
}
|
||||
|
||||
private Mongo newMongo() throws UnknownHostException, MongoException {
|
||||
return new MongoClient(dbConfiguration.getHostName(),
|
||||
dbConfiguration.getPort());
|
||||
return new MongoClient(dbConfiguration.getMongoClientURI());
|
||||
}
|
||||
|
||||
@Before
|
||||
public void dropTestDB() throws UnknownHostException, MongoException {
|
||||
mongo = newMongo();
|
||||
assertNotNull(mongo);
|
||||
mongo.dropDatabase(dbConfiguration.getDatabaseName());
|
||||
db = mongo.getDB(dbConfiguration.getDatabaseName());
|
||||
mongo.dropDatabase(dbConfiguration.getMongoClientURI().getDatabase());
|
||||
db = mongo.getDB(dbConfiguration.getMongoClientURI().getDatabase());
|
||||
assertNotNull(db);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,8 @@ Require-Bundle: javax.servlet;bundle-version="3.1.0",
|
||||
com.sap.sailing.server;bundle-version="1.0.0",
|
||||
com.sap.sse.mongodb;bundle-version="1.0.0",
|
||||
com.sap.sse.common,
|
||||
com.sap.sse
|
||||
com.sap.sse,
|
||||
org.mongodb.mongo-java-driver;bundle-version="2.13.0"
|
||||
Bundle-ClassPath: .
|
||||
Web-ContextPath: /sailingserver/test-support
|
||||
Import-Package: org.osgi.framework,
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.util.logging.Logger;
|
||||
import org.osgi.framework.BundleActivator;
|
||||
import org.osgi.framework.BundleContext;
|
||||
|
||||
import com.mongodb.MongoClientURI;
|
||||
import com.sap.sse.mongodb.internal.MongoDBServiceImpl;
|
||||
|
||||
public class Activator implements BundleActivator {
|
||||
@@ -13,30 +14,43 @@ public class Activator implements BundleActivator {
|
||||
|
||||
@Override
|
||||
public void start(BundleContext context) throws Exception {
|
||||
|
||||
MongoDBConfiguration defaultConfiguration = MongoDBConfiguration.getDefaultConfiguration();
|
||||
|
||||
String databaseName = context.getProperty(MongoDBConfiguration.MONGO_DB_NAME);
|
||||
if (databaseName == null) {
|
||||
databaseName = defaultConfiguration.getDatabaseName();
|
||||
MongoDBConfiguration defaultConfiguration = MongoDBConfiguration.getDefaultConfiguration();
|
||||
final MongoDBConfiguration newConfiguration;
|
||||
final String mongoClientURI = context.getProperty(MongoDBConfiguration.MONGO_URI);
|
||||
if (mongoClientURI != null) {
|
||||
logger.info("found "+MongoDBConfiguration.MONGO_URI+"="+mongoClientURI);
|
||||
newConfiguration = new MongoDBConfiguration(new MongoClientURI(
|
||||
context.getProperty(MongoDBConfiguration.MONGO_URI), MongoDBConfiguration.getDefaultOptionsBuilder()));
|
||||
} else {
|
||||
logger.log(Level.INFO, "found "+MongoDBConfiguration.MONGO_DB_NAME+"="+databaseName);
|
||||
final String databaseNameProperty = context.getProperty(MongoDBConfiguration.MONGO_DB_NAME);
|
||||
final String databaseName;
|
||||
if (databaseNameProperty == null) {
|
||||
databaseName = defaultConfiguration.getMongoClientURI().getDatabase();
|
||||
} else {
|
||||
databaseName = databaseNameProperty;
|
||||
logger.log(Level.INFO, "found "+MongoDBConfiguration.MONGO_DB_NAME+"="+databaseName);
|
||||
}
|
||||
final String hostNameFromProperty = context.getProperty(MongoDBConfiguration.MONGO_HOSTNAME);
|
||||
final String hostName;
|
||||
final String[] primaryHostAndPort = defaultConfiguration.getMongoClientURI().getHosts().get(0).split(":");
|
||||
if (hostNameFromProperty != null) {
|
||||
logger.log(Level.INFO, "found "+MongoDBConfiguration.MONGO_HOSTNAME+"="+hostNameFromProperty);
|
||||
hostName = hostNameFromProperty;
|
||||
} else {
|
||||
hostName = defaultConfiguration.getMongoClientURI().getHosts().get(0).split(":")[0];
|
||||
}
|
||||
if (context.getProperty(MongoDBConfiguration.MONGO_PORT) == null) {
|
||||
if (primaryHostAndPort.length > 1) {
|
||||
newConfiguration = new MongoDBConfiguration(hostName, Integer.valueOf(primaryHostAndPort[1]), databaseName);
|
||||
} else {
|
||||
newConfiguration = new MongoDBConfiguration(hostName, databaseName);
|
||||
}
|
||||
} else {
|
||||
final String portAsString = context.getProperty(MongoDBConfiguration.MONGO_PORT);
|
||||
logger.log(Level.INFO, "found "+MongoDBConfiguration.MONGO_PORT+"="+portAsString);
|
||||
newConfiguration = new MongoDBConfiguration(hostName, Integer.valueOf(portAsString), databaseName);
|
||||
}
|
||||
}
|
||||
String hostName = context.getProperty(MongoDBConfiguration.MONGO_HOSTNAME);
|
||||
if (hostName == null) {
|
||||
hostName = defaultConfiguration.getHostName();
|
||||
} else {
|
||||
logger.log(Level.INFO, "found "+MongoDBConfiguration.MONGO_HOSTNAME+"="+hostName);
|
||||
}
|
||||
int port;
|
||||
if(context.getProperty(MongoDBConfiguration.MONGO_PORT) == null) {
|
||||
port = defaultConfiguration.getPort();
|
||||
} else {
|
||||
port = Integer.valueOf(context.getProperty(MongoDBConfiguration.MONGO_PORT));
|
||||
logger.log(Level.INFO, "found "+MongoDBConfiguration.MONGO_PORT+"="+port);
|
||||
}
|
||||
|
||||
MongoDBConfiguration newConfiguration = new MongoDBConfiguration(hostName, port, databaseName);
|
||||
((MongoDBServiceImpl) MongoDBService.INSTANCE).setConfiguration(newConfiguration);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package com.sap.sse.mongodb;
|
||||
|
||||
import com.mongodb.MongoClientOptions;
|
||||
import com.mongodb.MongoClientOptions.Builder;
|
||||
import com.mongodb.MongoClientURI;
|
||||
import com.mongodb.ReadPreference;
|
||||
import com.mongodb.ServerAddress;
|
||||
import com.sap.sse.mongodb.internal.MongoDBServiceImpl;
|
||||
|
||||
/**
|
||||
@@ -9,38 +14,49 @@ import com.sap.sse.mongodb.internal.MongoDBServiceImpl;
|
||||
*/
|
||||
public class MongoDBConfiguration {
|
||||
|
||||
public static final String MONGO_PORT = "mongo.port";
|
||||
public static final String MONGO_HOSTNAME = "mongo.host";
|
||||
public static final String MONGO_DB_NAME = "mongo.dbName";
|
||||
|
||||
static final String MONGO_PORT = "mongo.port";
|
||||
static final String MONGO_HOSTNAME = "mongo.host";
|
||||
static final String MONGO_DB_NAME = "mongo.dbName";
|
||||
|
||||
/**
|
||||
* The system property by this name may contain a MongoClientURI in its string representation:
|
||||
* <pre>
|
||||
* mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database[.collection]][?options]]
|
||||
* </pre>
|
||||
* See also {@link MongoClientURI}.
|
||||
*/
|
||||
static final String MONGO_URI = "mongo.uri";
|
||||
|
||||
private static String DEFAULT_DB_NAME = "winddb";
|
||||
private static String DEFAULT_TEST_DB_NAME = "winddbTest";
|
||||
|
||||
private String hostName;
|
||||
private int port;
|
||||
private String databaseName;
|
||||
private final MongoClientURI mongoClientURI;
|
||||
|
||||
public static MongoDBConfiguration getDefaultConfiguration() {
|
||||
String defaultHostName = "127.0.0.1";
|
||||
if (System.getProperty(MONGO_HOSTNAME) != null) {
|
||||
defaultHostName = System.getProperty(MONGO_HOSTNAME);
|
||||
final MongoDBConfiguration result;
|
||||
if (System.getProperty(MONGO_URI) != null) {
|
||||
result = new MongoDBConfiguration(new MongoClientURI(System.getProperty(MONGO_URI)));
|
||||
} else {
|
||||
if (System.getenv("MONGODB_HOST") != null) {
|
||||
defaultHostName = System.getenv("MONGODB_HOST");
|
||||
String defaultHostName = "127.0.0.1";
|
||||
if (System.getProperty(MONGO_HOSTNAME) != null) {
|
||||
defaultHostName = System.getProperty(MONGO_HOSTNAME);
|
||||
} else {
|
||||
if (System.getenv("MONGODB_HOST") != null) {
|
||||
defaultHostName = System.getenv("MONGODB_HOST");
|
||||
}
|
||||
}
|
||||
String defaultDatabaseName = System.getProperty(MONGO_DB_NAME, DEFAULT_DB_NAME);
|
||||
if (System.getProperty(MONGO_PORT) != null) {
|
||||
result = new MongoDBConfiguration(defaultHostName, Integer.valueOf(System.getProperty(MONGO_PORT).trim()), defaultDatabaseName);
|
||||
} else {
|
||||
if (System.getenv("MONGODB_PORT") != null) {
|
||||
result = new MongoDBConfiguration(defaultHostName, Integer.valueOf(System.getenv("MONGODB_PORT").trim()), defaultDatabaseName);
|
||||
} else {
|
||||
result = new MongoDBConfiguration(defaultHostName, defaultDatabaseName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int defaultPort = 27017;
|
||||
if (System.getProperty(MONGO_PORT) != null) {
|
||||
defaultPort = Integer.valueOf(System.getProperty(MONGO_PORT).trim());
|
||||
} else {
|
||||
if (System.getenv("MONGODB_PORT") != null) {
|
||||
defaultPort = Integer.valueOf(System.getenv("MONGODB_PORT").trim());
|
||||
}
|
||||
}
|
||||
String defaultDatabaseName = System.getProperty(MONGO_DB_NAME, DEFAULT_DB_NAME);
|
||||
|
||||
return new MongoDBConfiguration(defaultHostName, defaultPort, defaultDatabaseName);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static MongoDBConfiguration getDefaultTestConfiguration() {
|
||||
@@ -52,24 +68,30 @@ public class MongoDBConfiguration {
|
||||
defaultHostName = System.getenv("MONGODB_HOST");
|
||||
}
|
||||
}
|
||||
|
||||
int defaultPort = 27017;
|
||||
String defaultDatabaseName = System.getProperty(MONGO_DB_NAME, DEFAULT_TEST_DB_NAME);
|
||||
final MongoDBConfiguration result;
|
||||
if (System.getProperty(MONGO_PORT) != null) {
|
||||
defaultPort = Integer.valueOf(System.getProperty(MONGO_PORT).trim());
|
||||
result = new MongoDBConfiguration(defaultHostName, Integer.valueOf(System.getProperty(MONGO_PORT).trim()), defaultDatabaseName);
|
||||
} else {
|
||||
if (System.getenv("MONGODB_PORT") != null) {
|
||||
defaultPort = Integer.valueOf(System.getenv("MONGODB_PORT").trim());
|
||||
result = new MongoDBConfiguration(defaultHostName, Integer.valueOf(System.getenv("MONGODB_PORT").trim()), defaultDatabaseName);
|
||||
} else {
|
||||
result = new MongoDBConfiguration(defaultHostName, defaultDatabaseName);
|
||||
}
|
||||
}
|
||||
String defaultDatabaseName = System.getProperty(MONGO_DB_NAME, DEFAULT_TEST_DB_NAME);
|
||||
|
||||
return new MongoDBConfiguration(defaultHostName, defaultPort, defaultDatabaseName);
|
||||
return result;
|
||||
}
|
||||
|
||||
public MongoDBConfiguration(MongoClientURI mongoClientURI) {
|
||||
this.mongoClientURI = mongoClientURI;
|
||||
}
|
||||
|
||||
public MongoDBConfiguration(String hostName, String databaseName) {
|
||||
this.mongoClientURI = new MongoClientURI("mongodb://"+hostName+"/"+databaseName);
|
||||
}
|
||||
|
||||
public MongoDBConfiguration(String hostName, int port, String databaseName) {
|
||||
this.hostName = hostName;
|
||||
this.port = port;
|
||||
this.databaseName = databaseName;
|
||||
this.mongoClientURI = new MongoClientURI("mongodb://"+hostName+":"+port+"/"+databaseName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -79,16 +101,23 @@ public class MongoDBConfiguration {
|
||||
return new MongoDBServiceImpl(this);
|
||||
}
|
||||
|
||||
public String getHostName() {
|
||||
return hostName;
|
||||
public MongoClientURI getMongoClientURI() {
|
||||
return mongoClientURI;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
public static MongoClientOptions.Builder getDefaultOptionsBuilder() {
|
||||
return new Builder().readPreference(ReadPreference.primaryPreferred());
|
||||
}
|
||||
|
||||
public String getDatabaseName() {
|
||||
return databaseName;
|
||||
return getMongoClientURI().getDatabase();
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
int result = ServerAddress.defaultPort();
|
||||
if (!getMongoClientURI().getHosts().isEmpty() && getMongoClientURI().getHosts().get(0).split(":").length > 1) {
|
||||
result = Integer.valueOf(getMongoClientURI().getHosts().get(0).split(":")[1]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import java.util.logging.Logger;
|
||||
import com.mongodb.DB;
|
||||
import com.mongodb.Mongo;
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.MongoClientURI;
|
||||
import com.sap.sse.mongodb.AlreadyRegisteredException;
|
||||
import com.sap.sse.mongodb.MongoDBConfiguration;
|
||||
import com.sap.sse.mongodb.MongoDBService;
|
||||
@@ -19,9 +20,9 @@ public class MongoDBServiceImpl implements MongoDBService {
|
||||
|
||||
private MongoDBConfiguration configuration;
|
||||
|
||||
private final Map<com.sap.sse.common.Util.Pair<String, Integer>, Mongo> mongos;
|
||||
private final Map<MongoClientURI, Mongo> mongos;
|
||||
|
||||
private final Map<com.sap.sse.common.Util.Pair<String, Integer>, DB> dbs;
|
||||
private final Map<MongoClientURI, DB> dbs;
|
||||
|
||||
/**
|
||||
* collection name -> fully qualified class name
|
||||
@@ -29,8 +30,8 @@ public class MongoDBServiceImpl implements MongoDBService {
|
||||
private final Map<String, String> registered;
|
||||
|
||||
public MongoDBServiceImpl() {
|
||||
mongos = new HashMap<com.sap.sse.common.Util.Pair<String, Integer>, Mongo>();
|
||||
dbs = new HashMap<com.sap.sse.common.Util.Pair<String,Integer>, DB>();
|
||||
mongos = new HashMap<>();
|
||||
dbs = new HashMap<>();
|
||||
registered = new HashMap<String, String>();
|
||||
}
|
||||
|
||||
@@ -45,13 +46,13 @@ public class MongoDBServiceImpl implements MongoDBService {
|
||||
|
||||
public void setConfiguration(MongoDBConfiguration configuration) {
|
||||
this.configuration = configuration;
|
||||
logger.info("Used Mongo configuration: host:port/DBName: "+configuration.getHostName()+":"+configuration.getPort()+"/"+configuration.getDatabaseName());
|
||||
logger.info("Used Mongo configuration: "+configuration.getMongoClientURI());
|
||||
}
|
||||
|
||||
public DB getDB() {
|
||||
if (configuration == null) {
|
||||
configuration = MongoDBConfiguration.getDefaultTestConfiguration();
|
||||
logger.info("Used default Mongo configuration: host:port/DBName: "+configuration.getHostName()+":"+configuration.getPort()+"/"+configuration.getDatabaseName());
|
||||
logger.info("Used default Mongo configuration: "+configuration.getMongoClientURI());
|
||||
}
|
||||
try {
|
||||
return getDB(configuration);
|
||||
@@ -61,15 +62,15 @@ public class MongoDBServiceImpl implements MongoDBService {
|
||||
}
|
||||
|
||||
private synchronized DB getDB(MongoDBConfiguration mongoDBConfiguration) throws UnknownHostException {
|
||||
com.sap.sse.common.Util.Pair<String, Integer> key = new com.sap.sse.common.Util.Pair<String, Integer>(mongoDBConfiguration.getHostName(), mongoDBConfiguration.getPort());
|
||||
final MongoClientURI key = mongoDBConfiguration.getMongoClientURI();
|
||||
DB db = dbs.get(key);
|
||||
if (db == null) {
|
||||
Mongo mongo = mongos.get(key);
|
||||
if (mongo == null) {
|
||||
mongo = new MongoClient(mongoDBConfiguration.getHostName(), mongoDBConfiguration.getPort());
|
||||
mongo = new MongoClient(mongoDBConfiguration.getMongoClientURI());
|
||||
mongos.put(key, mongo);
|
||||
}
|
||||
db = mongo.getDB(mongoDBConfiguration.getDatabaseName());
|
||||
db = mongo.getDB(mongoDBConfiguration.getMongoClientURI().getDatabase());
|
||||
dbs.put(key, db);
|
||||
}
|
||||
return db;
|
||||
|
||||
Reference in new issue
Block a user