diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoFactoryBean.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoFactoryBean.java new file mode 100644 index 000000000..4fee05697 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoFactoryBean.java @@ -0,0 +1,129 @@ +/* + * 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.data.document.mongodb; + +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +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.Mongo; +import com.mongodb.MongoOptions; +import com.mongodb.ServerAddress; + +/** + * Convenient factory for configuring MongoDB. + * + * @author Thomas Risberg + * @author Graeme Rocher + * + * @since 1.0 + */ +public class MongoFactoryBean implements FactoryBean, InitializingBean, + PersistenceExceptionTranslator { + + + /** + * Logger, available to subclasses. + */ + protected final Log logger = LogFactory.getLog(getClass()); + + private Mongo mongo; + private MongoOptions mongoOptions; + private String host; + private Integer port; + private List replicaSetSeeds; + private List replicaPair; + + public void setMongoOptions(MongoOptions mongoOptions) { + this.mongoOptions = mongoOptions; + } + + public void setReplicaSetSeeds(List replicaSetSeeds) { + this.replicaSetSeeds = replicaSetSeeds; + } + + public void setReplicaPair(List replicaPair) { + this.replicaPair = replicaPair; + } + + public void setHost(String host) { + this.host = host; + } + + public void setPort(int port) { + this.port = port; + } + + public Mongo getObject() throws Exception { + Assert.notNull(mongo, "Mongo must not be null"); + return mongo; + } + + public Class getObjectType() { + return Mongo.class; + } + + public boolean isSingleton() { + return false; + } + + public void afterPropertiesSet() throws Exception { + // apply defaults - convenient when used to configure for tests + // in an application context + if (mongo == null) { + + if (host == null) { + logger.warn("Property host not specified. Using default configuration"); + mongo = new Mongo(); + } + else { + ServerAddress defaultOptions = new ServerAddress(); + if(mongoOptions == null) mongoOptions = new MongoOptions(); + if(replicaPair != null) { + if(replicaPair.size() < 2) { + throw new CannotGetMongoDbConnectionException("A replica pair must have two server entries"); + } + mongo = new Mongo(replicaPair.get(0), replicaPair.get(1), mongoOptions); + } + else if(replicaSetSeeds != null) { + mongo = new Mongo(replicaSetSeeds, mongoOptions); + } + else { + String mongoHost = host != null ? host : defaultOptions.getHost(); + if(port != null) { + mongo = new Mongo(new ServerAddress(mongoHost, port), mongoOptions); + } + else { + mongo = new Mongo(mongoHost, mongoOptions); + } + } + } + } + } + + public DataAccessException translateExceptionIfPossible(RuntimeException ex) { + logger.debug("Translating " + ex); + return MongoDbUtils.translateMongoExceptionIfPossible(ex); + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOptionsFactoryBean.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOptionsFactoryBean.java new file mode 100644 index 000000000..a8c0cab1f --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOptionsFactoryBean.java @@ -0,0 +1,137 @@ +/* + * 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.data.document.mongodb; + +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; + + +import com.mongodb.MongoOptions; + +/** + * A factory bean for consruction a MongoOptions instance + * + * @author Graeme Rocher + * + */ +public class MongoOptionsFactoryBean implements FactoryBean, InitializingBean{ + + private static final MongoOptions MONGO_OPTIONS = new MongoOptions(); + /** + number of connections allowed per host + will block if run out + */ + private int connectionsPerHost = MONGO_OPTIONS.connectionsPerHost; + + /** + multiplier for connectionsPerHost for # of threads that can block + if connectionsPerHost is 10, and threadsAllowedToBlockForConnectionMultiplier is 5, + then 50 threads can block + more than that and an exception will be throw + */ + private int threadsAllowedToBlockForConnectionMultiplier = MONGO_OPTIONS.threadsAllowedToBlockForConnectionMultiplier; + + /** + * max wait time of a blocking thread for a connection + */ + private int maxWaitTime = MONGO_OPTIONS.maxWaitTime; + + /** + connect timeout in milliseconds. 0 is default and infinite + */ + private int connectTimeout = MONGO_OPTIONS.connectTimeout; + + /** + socket timeout. 0 is default and infinite + */ + private int socketTimeout = MONGO_OPTIONS.socketTimeout; + + /** + this controls whether or not on a connect, the system retries automatically + */ + private boolean autoConnectRetry = MONGO_OPTIONS.autoConnectRetry; + + + /** + number of connections allowed per host + will block if run out + */ + public void setConnectionsPerHost(int connectionsPerHost) { + this.connectionsPerHost = connectionsPerHost; + } + + /** + multiplier for connectionsPerHost for # of threads that can block + if connectionsPerHost is 10, and threadsAllowedToBlockForConnectionMultiplier is 5, + then 50 threads can block + more than that and an exception will be throw + */ + public void setThreadsAllowedToBlockForConnectionMultiplier( + int threadsAllowedToBlockForConnectionMultiplier) { + this.threadsAllowedToBlockForConnectionMultiplier = threadsAllowedToBlockForConnectionMultiplier; + } + + /** + * max wait time of a blocking thread for a connection + */ + public void setMaxWaitTime(int maxWaitTime) { + this.maxWaitTime = maxWaitTime; + } + + /** + connect timeout in milliseconds. 0 is default and infinite + */ + public void setConnectTimeout(int connectTimeout) { + this.connectTimeout = connectTimeout; + } + + /** + socket timeout. 0 is default and infinite + */ + public void setSocketTimeout(int socketTimeout) { + this.socketTimeout = socketTimeout; + } + + /** + this controls whether or not on a connect, the system retries automatically + */ + public void setAutoConnectRetry(boolean autoConnectRetry) { + this.autoConnectRetry = autoConnectRetry; + } + + public void afterPropertiesSet() throws Exception { + MONGO_OPTIONS.connectionsPerHost = connectionsPerHost; + MONGO_OPTIONS.threadsAllowedToBlockForConnectionMultiplier = threadsAllowedToBlockForConnectionMultiplier; + MONGO_OPTIONS.maxWaitTime = maxWaitTime; + MONGO_OPTIONS.connectTimeout = connectTimeout; + MONGO_OPTIONS.socketTimeout = socketTimeout; + MONGO_OPTIONS.autoConnectRetry = autoConnectRetry; + + } + + public MongoOptions getObject() throws Exception { + return MONGO_OPTIONS; + } + + public Class getObjectType() { + return MongoOptions.class; + } + + public boolean isSingleton() { + return true; + } + +}