10 changed files with 230 additions and 245 deletions
@ -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(); |
||||
} |
||||
@ -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; |
||||
} |
||||
|
||||
} |
||||
@ -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; |
||||
} |
||||
|
||||
} |
||||
@ -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; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue