From eca4edc1afbd00203fc569a5cd2dcdd29fe690f7 Mon Sep 17 00:00:00 2001 From: trisberg Date: Thu, 15 Jul 2010 16:40:43 -0400 Subject: [PATCH] beginning of the MongoDB support --- .classpath | 20 ++++ .gitignore | 1 + .project | 13 ++ .settings/org.eclipse.jdt.core.prefs | 5 + pom.xml | 111 ++++++++++++++++++ .../datastore/document/DocumentMapper.java | 29 +++++ .../datastore/document/DocumentSource.java | 29 +++++ .../document/mongodb/MongoDbFactoryBean.java | 75 ++++++++++++ 8 files changed, 283 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 pom.xml create mode 100644 src/main/java/org/springframework/datastore/document/DocumentMapper.java create mode 100644 src/main/java/org/springframework/datastore/document/DocumentSource.java create mode 100644 src/main/java/org/springframework/datastore/document/mongodb/MongoDbFactoryBean.java diff --git a/.classpath b/.classpath new file mode 100644 index 000000000..8cfe5894d --- /dev/null +++ b/.classpath @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..b83d22266 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/.project b/.project new file mode 100644 index 000000000..68bed8f8a --- /dev/null +++ b/.project @@ -0,0 +1,13 @@ + + datastore-document + + + + + org.eclipse.jdt.core.javabuilder + + + + org.eclipse.jdt.core.javanature + + \ No newline at end of file diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..cdaeaf82f --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +#Wed Jul 14 14:36:01 EDT 2010 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.source=1.5 +org.eclipse.jdt.core.compiler.compliance=1.5 diff --git a/pom.xml b/pom.xml new file mode 100644 index 000000000..2df6c53cb --- /dev/null +++ b/pom.xml @@ -0,0 +1,111 @@ + + + 4.0.0 + org.springframework.datastore.document + datastore-document + Spring Datastore Document + jar + 0.0.1-SNAPSHOT + + + UTF-8 + 3.0.0.RELEASE + 1.6.0 + + + + + junit + junit + 4.8.1 + test + + + + log4j + log4j + 1.2.15 + + + + javax.mail + mail + + + javax.jms + jms + + + com.sun.jdmk + jmxtools + + + com.sun.jmx + jmxri + + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.slf4j + jcl-over-slf4j + ${slf4j.version} + + + org.slf4j + slf4j-log4j12 + ${slf4j.version} + + + + + org.mongodb + mongo-java-driver + 1.4 + + + + + org.springframework + org.springframework.core + ${spring.version} + + + org.springframework + org.springframework.test + ${spring.version} + test + + + org.springframework + org.springframework.context + ${spring.version} + + + org.springframework + org.springframework.transaction + ${spring.version} + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + 1.5 + 1.5 + + + + + + \ No newline at end of file diff --git a/src/main/java/org/springframework/datastore/document/DocumentMapper.java b/src/main/java/org/springframework/datastore/document/DocumentMapper.java new file mode 100644 index 000000000..69d8bc381 --- /dev/null +++ b/src/main/java/org/springframework/datastore/document/DocumentMapper.java @@ -0,0 +1,29 @@ +/* + * 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; + +/** + * Class used to map a Document to a business object. + * + * @author Thomas Risberg + * @since 1.0 + */ +public interface DocumentMapper { + + T mapDocument(D document); + +} diff --git a/src/main/java/org/springframework/datastore/document/DocumentSource.java b/src/main/java/org/springframework/datastore/document/DocumentSource.java new file mode 100644 index 000000000..abbcf0351 --- /dev/null +++ b/src/main/java/org/springframework/datastore/document/DocumentSource.java @@ -0,0 +1,29 @@ +/* + * 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; + +/** + * Class used to map a business object to an object providing the source data for a Document. + * + * @author Thomas Risberg + * @since 1.0 + */ +public interface DocumentSource { + + D getDocument(T source); + +} diff --git a/src/main/java/org/springframework/datastore/document/mongodb/MongoDbFactoryBean.java b/src/main/java/org/springframework/datastore/document/mongodb/MongoDbFactoryBean.java new file mode 100644 index 000000000..77469efa2 --- /dev/null +++ b/src/main/java/org/springframework/datastore/document/mongodb/MongoDbFactoryBean.java @@ -0,0 +1,75 @@ +/* + * 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.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; + +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. + * + * @author Thomas Risberg + * @since 1.0 + */ +public class MongoDbFactoryBean implements FactoryBean, InitializingBean { + + /** + * Logger, available to subclasses. + */ + protected final Log logger = LogFactory.getLog(getClass()); + + private Mongo mongo; + private String databaseName; + + public void setMongo(Mongo mongo) { + this.mongo = mongo; + } + + public void setDatabaseName(String databaseName) { + this.databaseName = databaseName; + } + + public DB getObject() throws Exception { + return mongo.getDB(databaseName); + } + + public Class getObjectType() { + return DB.class; + } + + public boolean isSingleton() { + return false; + } + + 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(); + } + } + +}