Support for authentication via MongoTemplate and a new execute method that doesn't start and finish a request

This commit is contained in:
Graeme Rocher
2010-11-11 12:25:51 +01:00
parent 433900cd9f
commit 532e4a4806
3 changed files with 83 additions and 5 deletions

View File

@@ -23,4 +23,8 @@ public class CannotGetMongoDbConnectionException extends DataAccessResourceFailu
super(msg, cause); super(msg, cause);
} }
public CannotGetMongoDbConnectionException(String msg) {
super(msg);
}
} }

View File

@@ -75,11 +75,32 @@ public class MongoDbUtils {
return null; return null;
} }
/**
* Obtains a {@link DB} connection for the given {@link Mongo} instance and database name
*
* @param mongo The {@link Mongo} instance
* @param databaseName The database name
* @return The {@link DB} connection
*/
public static DB getDB(Mongo mongo, String databaseName) { public static DB getDB(Mongo mongo, String databaseName) {
return doGetDB(mongo, databaseName, true); return doGetDB(mongo, databaseName,null,null, true);
} }
public static DB doGetDB(Mongo mongo, String databaseName, boolean allowCreate) { /**
*
* Obtains a {@link DB} connection for the given {@link Mongo} instance and database name
*
* @param mongo The {@link Mongo} instance
* @param databaseName The database name
* @param username The username to authenticate with
* @param password The password to authenticate with
* @return The {@link DB} connection
*/
public static DB getDB(Mongo mongo, String databaseName, String username, char[] password) {
return doGetDB(mongo, databaseName,username,password, true);
}
public static DB doGetDB(Mongo mongo, String databaseName, String username, char[] password, boolean allowCreate) {
Assert.notNull(mongo, "No Mongo instance specified"); Assert.notNull(mongo, "No Mongo instance specified");
DBHolder dbHolder = (DBHolder) TransactionSynchronizationManager.getResource(mongo); DBHolder dbHolder = (DBHolder) TransactionSynchronizationManager.getResource(mongo);
@@ -103,6 +124,11 @@ public class MongoDbUtils {
logger.debug("Opening Mongo DB"); logger.debug("Opening Mongo DB");
DB db = mongo.getDB(databaseName); DB db = mongo.getDB(databaseName);
if(username != null && password != null) {
if(!db.authenticate(username, password)) {
throw new CannotGetMongoDbConnectionException("Failed to authenticate with Mongo using the given credentials");
}
}
// Use same Session for further Mongo actions within the transaction. // Use same Session for further Mongo actions within the transaction.
// Thread object will get removed by synchronization at transaction completion. // Thread object will get removed by synchronization at transaction completion.
if (TransactionSynchronizationManager.isSynchronizationActive()) { if (TransactionSynchronizationManager.isSynchronizationActive()) {

View File

@@ -51,6 +51,10 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> implements
private String databaseName; private String databaseName;
private String username;
private char[] password;
public MongoTemplate(Mongo mongo, String databaseName) { public MongoTemplate(Mongo mongo, String databaseName) {
this(mongo, databaseName, null, null); this(mongo, databaseName, null, null);
@@ -71,6 +75,25 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> implements
this.databaseName = databaseName; this.databaseName = databaseName;
} }
/**
* Sets the username to use to connect to the Mongo database
*
* @param username The username to use
*/
public void setUsername(String username) {
this.username = username;
}
/**
* Sets the password to use to authenticate with the Mongo database
*
* @param password The password to use
*/
public void setPassword(char[] password) {
this.password = password;
}
public void setDefaultCollectionName(String defaultCollectionName) { public void setDefaultCollectionName(String defaultCollectionName) {
this.defaultCollectionName = defaultCollectionName; this.defaultCollectionName = defaultCollectionName;
} }
@@ -83,6 +106,12 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> implements
return defaultCollectionName; return defaultCollectionName;
} }
/**
* @return The default collection used by this template
*/
public DBCollection getDefaultCollection() {
return getConnection().getCollection(getDefaultCollectionName());
}
public void executeCommand(String jsonCommand) { public void executeCommand(String jsonCommand) {
executeCommand((DBObject)JSON.parse(jsonCommand)); executeCommand((DBObject)JSON.parse(jsonCommand));
@@ -97,15 +126,31 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> implements
} }
} }
/**
* Executes a {@link DBCallback} translating any exceptions as necessary
*
* @param <T> The return type
* @param action The action to execute
*
* @return The return value of the {@link DBCallback}
*/
public <T> T execute(DBCallback<T> action) {
DB db = getConnection();
try {
return action.doInDB(db);
} catch (MongoException e) {
throw MongoDbUtils.translateMongoExceptionIfPossible(e);
}
}
public <T> T executeInSession(DBCallback<T> action) { public <T> T executeInSession(DBCallback<T> action) {
DB db = getConnection(); DB db = getConnection();
db.requestStart(); db.requestStart();
try { try {
return action.doInDB(db); return action.doInDB(db);
} catch (MongoException e) { } catch (MongoException e) {
//TODO refine exception thrown to capture last error. throw MongoDbUtils.translateMongoExceptionIfPossible(e);
CommandResult result = db.getLastError();
throw new InvalidDataAccessApiUsageException("Error accessing DB " + db + ":" + e.getMessage(), e);
} finally { } finally {
db.requestDone(); db.requestDone();
} }
@@ -276,6 +321,9 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> implements
@Override @Override
public DB getConnection() { public DB getConnection() {
if(username != null && password != null) {
return MongoDbUtils.getDB(mongo, databaseName, username, password);
}
return MongoDbUtils.getDB(mongo, databaseName); return MongoDbUtils.getDB(mongo, databaseName);
} }