adding beginning of DocumentTemplates for MongoDB and CouchDB; added simple MongBeanPropertyDocumentMapper
This commit is contained in:
37
.classpath
37
.classpath
@@ -1,32 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src/main/java"/>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="var" path="M2_REPO/aopalliance/aopalliance/1.0/aopalliance-1.0.jar" sourcepath="M2_REPO/aopalliance/aopalliance/1.0/aopalliance-1.0-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/aopalliance/com.springsource.org.aopalliance/1.0.0/com.springsource.org.aopalliance-1.0.0.jar" sourcepath="M2_REPO/org/aopalliance/com.springsource.org.aopalliance/1.0.0/com.springsource.org.aopalliance-1.0.0-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/data/data-commons/1.0.0.CI-SNAPSHOT/data-commons-1.0.0.CI-SNAPSHOT.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/slf4j/jcl-over-slf4j/1.6.0/jcl-over-slf4j-1.6.0.jar" sourcepath="M2_REPO/org/slf4j/jcl-over-slf4j/1.6.0/jcl-over-slf4j-1.6.0-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/junit/junit/4.8.1/junit-4.8.1.jar" sourcepath="M2_REPO/junit/junit/4.8.1/junit-4.8.1-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/log4j/log4j/1.2.15/log4j-1.2.15.jar" sourcepath="M2_REPO/log4j/log4j/1.2.15/log4j-1.2.15-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/mongodb/mongo-java-driver/1.4/mongo-java-driver-1.4.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/org.springframework.aop/3.0.0.RELEASE/org.springframework.aop-3.0.0.RELEASE.jar" sourcepath="M2_REPO/org/springframework/org.springframework.aop/3.0.0.RELEASE/org.springframework.aop-3.0.0.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/org.springframework.asm/3.0.0.RELEASE/org.springframework.asm-3.0.0.RELEASE.jar" sourcepath="M2_REPO/org/springframework/org.springframework.asm/3.0.0.RELEASE/org.springframework.asm-3.0.0.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/org.springframework.beans/3.0.0.RELEASE/org.springframework.beans-3.0.0.RELEASE.jar" sourcepath="M2_REPO/org/springframework/org.springframework.beans/3.0.0.RELEASE/org.springframework.beans-3.0.0.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/org.springframework.context/3.0.0.RELEASE/org.springframework.context-3.0.0.RELEASE.jar" sourcepath="M2_REPO/org/springframework/org.springframework.context/3.0.0.RELEASE/org.springframework.context-3.0.0.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/org.springframework.core/3.0.0.RELEASE/org.springframework.core-3.0.0.RELEASE.jar" sourcepath="M2_REPO/org/springframework/org.springframework.core/3.0.0.RELEASE/org.springframework.core-3.0.0.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/org.springframework.expression/3.0.0.RELEASE/org.springframework.expression-3.0.0.RELEASE.jar" sourcepath="M2_REPO/org/springframework/org.springframework.expression/3.0.0.RELEASE/org.springframework.expression-3.0.0.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/org.springframework.test/3.0.0.RELEASE/org.springframework.test-3.0.0.RELEASE.jar" sourcepath="M2_REPO/org/springframework/org.springframework.test/3.0.0.RELEASE/org.springframework.test-3.0.0.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/org.springframework.transaction/3.0.0.RELEASE/org.springframework.transaction-3.0.0.RELEASE.jar" sourcepath="M2_REPO/org/springframework/org.springframework.transaction/3.0.0.RELEASE/org.springframework.transaction-3.0.0.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/slf4j/slf4j-api/1.6.0/slf4j-api-1.6.0.jar" sourcepath="M2_REPO/org/slf4j/slf4j-api/1.6.0/slf4j-api-1.6.0-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/slf4j/slf4j-log4j12/1.6.0/slf4j-log4j12-1.6.0.jar" sourcepath="M2_REPO/org/slf4j/slf4j-log4j12/1.6.0/slf4j-log4j12-1.6.0-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-aop/3.0.2.RELEASE/spring-aop-3.0.2.RELEASE.jar" sourcepath="M2_REPO/org/springframework/spring-aop/3.0.2.RELEASE/spring-aop-3.0.2.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-asm/3.0.2.RELEASE/spring-asm-3.0.2.RELEASE.jar" sourcepath="M2_REPO/org/springframework/spring-asm/3.0.2.RELEASE/spring-asm-3.0.2.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-aspects/3.0.2.RELEASE/spring-aspects-3.0.2.RELEASE.jar" sourcepath="M2_REPO/org/springframework/spring-aspects/3.0.2.RELEASE/spring-aspects-3.0.2.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-beans/3.0.2.RELEASE/spring-beans-3.0.2.RELEASE.jar" sourcepath="M2_REPO/org/springframework/spring-beans/3.0.2.RELEASE/spring-beans-3.0.2.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-context/3.0.2.RELEASE/spring-context-3.0.2.RELEASE.jar" sourcepath="M2_REPO/org/springframework/spring-context/3.0.2.RELEASE/spring-context-3.0.2.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-context-support/3.0.2.RELEASE/spring-context-support-3.0.2.RELEASE.jar" sourcepath="M2_REPO/org/springframework/spring-context-support/3.0.2.RELEASE/spring-context-support-3.0.2.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-core/3.0.2.RELEASE/spring-core-3.0.2.RELEASE.jar" sourcepath="M2_REPO/org/springframework/spring-core/3.0.2.RELEASE/spring-core-3.0.2.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-expression/3.0.2.RELEASE/spring-expression-3.0.2.RELEASE.jar" sourcepath="M2_REPO/org/springframework/spring-expression/3.0.2.RELEASE/spring-expression-3.0.2.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-test/3.0.2.RELEASE/spring-test-3.0.2.RELEASE.jar" sourcepath="M2_REPO/org/springframework/spring-test/3.0.2.RELEASE/spring-test-3.0.2.RELEASE-sources.jar"/>
|
||||
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-tx/3.0.2.RELEASE/spring-tx-3.0.2.RELEASE.jar" sourcepath="M2_REPO/org/springframework/spring-tx/3.0.2.RELEASE/spring-tx-3.0.2.RELEASE-sources.jar"/>
|
||||
</classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
|
||||
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
||||
|
||||
34
.project
34
.project
@@ -1,13 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>datastore-document</name>
|
||||
<comment/>
|
||||
<projects/>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
<name>datastore-document</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.maven.ide.eclipse.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.maven.ide.eclipse.maven2Nature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#Wed Jul 14 14:36:01 EDT 2010
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
|
||||
#Wed Aug 25 00:54:37 EDT 2010
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.source=1.5
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
|
||||
org.eclipse.jdt.core.compiler.compliance=1.5
|
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||
org.eclipse.jdt.core.compiler.source=1.5
|
||||
|
||||
9
.settings/org.maven.ide.eclipse.prefs
Normal file
9
.settings/org.maven.ide.eclipse.prefs
Normal file
@@ -0,0 +1,9 @@
|
||||
#Wed Aug 25 00:54:36 EDT 2010
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
fullBuildGoals=process-test-resources
|
||||
includeModules=false
|
||||
resolveWorkspaceProjects=true
|
||||
resourceFilterGoals=process-resources resources\:testResources
|
||||
skipCompilerPlugin=true
|
||||
version=1
|
||||
11
pom.xml
11
pom.xml
@@ -62,11 +62,16 @@
|
||||
<version>${slf4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- MongoDB dependencies -->
|
||||
<!-- Document Store dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongo-java-driver</artifactId>
|
||||
<version>1.4</version>
|
||||
<version>2.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.jcouchdb</groupId>
|
||||
<artifactId>jcouchdb</artifactId>
|
||||
<version>0.11.0-1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring dependencies -->
|
||||
@@ -114,11 +119,13 @@
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>data-commons</artifactId>
|
||||
<version>1.0.0.CI-SNAPSHOT</version>
|
||||
</dependency>
|
||||
-->
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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 abstract class AbstractDocumentStoreTemplate<C> {
|
||||
|
||||
|
||||
public abstract DocumentStoreConnectionFactory<C> getDocumentStoreConnectionFactory();
|
||||
|
||||
public <T> T execute(DocumentStoreConnectionCallback<C, T> action) {
|
||||
try {
|
||||
return action.doInConnection(getDocumentStoreConnectionFactory().getConnection());
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new UncategorizedDocumentStoreException("Failure executing using datastore connection", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,8 +22,8 @@ package org.springframework.datastore.document;
|
||||
* @author Thomas Risberg
|
||||
* @since 1.0
|
||||
*/
|
||||
public interface DocumentSource<D, T> {
|
||||
public interface DocumentSource<D> {
|
||||
|
||||
D getDocument(T source);
|
||||
D getDocument();
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* 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 DocumentStoreConnectionCallback<C, T> {
|
||||
|
||||
T doInConnection(C con) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.springframework.dao.UncategorizedDataAccessException;
|
||||
|
||||
public class UncategorizedDocumentStoreException extends UncategorizedDataAccessException {
|
||||
|
||||
public UncategorizedDocumentStoreException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.springframework.datastore.document.couchdb;
|
||||
|
||||
import org.springframework.dao.DataAccessResourceFailureException;
|
||||
|
||||
public class CannotGetCouchDbConnectionException extends DataAccessResourceFailureException {
|
||||
|
||||
public CannotGetCouchDbConnectionException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* 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,8 @@
|
||||
package org.springframework.datastore.document.couchdb;
|
||||
|
||||
import org.jcouchdb.document.BaseDocument;
|
||||
import org.springframework.datastore.document.DocumentSource;
|
||||
|
||||
public interface CouchDbDocumentSource extends DocumentSource<BaseDocument> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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.db.Database;
|
||||
import org.jcouchdb.document.BaseDocument;
|
||||
import org.springframework.datastore.document.AbstractDocumentStoreTemplate;
|
||||
import org.springframework.datastore.document.DocumentSource;
|
||||
import org.springframework.datastore.document.DocumentStoreConnectionFactory;
|
||||
|
||||
public class CouchDbTemplate extends AbstractDocumentStoreTemplate<Database> {
|
||||
|
||||
private DocumentStoreConnectionFactory<Database> connectionFactory;
|
||||
|
||||
public CouchDbTemplate() {
|
||||
super();
|
||||
}
|
||||
|
||||
public CouchDbTemplate(String host, String databaseName) {
|
||||
super();
|
||||
connectionFactory = new CouchDbConnectionFactory(host, databaseName);
|
||||
}
|
||||
|
||||
public CouchDbTemplate(CouchDbConnectionFactory mcf) {
|
||||
super();
|
||||
connectionFactory = mcf;
|
||||
}
|
||||
|
||||
public void save(DocumentSource<BaseDocument> documentSource) {
|
||||
BaseDocument d = documentSource.getDocument();
|
||||
getDocumentStoreConnectionFactory().getConnection().createDocument(d);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DocumentStoreConnectionFactory<Database> getDocumentStoreConnectionFactory() {
|
||||
return connectionFactory;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.springframework.datastore.document.mongodb;
|
||||
|
||||
import org.springframework.dao.DataAccessResourceFailureException;
|
||||
|
||||
public class CannotGetMongoDbConnectionException extends DataAccessResourceFailureException {
|
||||
|
||||
public CannotGetMongoDbConnectionException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* 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 java.beans.PropertyDescriptor;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.BeanWrapper;
|
||||
import org.springframework.beans.NotWritablePropertyException;
|
||||
import org.springframework.beans.PropertyAccessorFactory;
|
||||
import org.springframework.beans.TypeMismatchException;
|
||||
import org.springframework.dao.DataRetrievalFailureException;
|
||||
import org.springframework.datastore.document.DocumentMapper;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
/**
|
||||
* Class used to map properties of a Document to the corresponding properties of a business object.
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @since 1.0
|
||||
*/
|
||||
public class MongoBeanPropertyDocumentMapper<T> implements DocumentMapper<DBObject, T> {
|
||||
|
||||
/** Logger available to subclasses */
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
/** The class we are mapping to */
|
||||
private Class<T> mappedClass;
|
||||
|
||||
/** Map of the fields we provide mapping for */
|
||||
private Map<String, PropertyDescriptor> mappedFields;
|
||||
|
||||
/** Set of bean properties we provide mapping for */
|
||||
private Set<String> mappedProperties;
|
||||
|
||||
|
||||
public MongoBeanPropertyDocumentMapper(Class<T> mappedClass) {
|
||||
initialize(mappedClass);
|
||||
}
|
||||
|
||||
|
||||
public T mapDocument(DBObject document) {
|
||||
Assert.state(this.mappedClass != null, "Mapped class was not specified");
|
||||
T mappedObject = BeanUtils.instantiate(this.mappedClass);
|
||||
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject);
|
||||
initBeanWrapper(bw);
|
||||
|
||||
Set<String> keys = document.keySet();
|
||||
for (String key : keys) {
|
||||
String keyToUse = ("_id".equals(key) ? "id" : key);
|
||||
PropertyDescriptor pd = this.mappedFields.get(keyToUse);
|
||||
if (pd != null) {
|
||||
try {
|
||||
Object value = document.get(key);
|
||||
try {
|
||||
bw.setPropertyValue(pd.getName(), value);
|
||||
}
|
||||
catch (TypeMismatchException e) {
|
||||
logger.warn("Intercepted TypeMismatchException for " + key + "' with value " + value +
|
||||
" when setting property '" + pd.getName() + "' of type " + pd.getPropertyType() +
|
||||
" on object: " + mappedObject);
|
||||
}
|
||||
}
|
||||
catch (NotWritablePropertyException ex) {
|
||||
throw new DataRetrievalFailureException(
|
||||
"Unable to map key " + key + " to property " + pd.getName(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mappedObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the class that we are mapping to.
|
||||
*/
|
||||
public final Class<T> getMappedClass() {
|
||||
return this.mappedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the mapping metadata for the given class.
|
||||
* @param mappedClass the mapped class.
|
||||
*/
|
||||
protected void initialize(Class<T> mappedClass) {
|
||||
this.mappedClass = mappedClass;
|
||||
this.mappedFields = new HashMap<String, PropertyDescriptor>();
|
||||
this.mappedProperties = new HashSet<String>();
|
||||
PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);
|
||||
for (PropertyDescriptor pd : pds) {
|
||||
if (pd.getWriteMethod() != null) {
|
||||
this.mappedFields.put(pd.getName(), pd);
|
||||
this.mappedProperties.add(pd.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the given BeanWrapper to be used for row mapping.
|
||||
* To be called for each row.
|
||||
* <p>The default implementation is empty. Can be overridden in subclasses.
|
||||
* @param bw the BeanWrapper to initialize
|
||||
*/
|
||||
protected void initBeanWrapper(BeanWrapper bw) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Static factory method to create a new MongoBeanPropertyDocumentMapper
|
||||
* (with the mapped class specified only once).
|
||||
* @param mappedClass the class that document should be mapped to
|
||||
*/
|
||||
public static <T> MongoBeanPropertyDocumentMapper<T> newInstance(Class<T> mappedClass) {
|
||||
MongoBeanPropertyDocumentMapper<T> newInstance = new MongoBeanPropertyDocumentMapper<T>(mappedClass);
|
||||
return newInstance;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,13 +14,29 @@
|
||||
* 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.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.datastore.document.DocumentStoreConnectionFactory;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.mongodb.DB;
|
||||
@@ -32,7 +48,7 @@ import com.mongodb.Mongo;
|
||||
* @author Thomas Risberg
|
||||
* @since 1.0
|
||||
*/
|
||||
public class MongoConnectionFactory implements DatastoreConnectionFactory<DB>, InitializingBean {
|
||||
public class MongoConnectionFactory implements DocumentStoreConnectionFactory<DB>, InitializingBean {
|
||||
|
||||
/**
|
||||
* Logger, available to subclasses.
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.springframework.datastore.document.mongodb;
|
||||
|
||||
import org.springframework.datastore.document.DocumentSource;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
public interface MongoDbDocumentSource extends DocumentSource<DBObject> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* 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 java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.dao.DataRetrievalFailureException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.datastore.document.AbstractDocumentStoreTemplate;
|
||||
import org.springframework.datastore.document.DocumentMapper;
|
||||
import org.springframework.datastore.document.DocumentSource;
|
||||
import org.springframework.datastore.document.DocumentStoreConnectionFactory;
|
||||
|
||||
import com.mongodb.CommandResult;
|
||||
import com.mongodb.DB;
|
||||
import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.Mongo;
|
||||
import com.mongodb.MongoException;
|
||||
import com.mongodb.WriteResult;
|
||||
|
||||
public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
|
||||
|
||||
private DocumentStoreConnectionFactory<DB> connectionFactory;
|
||||
|
||||
public MongoTemplate() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MongoTemplate(Mongo mongo, String databaseName) {
|
||||
super();
|
||||
connectionFactory = new MongoConnectionFactory(mongo, databaseName);
|
||||
}
|
||||
|
||||
public MongoTemplate(MongoConnectionFactory mcf) {
|
||||
super();
|
||||
connectionFactory = mcf;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DocumentStoreConnectionFactory<DB> getDocumentStoreConnectionFactory() {
|
||||
return connectionFactory;
|
||||
}
|
||||
|
||||
|
||||
public void execute(DocumentSource<DBObject> command) {
|
||||
CommandResult cr = getDocumentStoreConnectionFactory()
|
||||
.getConnection()
|
||||
.command(command.getDocument());
|
||||
System.out.println("! " + cr.getErrorMessage());
|
||||
}
|
||||
|
||||
public void createCollection(String collectionName, DocumentSource<DBObject> documentSource) {
|
||||
DB db = getDocumentStoreConnectionFactory().getConnection();
|
||||
try {
|
||||
db.createCollection(collectionName, documentSource.getDocument());
|
||||
} catch (MongoException e) {
|
||||
throw new InvalidDataAccessApiUsageException("Error creating collection " + collectionName + ": " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public void dropCollection(String collectionName) {
|
||||
getDocumentStoreConnectionFactory()
|
||||
.getConnection()
|
||||
.getCollection(collectionName)
|
||||
.drop();
|
||||
}
|
||||
|
||||
public void save(String collectionName, DocumentSource<DBObject> documentSource) {
|
||||
DBObject dbDoc = documentSource.getDocument();
|
||||
WriteResult wr = null;
|
||||
try {
|
||||
wr = getDocumentStoreConnectionFactory()
|
||||
.getConnection()
|
||||
.getCollection(collectionName)
|
||||
.save(dbDoc);
|
||||
} catch (MongoException e) {
|
||||
throw new DataRetrievalFailureException(wr.getLastError().getErrorMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> List<T> queryForCollection(String collectionName, DocumentMapper<DBObject, T> mapper) {
|
||||
List<T> results = new ArrayList<T>();
|
||||
DBCollection collection = getDocumentStoreConnectionFactory()
|
||||
.getConnection()
|
||||
.getCollection(collectionName);
|
||||
for (DBObject dbo : collection.find()) {
|
||||
results.add(mapper.mapDocument(dbo));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user