Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011-2013 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.mongodb.core;
 
 
 import  org.springframework.beans.factory.DisposableBean;
 import  org.springframework.dao.DataAccessException;
 import  org.springframework.dao.support.PersistenceExceptionTranslator;
 import  org.springframework.util.Assert;
 import  org.springframework.util.StringUtils;
 
 import com.mongodb.DB;
Factory to create DB instances from a Mongo instance.

Author(s):
Mark Pollack
Oliver Gierke
Thomas Darimont
 
 public class SimpleMongoDbFactory implements DisposableBean, MongoDbFactory {
 
 	private final Mongo mongo;
 	private final String databaseName;
 	private final boolean mongoInstanceCreated;
 	private final UserCredentials credentials;
 	private final PersistenceExceptionTranslator exceptionTranslator;
 
Create an instance of SimpleMongoDbFactory given the Mongo instance and database name.

Parameters:
mongo Mongo instance, must not be null.
databaseName database name, not be null or empty.
 
 	public SimpleMongoDbFactory(Mongo mongoString databaseName) {
 		this(mongodatabaseNamenull);
 	}

Create an instance of SimpleMongoDbFactory given the Mongo instance, database name, and username/password

Parameters:
mongo Mongo instance, must not be null.
databaseName Database name, must not be null or empty.
credentials username and password.
 
 	public SimpleMongoDbFactory(Mongo mongoString databaseNameUserCredentials credentials) {
 		this(mongodatabaseNamecredentialsfalsenull);
 	}

Create an instance of SimpleMongoDbFactory given the Mongo instance, database name, and username/password

Parameters:
mongo Mongo instance, must not be null.
databaseName Database name, must not be null or empty.
credentials username and password.
authenticationDatabaseName the database name to use for authentication
 
 	public SimpleMongoDbFactory(Mongo mongoString databaseNameUserCredentials credentials,
 			String authenticationDatabaseName) {
 		this(mongodatabaseNamecredentialsfalseauthenticationDatabaseName);
 	}

Creates a new SimpleMongoDbFactory instance from the given MongoURI.

Parameters:
uri must not be null.
Throws:
MongoException
UnknownHostException
See also:
MongoURI
 
 	@SuppressWarnings("deprecation")
 		this(new Mongo(uri), uri.getDatabase(), new UserCredentials(uri.getUsername(), parseChars(uri.getPassword())),
 				trueuri.getDatabase());
 	}
 
	private SimpleMongoDbFactory(Mongo mongoString databaseNameUserCredentials credentials,
			boolean mongoInstanceCreatedString authenticationDatabaseName) {
		Assert.notNull(mongo"Mongo must not be null");
		Assert.hasText(databaseName"Database name must not be empty");
		Assert.isTrue(databaseName.matches("[\\w-]+"),
				"Database name must only contain letters, numbers, underscores and dashes!");
		this. = mongo;
		this. = databaseName;
		this. = mongoInstanceCreated;
		this. = credentials == null ? . : credentials;
		this. = StringUtils.hasText(authenticationDatabaseName) ? authenticationDatabaseName
databaseName;
		Assert.isTrue(this..matches("[\\w-]+"),
				"Authentication database name must only contain letters, numbers, underscores and dashes!");
	}

Configures the WriteConcern to be used on the DB instance being created.

Parameters:
writeConcern the writeConcern to set
	public void setWriteConcern(WriteConcern writeConcern) {
		this. = writeConcern;
	}
	/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.MongoDbFactory#getDb()
	 */
	public DB getDb() throws DataAccessException {
	}
	/*
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.MongoDbFactory#getDb(java.lang.String)
	 */
	public DB getDb(String dbNamethrows DataAccessException {
		Assert.hasText(dbName"Database name must not be empty.");
		DB db = MongoDbUtils.getDB(dbName);
		if ( != null) {
		}
		return db;
	}

Clean up the Mongo instance if it was created by the factory itself.

See also:
DisposableBean.destroy()
	public void destroy() throws Exception {
		}
	}
	private static String parseChars(char[] chars) {
		return chars == null ? null : String.valueOf(chars);
	}
	/* 
	 * (non-Javadoc)
	 * @see org.springframework.data.mongodb.MongoDbFactory#getExceptionTranslator()
	 */
	public PersistenceExceptionTranslator getExceptionTranslator() {
		return this.;
	}
New to GrepCode? Check out our FAQ X