adding beginnings of some Mongo abstractions
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.datastore.core.DatastoreConnectionFactory;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.mongodb.DB;
|
||||
import com.mongodb.Mongo;
|
||||
|
||||
/**
|
||||
* Convenient factory for configuring MongoDB.
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @since 1.0
|
||||
*/
|
||||
public class MongoConnectionFactory implements DatastoreConnectionFactory<DB>, InitializingBean {
|
||||
|
||||
/**
|
||||
* Logger, available to subclasses.
|
||||
*/
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private Mongo mongo;
|
||||
private String databaseName;
|
||||
|
||||
public MongoConnectionFactory() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MongoConnectionFactory(String databaseName) {
|
||||
super();
|
||||
this.databaseName = databaseName;
|
||||
}
|
||||
|
||||
public MongoConnectionFactory(Mongo mongo, String databaseName) {
|
||||
super();
|
||||
this.mongo = mongo;
|
||||
this.databaseName = databaseName;
|
||||
}
|
||||
|
||||
public void setMongo(Mongo mongo) {
|
||||
this.mongo = mongo;
|
||||
}
|
||||
|
||||
public void setDatabaseName(String databaseName) {
|
||||
this.databaseName = databaseName;
|
||||
}
|
||||
|
||||
public boolean isSingleton() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
// 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");
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package org.springframework.datastore.document.mongodb;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.core.DataMapper;
|
||||
import org.springframework.data.core.QueryDefinition;
|
||||
import org.springframework.datastore.core.AbstractDatastoreTemplate;
|
||||
|
||||
import com.mongodb.DB;
|
||||
import com.mongodb.Mongo;
|
||||
|
||||
public class MongoDatastoreTemplate extends AbstractDatastoreTemplate<DB> {
|
||||
|
||||
private Mongo mongo;
|
||||
|
||||
|
||||
public MongoDatastoreTemplate() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MongoDatastoreTemplate(Mongo mongo, String databaseName) {
|
||||
super();
|
||||
this.mongo = mongo;
|
||||
setDatastoreConnectionFactory(new MongoConnectionFactory(mongo, databaseName));
|
||||
}
|
||||
|
||||
|
||||
public Mongo getMongo() {
|
||||
return mongo;
|
||||
}
|
||||
|
||||
public void setMongo(Mongo mongo) {
|
||||
this.mongo = mongo;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public <S, T> List<T> query(QueryDefinition arg0, DataMapper<S, T> arg1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -38,19 +38,18 @@ public class MongoDbFactoryBean implements FactoryBean<DB>, InitializingBean {
|
||||
*/
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private Mongo mongo;
|
||||
private String databaseName;
|
||||
private MongoConnectionFactory mcf = new MongoConnectionFactory();
|
||||
|
||||
public void setMongo(Mongo mongo) {
|
||||
this.mongo = mongo;
|
||||
this.mcf.setMongo(mongo);
|
||||
}
|
||||
|
||||
public void setDatabaseName(String databaseName) {
|
||||
this.databaseName = databaseName;
|
||||
this.mcf.setDatabaseName(databaseName);
|
||||
}
|
||||
|
||||
public DB getObject() throws Exception {
|
||||
return mongo.getDB(databaseName);
|
||||
return mcf.getConnection();
|
||||
}
|
||||
|
||||
public Class<? extends DB> getObjectType() {
|
||||
@@ -62,14 +61,7 @@ public class MongoDbFactoryBean implements FactoryBean<DB>, InitializingBean {
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
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();
|
||||
}
|
||||
this.mcf.afterPropertiesSet();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user