return AdvancedDatastore.class;
}
@Override
protected AdvancedDatastore createInstance() throws Exception {
AdvancedDatastore datastore;
if (StringUtils.hasText(user)) {
// If there is a username supplied, we need to authenticate. Morphia does not
// allow us to use a MongoDB admin user so we need to do some setup work prior
// to involving Morphia. To do this, we will first try to authenticate as a
// database user and if that fails, we will try to authenticate as an admin
// user. If both fail, we let the MongoException bubble up as it is unhandleable.
logger.debug("Create the morphia datastore with credentials");
// Try to login as a database user first and fall back to admin user if login fails
if (mongo.getDB(dbName).authenticate(user, password.toCharArray())) {
logger.debug("Successfully authenticated to MongoDB database (" + dbName +
") as " + user);
} else {
if (mongo.getDB("admin").authenticate(user, password.toCharArray())) {
logger.debug("Successfully authenticated to MongoDB database (" + dbName +
") as admin " + user);
} else {
throw new MongoException("Unable to authenticate to MongoDB using " +
user + "@" + dbName + " or " + user + "@admin.");
}
}
} else {
logger.debug("Create the morphia datastore WITHOUT credentials");
}
datastore = (AdvancedDatastore) morphia.createDatastore(mongo, dbName);
datastore.ensureCaps();
datastore.ensureIndexes();
return datastore;
}