Browse Source

refactoring connection factory bean; adding exception translation hooks

pull/1/head
Thomas Risberg 16 years ago
parent
commit
2515607121
  1. 5
      src/main/java/org/springframework/datastore/document/AbstractDocumentStoreTemplate.java
  2. 21
      src/main/java/org/springframework/datastore/document/DocumentStoreConnectionFactory.java
  3. 123
      src/main/java/org/springframework/datastore/document/couchdb/CouchDbConnectionFactory.java
  4. 81
      src/main/java/org/springframework/datastore/document/couchdb/CouchDbFactoryBean.java
  5. 57
      src/main/java/org/springframework/datastore/document/couchdb/CouchDbUtils.java
  6. 15
      src/main/java/org/springframework/datastore/document/couchdb/CouchTemplate.java
  7. 5
      src/main/java/org/springframework/datastore/document/mongodb/MongoBeanPropertyDocumentSource.java
  8. 53
      src/main/java/org/springframework/datastore/document/mongodb/MongoDbFactoryBean.java
  9. 58
      src/main/java/org/springframework/datastore/document/mongodb/MongoDbUtils.java
  10. 53
      src/main/java/org/springframework/datastore/document/mongodb/MongoTemplate.java

5
src/main/java/org/springframework/datastore/document/AbstractDocumentStoreTemplate.java

@ -19,12 +19,11 @@ package org.springframework.datastore.document; @@ -19,12 +19,11 @@ package org.springframework.datastore.document;
public abstract class AbstractDocumentStoreTemplate<C> {
public abstract DocumentStoreConnectionFactory<C> getDocumentStoreConnectionFactory();
public abstract C getConnection();
public <T> T execute(DocumentStoreConnectionCallback<C, T> action) {
try {
return action.doInConnection(getDocumentStoreConnectionFactory().getConnection());
return action.doInConnection(getConnection());
}
catch (Exception e) {
throw new UncategorizedDocumentStoreException("Failure executing using datastore connection", e);

21
src/main/java/org/springframework/datastore/document/DocumentStoreConnectionFactory.java

@ -1,21 +0,0 @@ @@ -1,21 +0,0 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.datastore.document;
public interface DocumentStoreConnectionFactory<C> {
C getConnection();
}

123
src/main/java/org/springframework/datastore/document/couchdb/CouchDbConnectionFactory.java

@ -1,123 +0,0 @@ @@ -1,123 +0,0 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.datastore.document.couchdb;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jcouchdb.db.Database;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.datastore.document.DocumentStoreConnectionFactory;
/**
* Convenient factory for configuring CouchDB.
*
* @author Thomas Risberg
* @since 1.0
*/
public class CouchDbConnectionFactory implements DocumentStoreConnectionFactory<Database>, InitializingBean {
/**
* Logger, available to subclasses.
*/
protected final Log logger = LogFactory.getLog(getClass());
private Database database;
private String host;
private String databaseName;
public CouchDbConnectionFactory() {
super();
}
public CouchDbConnectionFactory(String host, String databaseName) {
super();
this.host = host;
this.databaseName = databaseName;
}
public CouchDbConnectionFactory(Database database) {
super();
this.database = database;
}
public void setDatabase(Database database) {
this.database = database;
}
public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}
public void setHost(String host) {
this.host = host;
}
public boolean isSingleton() {
return false;
}
public void afterPropertiesSet() throws Exception {
// apply defaults - convenient when used to configure for tests
// in an application context
if (database == null) {
if (databaseName == null) {
logger.warn("Property databaseName not specified. Using default name 'test'");
databaseName = "test";
}
if (host == null) {
logger.warn("Property host not specified. Using default 'localhost'");
database = new Database(host, databaseName);
}
database = new Database(host, databaseName);
}
else {
logger.info("Using provided database configuration");
}
}
public Database getConnection() {
synchronized (this){
if (database == null) {
try {
afterPropertiesSet();
} catch (Exception e) {
throw new CannotGetCouchDbConnectionException("Unable to connect to CouchDB", e);
}
}
}
return database;
}
}

81
src/main/java/org/springframework/datastore/document/mongodb/MongoDbConnectionFactory.java → src/main/java/org/springframework/datastore/document/couchdb/CouchDbFactoryBean.java

@ -14,33 +14,17 @@ @@ -14,33 +14,17 @@
* limitations under the License.
*/
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.datastore.document.mongodb;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
package org.springframework.datastore.document.couchdb;
import org.jcouchdb.db.Database;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.datastore.document.DocumentStoreConnectionFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.util.Assert;
import com.mongodb.DB;
import com.mongodb.Mongo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Convenient factory for configuring MongoDB.
@ -48,37 +32,43 @@ import com.mongodb.Mongo; @@ -48,37 +32,43 @@ import com.mongodb.Mongo;
* @author Thomas Risberg
* @since 1.0
*/
public class MongoDbConnectionFactory implements DocumentStoreConnectionFactory<DB>, InitializingBean {
public class CouchDbFactoryBean implements FactoryBean<Database>, InitializingBean,
PersistenceExceptionTranslator {
/**
* Logger, available to subclasses.
*/
protected final Log logger = LogFactory.getLog(getClass());
private Mongo mongo;
private String host;
private Integer port;
private String databaseName;
public MongoDbConnectionFactory() {
super();
public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}
public MongoDbConnectionFactory(String databaseName) {
super();
this.databaseName = databaseName;
public void setHost(String host) {
this.host = host;
}
public MongoDbConnectionFactory(Mongo mongo, String databaseName) {
super();
this.mongo = mongo;
this.databaseName = databaseName;
public void setPort(int port) {
this.port = port;
}
public void setMongo(Mongo mongo) {
this.mongo = mongo;
public Database getObject() throws Exception {
Assert.hasText(host, "Host must not be empty");
Assert.hasText(databaseName, "Database name must not be empty");
if (port == null) {
return new Database(host, databaseName);
}
else {
return new Database(host, port, databaseName);
}
}
public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
public Class<? extends Database> getObjectType() {
return Database.class;
}
public boolean isSingleton() {
@ -88,20 +78,19 @@ public class MongoDbConnectionFactory implements DocumentStoreConnectionFactory< @@ -88,20 +78,19 @@ public class MongoDbConnectionFactory implements DocumentStoreConnectionFactory<
public void afterPropertiesSet() throws Exception {
// apply defaults - convenient when used to configure for tests
// in an application context
if (host == null) {
logger.warn("Property host not specified. Using default 'localhost'");
databaseName = "localhost";
}
if (databaseName == null) {
logger.warn("Property databaseName not specified. Using default name 'test'");
databaseName = "test";
}
if (mongo == null) {
logger.warn("Property mongo not specified. Using default configuration");
mongo = new Mongo();
}
}
public DB getConnection() {
Assert.notNull(mongo, "Mongo must not be null");
Assert.hasText(databaseName, "Database name must not be empty");
return mongo.getDB(databaseName);
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
logger.debug("Translating " + ex);
return CouchDbUtils.translateCouchExceptionIfPossible(ex);
}
}

57
src/main/java/org/springframework/datastore/document/couchdb/CouchDbUtils.java

@ -0,0 +1,57 @@ @@ -0,0 +1,57 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.datastore.document.couchdb;
import org.jcouchdb.exception.CouchDBException;
import org.springframework.dao.DataAccessException;
import org.springframework.datastore.document.UncategorizedDocumentStoreException;
/**
* Helper class featuring helper methods for internal MongoDb classes.
*
* <p>Mainly intended for internal use within the framework.
*
* @author Thomas Risberg
* @since 1.0
*/
public class CouchDbUtils {
/**
* Convert the given runtime exception to an appropriate exception from the
* <code>org.springframework.dao</code> hierarchy.
* Return null if no translation is appropriate: any other exception may
* have resulted from user code, and should not be translated.
* @param ex runtime exception that occurred
* @return the corresponding DataAccessException instance,
* or <code>null</code> if the exception should not be translated
*/
public static DataAccessException translateCouchExceptionIfPossible(RuntimeException ex) {
// Check for well-known MongoException subclasses.
// All other MongoExceptions
if (ex instanceof CouchDBException) {
return new UncategorizedDocumentStoreException(ex.getMessage(), ex);
}
// If we get here, we have an exception that resulted from user code,
// rather than the persistence provider, so we return null to indicate
// that translation should not occur.
return null;
}
}

15
src/main/java/org/springframework/datastore/document/couchdb/CouchTemplate.java

@ -21,11 +21,10 @@ import org.jcouchdb.db.Database; @@ -21,11 +21,10 @@ import org.jcouchdb.db.Database;
import org.jcouchdb.document.BaseDocument;
import org.springframework.datastore.document.AbstractDocumentStoreTemplate;
import org.springframework.datastore.document.DocumentSource;
import org.springframework.datastore.document.DocumentStoreConnectionFactory;
public class CouchTemplate extends AbstractDocumentStoreTemplate<Database> {
private DocumentStoreConnectionFactory<Database> connectionFactory;
private Database database;
public CouchTemplate() {
super();
@ -33,22 +32,22 @@ import org.springframework.datastore.document.DocumentStoreConnectionFactory; @@ -33,22 +32,22 @@ import org.springframework.datastore.document.DocumentStoreConnectionFactory;
public CouchTemplate(String host, String databaseName) {
super();
connectionFactory = new CouchDbConnectionFactory(host, databaseName);
database = new Database(host, databaseName);
}
public CouchTemplate(CouchDbConnectionFactory mcf) {
public CouchTemplate(Database database) {
super();
connectionFactory = mcf;
this.database = database;
}
public void save(DocumentSource<BaseDocument> documentSource) {
BaseDocument d = documentSource.getDocument();
getDocumentStoreConnectionFactory().getConnection().createDocument(d);
getConnection().createDocument(d);
}
@Override
public DocumentStoreConnectionFactory<Database> getDocumentStoreConnectionFactory() {
return connectionFactory;
public Database getConnection() {
return database;
}

5
src/main/java/org/springframework/datastore/document/mongodb/MongoBeanPropertyDocumentSource.java

@ -27,13 +27,9 @@ import org.apache.commons.logging.LogFactory; @@ -27,13 +27,9 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.NotReadablePropertyException;
import org.springframework.beans.NotWritablePropertyException;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.datastore.document.DocumentMapper;
import org.springframework.datastore.document.DocumentSource;
import org.springframework.util.Assert;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
@ -67,6 +63,7 @@ public class MongoBeanPropertyDocumentSource implements DocumentSource<DBObject> @@ -67,6 +63,7 @@ public class MongoBeanPropertyDocumentSource implements DocumentSource<DBObject>
}
@SuppressWarnings("rawtypes")
public DBObject getDocument() {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this.source);
DBObject dbo = new BasicDBObject();

53
src/main/java/org/springframework/datastore/document/mongodb/MongoDbFactoryBean.java

@ -18,6 +18,9 @@ package org.springframework.datastore.document.mongodb; @@ -18,6 +18,9 @@ package org.springframework.datastore.document.mongodb;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.util.Assert;
import com.mongodb.DB;
import com.mongodb.Mongo;
@ -31,25 +34,39 @@ import org.apache.commons.logging.LogFactory; @@ -31,25 +34,39 @@ import org.apache.commons.logging.LogFactory;
* @author Thomas Risberg
* @since 1.0
*/
public class MongoDbFactoryBean implements FactoryBean<DB>, InitializingBean {
public class MongoDbFactoryBean implements FactoryBean<DB>, InitializingBean,
PersistenceExceptionTranslator {
/**
* Logger, available to subclasses.
*/
protected final Log logger = LogFactory.getLog(getClass());
private MongoDbConnectionFactory mcf = new MongoDbConnectionFactory();
private Mongo mongo;
private String host;
private Integer port;
private String databaseName;
public void setMongo(Mongo mongo) {
this.mcf.setMongo(mongo);
this.mongo = mongo;
}
public void setDatabaseName(String databaseName) {
this.mcf.setDatabaseName(databaseName);
this.databaseName = databaseName;
}
public void setHost(String host) {
this.host = host;
}
public void setPort(int port) {
this.port = port;
}
public DB getObject() throws Exception {
return mcf.getConnection();
Assert.notNull(mongo, "Mongo must not be null");
Assert.hasText(databaseName, "Database name must not be empty");
return mongo.getDB(databaseName);
}
public Class<? extends DB> getObjectType() {
@ -61,7 +78,31 @@ public class MongoDbFactoryBean implements FactoryBean<DB>, InitializingBean { @@ -61,7 +78,31 @@ public class MongoDbFactoryBean implements FactoryBean<DB>, InitializingBean {
}
public void afterPropertiesSet() throws Exception {
this.mcf.afterPropertiesSet();
// apply defaults - convenient when used to configure for tests
// in an application context
if (databaseName == null) {
logger.warn("Property databaseName not specified. Using default name 'test'");
databaseName = "test";
}
if (mongo == null) {
logger.warn("Property mongo not specified. Using default configuration");
if (host == null) {
mongo = new Mongo();
}
else {
if (port == null) {
mongo = new Mongo(host);
}
else {
mongo = new Mongo(host, port);
}
}
}
}
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
logger.debug("Translating " + ex);
return MongoDbUtils.translateMongoExceptionIfPossible(ex);
}
}

58
src/main/java/org/springframework/datastore/document/mongodb/MongoDbUtils.java

@ -0,0 +1,58 @@ @@ -0,0 +1,58 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.datastore.document.mongodb;
import org.springframework.dao.DataAccessException;
import org.springframework.datastore.document.UncategorizedDocumentStoreException;
import com.mongodb.MongoException;
/**
* Helper class featuring helper methods for internal MongoDb classes.
*
* <p>Mainly intended for internal use within the framework.
*
* @author Thomas Risberg
* @since 1.0
*/
public class MongoDbUtils {
/**
* Convert the given runtime exception to an appropriate exception from the
* <code>org.springframework.dao</code> hierarchy.
* Return null if no translation is appropriate: any other exception may
* have resulted from user code, and should not be translated.
* @param ex runtime exception that occurred
* @return the corresponding DataAccessException instance,
* or <code>null</code> if the exception should not be translated
*/
public static DataAccessException translateMongoExceptionIfPossible(RuntimeException ex) {
// Check for well-known MongoException subclasses.
// All other MongoExceptions
if (ex instanceof MongoException) {
return new UncategorizedDocumentStoreException(ex.getMessage(), ex);
}
// If we get here, we have an exception that resulted from user code,
// rather than the persistence provider, so we return null to indicate
// that translation should not occur.
return null;
}
}

53
src/main/java/org/springframework/datastore/document/mongodb/MongoTemplate.java

@ -25,42 +25,29 @@ import org.springframework.dao.InvalidDataAccessApiUsageException; @@ -25,42 +25,29 @@ import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.datastore.document.AbstractDocumentStoreTemplate;
import org.springframework.datastore.document.DocumentMapper;
import org.springframework.datastore.document.DocumentSource;
import org.springframework.datastore.document.DocumentStoreConnectionFactory;
import com.mongodb.CommandResult;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.WriteResult;
public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
private DocumentStoreConnectionFactory<DB> connectionFactory;
private DB db;
public MongoTemplate() {
super();
}
// public MongoTemplate() {
// super();
// }
public MongoTemplate(Mongo mongo, String databaseName) {
public MongoTemplate(DB db) {
super();
connectionFactory = new MongoDbConnectionFactory(mongo, databaseName);
}
public MongoTemplate(MongoDbConnectionFactory mcf) {
super();
connectionFactory = mcf;
}
@Override
public DocumentStoreConnectionFactory<DB> getDocumentStoreConnectionFactory() {
return connectionFactory;
this.db = db;
}
public void execute(String command) {
DB db = getDocumentStoreConnectionFactory().getConnection();
CommandResult cr = db.command(command);
CommandResult cr = getConnection().command(command);
String err = cr.getErrorMessage();
if (err != null) {
throw new InvalidDataAccessApiUsageException("Command execution of " +
@ -69,9 +56,7 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> { @@ -69,9 +56,7 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
}
public void execute(DocumentSource<DBObject> command) {
CommandResult cr = getDocumentStoreConnectionFactory()
.getConnection()
.command(command.getDocument());
CommandResult cr = getConnection().command(command.getDocument());
String err = cr.getErrorMessage();
if (err != null) {
throw new InvalidDataAccessApiUsageException("Command execution of " +
@ -80,18 +65,15 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> { @@ -80,18 +65,15 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
}
public void createCollection(String collectionName, DocumentSource<DBObject> documentSource) {
DB db = getDocumentStoreConnectionFactory().getConnection();
try {
db.createCollection(collectionName, documentSource.getDocument());
getConnection().createCollection(collectionName, documentSource.getDocument());
} catch (MongoException e) {
throw new InvalidDataAccessApiUsageException("Error creating collection " + collectionName + ": " + e.getMessage(), e);
}
}
public void dropCollection(String collectionName) {
getDocumentStoreConnectionFactory()
.getConnection()
.getCollection(collectionName)
getConnection().getCollection(collectionName)
.drop();
}
@ -102,10 +84,9 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> { @@ -102,10 +84,9 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
public void save(String collectionName, DocumentSource<DBObject> documentSource) {
DBObject dbDoc = documentSource.getDocument();
DB db = getDocumentStoreConnectionFactory().getConnection();
WriteResult wr = null;
try {
wr = db.getCollection(collectionName).save(dbDoc);
wr = getConnection().getCollection(collectionName).save(dbDoc);
} catch (MongoException e) {
throw new DataRetrievalFailureException(wr.getLastError().getErrorMessage(), e);
}
@ -118,12 +99,20 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> { @@ -118,12 +99,20 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
public <T> List<T> queryForCollection(String collectionName, DocumentMapper<DBObject, T> mapper) {
List<T> results = new ArrayList<T>();
DB db = getDocumentStoreConnectionFactory().getConnection();
DBCollection collection = db.getCollection(collectionName);
DBCollection collection = getConnection().getCollection(collectionName);
for (DBObject dbo : collection.find()) {
results.add(mapper.mapDocument(dbo));
}
return results;
}
public RuntimeException translateIfNecessary(RuntimeException ex) {
return MongoDbUtils.translateMongoExceptionIfPossible(ex);
}
@Override
public DB getConnection() {
return db;
}
}

Loading…
Cancel
Save