String dbName = getProperty(properties, "dbName", "test");
String username = getProperty(properties, "username", null);
String password = getProperty(properties, "password", null);
try {
Mongo mongo;
if (mongoURI != null) {
MongoURI uri = new MongoURI(mongoURI);
mongo = new Mongo(uri);
}
else {
MongoOptions mongoOptions = createMongoOptions(properties);
mongo = createMongo(host, port, mongoOptions);
}
mongo.setWriteConcern(WriteConcern.valueOf(writeConcern));
if (readPreference.equals("PrimaryReadPreference")) {
mongo.setReadPreference(ReadPreference.PRIMARY);
}
else if (readPreference.equals("PrimaryReadPreference")) {
mongo.setReadPreference(ReadPreference.SECONDARY);
} else {
m_logService.log(LogService.LOG_ERROR, "ReadPreference '" + readPreference + "' is not supported and was ignored." );
}
if(username != null) {
mongo.getDB(dbName).authenticate(username, password.toCharArray());
m_logService.log(LogService.LOG_INFO, "Authenticated as '" + username + "'");
}
MongoDBServiceImpl instance = new MongoDBServiceImpl(mongo, mongo.getDB(dbName));
Properties serviceProperties = new Properties();
serviceProperties.put("dbName", dbName);
Component component = m_dependencyManager.createComponent().setInterface(MongoDBService.class.getName(), serviceProperties).setImplementation(instance);
m_dependencyManager.add(component);