INT-1991 moved MongoDB module to the main branch

This commit is contained in:
Oleg Zhurakousky
2011-07-26 08:47:35 -04:00
parent ac9a1c7700
commit c26a1c6c6b
8 changed files with 591 additions and 0 deletions

View File

@@ -123,6 +123,8 @@ configure(javaprojects) {
mockitoVersion = '1.8.4'
springVersion = '3.1.0.M2'
springAmqpVersion = '1.0.0.RC2'
springDataMongoVersion = '1.0.0.BUILD-SNAPSHOT'
springDataCommonsVersion = '1.2.0.BUILD-SNAPSHOT'
springSecurityVersion = '3.0.5.RELEASE'
springWsVersion = '2.0.2.RELEASE'
@@ -321,6 +323,16 @@ project('spring-integration-mail') {
[compileJava,compileTestJava]*.options*.compilerArgs = ["${xLintArg},-path"]
}
project('spring-integration-mongodb') {
description = 'Spring Integration MongoDB Support'
dependencies {
compile project(":spring-integration-core")
compile "org.springframework:spring-context:$springVersion"
compile "org.springframework.data:spring-data-mongodb:$springDataMongoVersion"
compile "org.springframework.data:spring-data-commons-core:$springDataCommonsVersion"
}
}
project('spring-integration-rmi') {
description = 'Spring Integration RMI Support'
dependencies {

View File

@@ -8,6 +8,7 @@
<packaging>pom</packaging>
<name>Spring Integration</name>
<modules>
<module>spring-integration-amqp</module>
<module>spring-integration-core</module>
<module>spring-integration-event</module>
<module>spring-integration-feed</module>
@@ -20,6 +21,7 @@
<module>spring-integration-jms</module>
<module>spring-integration-jmx</module>
<module>spring-integration-mail</module>
<module>spring-integration-mongodb</module>
<module>spring-integration-rmi</module>
<module>spring-integration-security</module>
<module>spring-integration-sftp</module>

View File

@@ -30,6 +30,7 @@ include 'spring-integration-jdbc'
include 'spring-integration-jms'
include 'spring-integration-jmx'
include 'spring-integration-mail'
include 'spring-integration-mongodb'
include 'spring-integration-rmi'
include 'spring-integration-security'
include 'spring-integration-sftp'

View File

@@ -0,0 +1,173 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mongodb</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<name>Spring Integration MongoDB Support</name>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/java</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
<testResource>
<directory>src/test/resources</directory>
<includes>
<include>**/*</include>
</includes>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
<excludes>
<exclude>**/*Abstract*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>http://download.java.net/maven/2</id>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>http://maven.springframework.org/milestone</id>
<url>http://maven.springframework.org/milestone</url>
</repository>
<repository>
<id>http://maven.springframework.org/release</id>
<url>http://maven.springframework.org/release</url>
</repository>
<repository>
<id>http://maven.springframework.org/snapshot</id>
<url>http://maven.springframework.org/snapshot</url>
</repository>
<repository>
<id>http://repository.springsource.com/maven/bundles/external</id>
<url>http://repository.springsource.com/maven/bundles/external</url>
</repository>
<repository>
<id>http://repository.springsource.com/maven/bundles/milestone</id>
<url>http://repository.springsource.com/maven/bundles/milestone</url>
</repository>
<repository>
<id>http://repository.springsource.com/maven/bundles/release</id>
<url>http://repository.springsource.com/maven/bundles/release</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymockclassextension</artifactId>
<version>2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.0.M2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.0.M2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF8</project.build.sourceEncoding>
</properties>
</project>

View File

@@ -0,0 +1,197 @@
/*
* Copyright 2002-2011 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.integration.mongodb.store;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.bson.types.ObjectId;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.integration.Message;
import org.springframework.integration.MessageHeaders;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.store.MessageStore;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
/**
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public class MongoMessageStore implements MessageStore, BeanClassLoaderAware {
private final static String DEFAULT_COLLECTION_NAME = "messages";
private final MongoTemplate template;
//private final MongoConverter mongoConverter = new MessageReadingMongoConverter();
private final MongoConverter mongoConverter = null;
private volatile ClassLoader classLoader = ClassUtils.getDefaultClassLoader();
public MongoMessageStore(Mongo mongo, String databaseName) {
Assert.notNull(mongo, "mongo must not be null");
Assert.hasText(databaseName, "databaseName must not be empty");
MongoDbFactory mongoFactory = new SimpleMongoDbFactory(mongo, databaseName);
MessageReadingMongoConverter converter = new MessageReadingMongoConverter(mongoFactory, new MongoMappingContext());
this.template = new MongoTemplate(mongoFactory, converter);
// this.template.setDefaultCollectionName(DEFAULT_COLLECTION_NAME);
// this.template.createCollection(DEFAULT_COLLECTION_NAME);
}
public void setCollectionName(String collectionName) {
Assert.hasText(collectionName, "collectionName must not be empty");
//this.template.setDefaultCollectionName(collectionName);
this.template.createCollection(collectionName);
}
// public void setUsername(String username) {
// template.setUsername(username);
// }
//
// public void setPassword(String password) {
// template.setPassword(password);
// }
public void setBeanClassLoader(ClassLoader classLoader) {
Assert.notNull(classLoader, "classLoader must not be null");
this.classLoader = classLoader;
}
public <T> Message<T> addMessage(Message<T> message) {
this.template.insert(message, "messages");
return message;
}
public Message<?> getMessage(UUID id) {
return this.template.findOne(this.idQuery(id), Message.class);
}
public int getMessageCount() {
// TODO: long to int
return new Long(this.template.getCollection(DEFAULT_COLLECTION_NAME).getCount()).intValue();
}
public Message<?> removeMessage(UUID id) {
return this.template.findAndRemove(idQuery(id), Message.class, "messages");
}
private Query idQuery(UUID id) {
System.out.println(id.toString());
return new Query(where("_id").is(id.toString()));
}
private class MessageReadingMongoConverter extends MappingMongoConverter {
public MessageReadingMongoConverter(
MongoDbFactory mongoDbFactory,
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
super(mongoDbFactory, mappingContext);
// TODO Auto-generated constructor stub
}
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
Set<Converter<?, ?>> customConverters = new HashSet<Converter<?,?>>();
customConverters.add(new UuidToStringConverter());
customConverters.add(new StringToUuidConverter());
//this.setCustomConverters(customConverters);
}
@Override
public void write(Object source, DBObject target) {
if (source instanceof Message) {
String payloadType = ((Message<?>) source).getPayload().getClass().getName();
target.put("_payloadType", payloadType);
target.put("_id", ((Message<?>) source).getHeaders().getId().toString());
}
// // TODO: fix this (the base class should handle it via converters?)
// if (source instanceof UUID) {
// target.put("uuid", ((UUID)source).toString());
// }
super.write(source, target);
}
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public <S> S read(Class<S> clazz, DBObject source) {
if (!Message.class.equals(clazz)) {
return super.read(clazz, source);
}
Map<String, Object> headers = (Map<String, Object>) source.get("headers");
Object payload = source.get("payload");
Object payloadType = source.get("_payloadType");
if (payloadType != null && payload instanceof DBObject) {
try {
Class<?> payloadClass = ClassUtils.forName(payloadType.toString(), classLoader);
payload = this.read(payloadClass, (DBObject) payload);
}
catch (Exception e) {
throw new IllegalStateException("failed to load class: " + payloadType, e);
}
}
GenericMessage message = new GenericMessage(payload, headers);
Map innerMap = (Map) new DirectFieldAccessor(message.getHeaders()).getPropertyValue("headers");
// TODO: unpick this mess
innerMap.put(MessageHeaders.ID, UUID.fromString(source.get("_id").toString()));
innerMap.put(MessageHeaders.TIMESTAMP, headers.get(MessageHeaders.TIMESTAMP));
return (S) message;
}
}
private static class UuidToStringConverter implements Converter<UUID, String> {
public String convert(UUID source) {
return source.toString();
}
}
private static class StringToUuidConverter implements Converter<String, UUID> {
public UUID convert(String source) {
return UUID.fromString(source);
}
}
}

View File

@@ -0,0 +1,145 @@
/*
* Copyright 2002-2011 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.integration.mongodb.store;
import static org.junit.Assert.assertEquals;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.integration.Message;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.transformer.ClaimCheckInTransformer;
import org.springframework.integration.transformer.ClaimCheckOutTransformer;
import com.mongodb.Mongo;
/**
* @author Mark Fisher
*/
public class MongoClaimCheckIntegrationTests {
@Test @Ignore
public void stringPayload() throws Exception {
Mongo mongo = new Mongo();
MongoMessageStore messageStore = new MongoMessageStore(mongo, "test");
ClaimCheckInTransformer checkin = new ClaimCheckInTransformer(messageStore);
ClaimCheckOutTransformer checkout = new ClaimCheckOutTransformer(messageStore);
Message<?> originalMessage = MessageBuilder.withPayload("test1").build();
Message<?> claimCheckMessage = checkin.transform(originalMessage);
assertEquals(originalMessage.getHeaders().getId(), claimCheckMessage.getPayload());
Message<?> checkedOutMessage = checkout.transform(claimCheckMessage);
assertEquals(claimCheckMessage.getPayload(), checkedOutMessage.getHeaders().getId());
assertEquals(originalMessage.getPayload(), checkedOutMessage.getPayload());
assertEquals(originalMessage, checkedOutMessage);
//System.out.println("original: " + originalMessage);
//System.out.println("claimcheck: " + claimCheckMessage);
//System.out.println("checkedout: " + checkedOutMessage);
}
@Test @Ignore
public void objectPayload() throws Exception {
Mongo mongo = new Mongo();
MongoMessageStore messageStore = new MongoMessageStore(mongo, "test");
ClaimCheckInTransformer checkin = new ClaimCheckInTransformer(messageStore);
ClaimCheckOutTransformer checkout = new ClaimCheckOutTransformer(messageStore);
Beverage payload = new Beverage();
payload.setName("latte");
payload.setShots(3);
payload.setIced(false);
Message<?> originalMessage = MessageBuilder.withPayload(payload).build();
Message<?> claimCheckMessage = checkin.transform(originalMessage);
assertEquals(originalMessage.getHeaders().getId(), claimCheckMessage.getPayload());
Message<?> checkedOutMessage = checkout.transform(claimCheckMessage);
assertEquals(originalMessage.getPayload(), checkedOutMessage.getPayload());
assertEquals(claimCheckMessage.getPayload(), checkedOutMessage.getHeaders().getId());
assertEquals(originalMessage, checkedOutMessage);
}
@SuppressWarnings("unused")
private static class Beverage {
private String name;
private int shots;
private boolean iced;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getShots() {
return shots;
}
public void setShots(int shots) {
this.shots = shots;
}
public boolean isIced() {
return iced;
}
public void setIced(boolean iced) {
this.iced = iced;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (iced ? 1231 : 1237);
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + shots;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Beverage other = (Beverage) obj;
if (iced != other.iced) {
return false;
}
if (name == null) {
if (other.name != null) {
return false;
}
}
else if (!name.equals(other.name)) {
return false;
}
if (shots != other.shots) {
return false;
}
return true;
}
}
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2002-2011 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.integration.mongodb.store;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.integration.Message;
import org.springframework.integration.support.MessageBuilder;
import com.mongodb.Mongo;
/**
* @author Mark Fisher
*/
public class MongoMessageStoreTests {
@Test @Ignore
public void addAndRemove() throws Exception {
Mongo mongo = new Mongo();
MongoMessageStore store = new MongoMessageStore(mongo, "test");
Message<?> message = MessageBuilder.withPayload("UUID again and again").build();
Message<?> claimCheck = store.addMessage(message);
System.out.println("added: " + claimCheck);
//Thread.sleep(10000);
message = store.removeMessage(message.getHeaders().getId());
System.out.println("removed: " + message);
}
}

View File

@@ -0,0 +1,17 @@
Bundle-SymbolicName: org.springframework.integration.mongodb
Bundle-Name: Spring Integration MongoDB Support
Bundle-Vendor: SpringSource
Bundle-ManifestVersion: 2
Import-Template:
org.apache.commons.logging;version="[1.1.1, 2.0.0)",
org.springframework.integration.*;version="[2.0.0, 2.0.1)",
org.springframework.beans.*;version="[3.0.0, 4.0.0)",
org.springframework.context;version="[3.0.0, 4.0.0)",
org.springframework.core.*;version="[3.0.0, 4.0.0)",
org.springframework.expression.*;version="[3.0.0, 4.0.0)",
org.springframework.util;version="[3.0.0, 4.0.0)",
org.springframework.expression.*;version="[3.0.0, 4.0.0)",
org.springframework.data.document.mongodb.*;version="[1.0.0, 2.0.0)",
com.mongodb.*;version="[2.4.0, 2.5.0]",
javax.*;version="0",
org.w3c.dom.*;version="0"