INT-2612 Add MongoDb Adapters
Initial support for MongoDb adapters based on initial work done by Amol Nayak INT-2612b Add namespace support Add entityClass and expectSingleResult attribiutes to MongoDbMessageSource INT-2612 Polishing Mostly JavaDocs and a few asserts; plus renamed mongodb-template to mongo-template. INT-2612 Remove 'store-' Prefix from Elements INT-2612 Polish Tests Add tests for mongo-converter pass and fail when template present INT-2612 Add missing files INT-2612 Polishing INT-2612 Add @MongoDbAvailable to parser tests INT-2612b Add dedicated parser tests
This commit is contained in:
committed by
Gary Russell
parent
a333d33112
commit
bd51b3ee7a
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.config;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.BeanMetadataElement;
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.config.xml.AbstractPollingInboundChannelAdapterParser;
|
||||
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
|
||||
import org.springframework.integration.mongodb.inbound.MongoDbMessageSource;
|
||||
/**
|
||||
* Parser for Mongodb store inbound adapters
|
||||
*
|
||||
* @author Amol Nayak
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.2
|
||||
*/
|
||||
public class MongoDbInboundChannelAdapterParser extends AbstractPollingInboundChannelAdapterParser {
|
||||
|
||||
@Override
|
||||
protected BeanMetadataElement parseSource(Element element, ParserContext parserContext) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(MongoDbMessageSource.class);
|
||||
|
||||
// Will parse and validate 'mongodb-template', 'mongodb-factory',
|
||||
// 'collection-name', 'collection-name-expression' and 'mongo-converter'
|
||||
MongoParserUtils.processCommonAttributes(element, parserContext, builder);
|
||||
|
||||
RootBeanDefinition queryExpressionDef =
|
||||
IntegrationNamespaceUtils.createExpressionDefinitionFromValueOrExpression("query", "query-expression",
|
||||
parserContext, element, true);
|
||||
|
||||
builder.addConstructorArgValue(queryExpressionDef);
|
||||
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "entity-class");
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "expect-single-result");
|
||||
|
||||
String beanName = BeanDefinitionReaderUtils.registerWithGeneratedName(
|
||||
builder.getBeanDefinition(), parserContext.getRegistry());
|
||||
return new RuntimeBeanReference(beanName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.config;
|
||||
|
||||
import org.springframework.integration.config.xml.AbstractIntegrationNamespaceHandler;
|
||||
|
||||
/**
|
||||
* Namespace handler for Spring Integration's 'mongodb' namespace.
|
||||
*
|
||||
* @author Amol Nayak
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
* @since 2.2
|
||||
*/
|
||||
public class MongoDbNamespaceHandler extends AbstractIntegrationNamespaceHandler {
|
||||
|
||||
public void init() {
|
||||
registerBeanDefinitionParser("inbound-channel-adapter", new MongoDbInboundChannelAdapterParser());
|
||||
registerBeanDefinitionParser("outbound-channel-adapter", new MongoDbOutboundChannelAdapterParser());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2007-2012 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.config;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.config.xml.AbstractOutboundChannelAdapterParser;
|
||||
import org.springframework.integration.mongodb.outbound.MongoDbStoringMessageHandler;
|
||||
/**
|
||||
* Parser for Mongodb store outbound adapters
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.2
|
||||
*/
|
||||
public class MongoDbOutboundChannelAdapterParser extends AbstractOutboundChannelAdapterParser {
|
||||
|
||||
@Override
|
||||
protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MongoDbStoringMessageHandler.class);
|
||||
|
||||
// Will parse and validate 'mongodb-template', 'mongodb-factory',
|
||||
// 'collection-name', 'collection-name-expression' and 'mongo-converter'
|
||||
MongoParserUtils.processCommonAttributes(element, parserContext, builder);
|
||||
|
||||
return builder.getBeanDefinition();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.config;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.w3c.dom.Element;
|
||||
/**
|
||||
* Utility class used by mongo parsers
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
* @author Gary Russell
|
||||
* @since 2.2
|
||||
*/
|
||||
class MongoParserUtils {
|
||||
|
||||
/**
|
||||
* Will parse and validate
|
||||
* 'mongodb-template', 'mongodb-factory', 'collection-name', 'collection-name-expression' and 'mongo-converter'
|
||||
*
|
||||
* @param element
|
||||
* @param parserContext
|
||||
* @param builder
|
||||
*/
|
||||
public static void processCommonAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder){
|
||||
String mongoDbTemplate = element.getAttribute("mongo-template");
|
||||
String mongoDbFactory = element.getAttribute("mongodb-factory");
|
||||
|
||||
if (StringUtils.hasText(mongoDbTemplate) && StringUtils.hasText(mongoDbFactory)){
|
||||
parserContext.getReaderContext().error("Only one of '" + mongoDbTemplate + "' or '"
|
||||
+ mongoDbFactory + "' is allowed", element);
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(mongoDbTemplate)){
|
||||
builder.addConstructorArgReference(mongoDbTemplate);
|
||||
if (StringUtils.hasText(element.getAttribute("mongo-converter"))) {
|
||||
parserContext.getReaderContext().error("'mongo-converter' is not allowed with 'mongo-template'",
|
||||
element);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!StringUtils.hasText(mongoDbFactory)) {
|
||||
mongoDbFactory = "mongoDbFactory";
|
||||
}
|
||||
builder.addConstructorArgReference(mongoDbFactory);
|
||||
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "mongo-converter");
|
||||
}
|
||||
|
||||
RootBeanDefinition collectionNameExpressionDef =
|
||||
IntegrationNamespaceUtils.createExpressionDefinitionFromValueOrExpression("collection-name", "collection-name-expression",
|
||||
parserContext, element, false);
|
||||
|
||||
|
||||
if (collectionNameExpressionDef != null){
|
||||
builder.addPropertyValue("collectionNameExpression", collectionNameExpressionDef);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Contains parser classes for the MongoDb namespace support.
|
||||
*/
|
||||
package org.springframework.integration.mongodb.config;
|
||||
@@ -0,0 +1,243 @@
|
||||
/*
|
||||
* Copyright 2007-2012 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.inbound;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.data.mongodb.MongoDbFactory;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverter;
|
||||
import org.springframework.data.mongodb.core.query.BasicQuery;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.common.LiteralExpression;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.context.IntegrationObjectSupport;
|
||||
import org.springframework.integration.core.PseudoTransactionalMessageSource;
|
||||
import org.springframework.integration.mongodb.support.MongoHeaders;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.util.ExpressionUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
/**
|
||||
* An instance of {@link MessageSource} which returns a {@link Message} with a payload
|
||||
* which is the result of execution of a {@link Query}. When expectSingleResult is false
|
||||
* (default),
|
||||
* the MongoDb {@link Query} is executed using {@link MongoOperations#find(Query, Class)}
|
||||
* method which returns a {@link List}. The returned {@link List} will be used as
|
||||
* the payoad of the {@link Message} returned by the {{@link #receive()} method.
|
||||
* An empty {@link List} is treated as null, thus resulting in no {@link Message} returned
|
||||
* by the {{@link #receive()} method.
|
||||
* <p>
|
||||
* When expectSingleResult is true, the {@link MongoOperations#findOne(Query, Class)} is
|
||||
* used instead, and the message payload will be the single object returned from the
|
||||
* query.
|
||||
*
|
||||
* @author Amol Nayak
|
||||
* @author Oleg Zhurakousky
|
||||
*
|
||||
* @since 2.2
|
||||
*/
|
||||
public class MongoDbMessageSource extends IntegrationObjectSupport
|
||||
implements PseudoTransactionalMessageSource<Object, MongoOperations>{
|
||||
|
||||
private final Expression queryExpression;
|
||||
|
||||
private volatile Expression collectionNameExpression = new LiteralExpression("data");
|
||||
|
||||
private volatile StandardEvaluationContext evaluationContext;
|
||||
|
||||
private volatile MongoOperations mongoTemplate;
|
||||
|
||||
private volatile MongoConverter mongoConverter;
|
||||
|
||||
private volatile MongoDbFactory mongoDbFactory;
|
||||
|
||||
private volatile boolean initialized = false;
|
||||
|
||||
private volatile Class<?> entityClass = DBObject.class;
|
||||
|
||||
private volatile boolean expectSingleResult = false;
|
||||
|
||||
/**
|
||||
* Creates an instance with the provided {@link MongoDbFactory} and SpEL expression
|
||||
* which should resolve to a MongoDb 'query' string
|
||||
* (see http://www.mongodb.org/display/DOCS/Querying).
|
||||
* The 'queryExpression' will be evaluated on every call to the {@link #receive()} method.
|
||||
*
|
||||
* @param mongoDbFactory
|
||||
* @param queryExpression
|
||||
*/
|
||||
public MongoDbMessageSource(MongoDbFactory mongoDbFactory, Expression queryExpression){
|
||||
Assert.notNull(mongoDbFactory, "'mongoDbFactory' must not be null");
|
||||
Assert.notNull(queryExpression, "'queryExpression' must not be null");
|
||||
|
||||
this.mongoDbFactory = mongoDbFactory;
|
||||
this.queryExpression = queryExpression;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance with the provided {@link MongoOperations} and SpEL expression
|
||||
* which should resolve to a Mongo 'query' string
|
||||
* (see http://www.mongodb.org/display/DOCS/Querying).
|
||||
* It assumes that the {@link MongoOperations} is fully initialized and ready to be used.
|
||||
* The 'queryExpression' will be evaluated on every call to the {@link #receive()} method.
|
||||
*
|
||||
* @param mongoTemplate
|
||||
* @param queryExpression
|
||||
*/
|
||||
public MongoDbMessageSource(MongoOperations mongoTemplate, Expression queryExpression){
|
||||
Assert.notNull(mongoTemplate, "'mongoTemplate' must not be null");
|
||||
Assert.notNull(queryExpression, "'queryExpression' must not be null");
|
||||
|
||||
this.mongoTemplate = mongoTemplate;
|
||||
this.queryExpression = queryExpression;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows you to set the type of the entityClass that will be passed to the
|
||||
* {@link MongoTemplate#find(Query, Class)} or {@link MongoTemplate#findOne(Query, Class)}
|
||||
* method.
|
||||
* Default is {@link DBObject}.
|
||||
*
|
||||
* @param entityClass
|
||||
*/
|
||||
public void setEntityClass(Class<?> entityClass) {
|
||||
Assert.notNull(entityClass, "'entityClass' must not be null");
|
||||
this.entityClass = entityClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows you to manage which find* method to invoke on {@link MongoTemplate}.
|
||||
* Default is 'false', which means the {@link #receive()} method will use
|
||||
* the {@link MongoTemplate#find(Query, Class)} method. If set to 'true',
|
||||
* {@link #receive()} will use {@link MongoTemplate#findOne(Query, Class)},
|
||||
* and the payload of the returned {@link Message} will be the returned target Object of type
|
||||
* identified by {{@link #entityClass} instead of a List.
|
||||
*
|
||||
* @param expectSingleResult
|
||||
*/
|
||||
public void setExpectSingleResult(boolean expectSingleResult) {
|
||||
this.expectSingleResult = expectSingleResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the SpEL {@link Expression} that should resolve to a collection name
|
||||
* used by the {@link Query}. The resulting collection name will be included
|
||||
* in the {@link MongoHeaders#COLLECTION_NAME} header.
|
||||
*
|
||||
* @param collectionNameExpression
|
||||
*/
|
||||
public void setCollectionNameExpression(Expression collectionNameExpression) {
|
||||
Assert.notNull(collectionNameExpression, "'collectionNameExpression' must not be null");
|
||||
this.collectionNameExpression = collectionNameExpression;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows you to provide a custom {@link MongoConverter} used to assist in deserialization
|
||||
* data read from MongoDb. Only allowed if this instance was constructed with a
|
||||
* {@link MongoDbFactory}.
|
||||
*
|
||||
* @param mongoConverter
|
||||
*/
|
||||
public void setMongoConverter(MongoConverter mongoConverter) {
|
||||
Assert.isNull(this.mongoTemplate,
|
||||
"'mongoConverter' can not be set when instance was constructed with MongoTemplate");
|
||||
this.mongoConverter = mongoConverter;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onInit() throws Exception {
|
||||
if (this.getBeanFactory() != null){
|
||||
this.evaluationContext =
|
||||
ExpressionUtils.createStandardEvaluationContext(this.getBeanFactory());
|
||||
}
|
||||
else {
|
||||
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext();
|
||||
}
|
||||
|
||||
if (this.mongoTemplate == null){
|
||||
this.mongoTemplate = new MongoTemplate(this.mongoDbFactory, this.mongoConverter);
|
||||
}
|
||||
this.initialized = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Will execute a {@link Query} returning its results as the Message payload.
|
||||
* The payload can be either {@link List} of elements of objects of type
|
||||
* identified by {{@link #entityClass}, or a single element of type identified by {{@link #entityClass}
|
||||
* based on the value of {{@link #expectSingleResult} attribute which defaults to 'false' resulting
|
||||
* {@link Message} with payload of type {@link List}. The collection name used in the
|
||||
* query will be provided in the {@link MongoHeaders#COLLECTION_NAME} header.
|
||||
*/
|
||||
public Message<Object> receive() {
|
||||
Assert.isTrue(this.initialized, "This class is not yet initialized. Invoke its afterPropertiesSet() method");
|
||||
Message<Object> message = null;
|
||||
Query query = new BasicQuery(this.queryExpression.getValue(this.evaluationContext, String.class));
|
||||
Assert.notNull(query, "'queryExpression' must not evaluate to null");
|
||||
String collectionName = this.collectionNameExpression.getValue(this.evaluationContext, String.class);
|
||||
Assert.notNull(collectionName, "'collectionNameExpression' must not evaluate to null");
|
||||
|
||||
Object result = null;
|
||||
if (this.expectSingleResult){
|
||||
result = this.mongoTemplate.
|
||||
findOne(query, this.entityClass, collectionName);
|
||||
}
|
||||
else {
|
||||
List<?> results = this.mongoTemplate.
|
||||
find(query, this.entityClass, collectionName);
|
||||
if (!CollectionUtils.isEmpty(results)){
|
||||
result = results;
|
||||
}
|
||||
}
|
||||
if (result != null){
|
||||
message = MessageBuilder.withPayload(result)
|
||||
.setHeader(MongoHeaders.COLLECTION_NAME, collectionName)
|
||||
.build();
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mongo-template.
|
||||
*/
|
||||
public MongoOperations getResource() {
|
||||
return this.mongoTemplate;
|
||||
}
|
||||
|
||||
public void afterCommit(Object object) {
|
||||
|
||||
}
|
||||
|
||||
public void afterRollback(Object object) {
|
||||
|
||||
}
|
||||
|
||||
public void afterReceiveNoTx(MongoOperations resource) {
|
||||
|
||||
}
|
||||
|
||||
public void afterSendNoTx(MongoOperations resource) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Provides classes related to the Mongo inbound channel adapters
|
||||
*/
|
||||
package org.springframework.integration.mongodb.inbound;
|
||||
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Copyright 2007-2012 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.outbound;
|
||||
|
||||
import org.springframework.data.mongodb.MongoDbFactory;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverter;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.common.LiteralExpression;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.core.MessageHandler;
|
||||
import org.springframework.integration.handler.AbstractMessageHandler;
|
||||
import org.springframework.integration.util.ExpressionUtils;
|
||||
import org.springframework.util.Assert;
|
||||
/**
|
||||
* Implementation of {@link MessageHandler} which writes Message payload into a MongoDb collection
|
||||
* identified by evaluation of the {@link #collectionNameExpression}.
|
||||
*
|
||||
* @author Amol Nayak
|
||||
* @author Oleg Zhurakousky
|
||||
* @since 2.2
|
||||
*
|
||||
*/
|
||||
public class MongoDbStoringMessageHandler extends AbstractMessageHandler {
|
||||
|
||||
private volatile MongoOperations mongoTemplate;
|
||||
|
||||
private volatile MongoDbFactory mongoDbFactory;
|
||||
|
||||
private volatile MongoConverter mongoConverter;
|
||||
|
||||
private volatile StandardEvaluationContext evaluationContext;
|
||||
|
||||
private volatile Expression collectionNameExpression = new LiteralExpression("data");
|
||||
|
||||
private volatile boolean initialized = false;
|
||||
|
||||
/**
|
||||
* Will construct this instance using provided {@link MongoDbFactory}
|
||||
*
|
||||
* @param mongoDbFactory
|
||||
*/
|
||||
public MongoDbStoringMessageHandler(MongoDbFactory mongoDbFactory){
|
||||
Assert.notNull(mongoDbFactory, "'mongoDbFactory' must not be null");
|
||||
|
||||
this.mongoDbFactory = mongoDbFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Will construct this instance using fully created and initialized instance of
|
||||
* provided {@link MongoOperations}
|
||||
*
|
||||
* @param mongoTemplate
|
||||
*/
|
||||
public MongoDbStoringMessageHandler(MongoOperations mongoTemplate){
|
||||
Assert.notNull(mongoTemplate, "'mongoTemplate' must not be null");
|
||||
|
||||
this.mongoTemplate = mongoTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows you to provide custom {@link MongoConverter} used to assist in serialization
|
||||
* of data written to MongoDb. Only allowed if this instance was constructed with a
|
||||
* {@link MongoDbFactory}.
|
||||
*
|
||||
* @param mongoConverter
|
||||
*/
|
||||
public void setMongoConverter(MongoConverter mongoConverter) {
|
||||
Assert.isNull(this.mongoTemplate,
|
||||
"'mongoConverter' can not be set when instance was constructed with MongoTemplate");
|
||||
this.mongoConverter = mongoConverter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the SpEL {@link Expression} that should resolve to a collection name
|
||||
* used by {@link MongoOperations} to store data
|
||||
*
|
||||
* @param collectionNameExpression
|
||||
*/
|
||||
public void setCollectionNameExpression(Expression collectionNameExpression) {
|
||||
Assert.notNull(collectionNameExpression, "'collectionNameExpression' must not be null");
|
||||
this.collectionNameExpression = collectionNameExpression;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onInit() throws Exception {
|
||||
if (this.getBeanFactory() != null) {
|
||||
this.evaluationContext =
|
||||
ExpressionUtils.createStandardEvaluationContext(this.getBeanFactory());
|
||||
}
|
||||
else {
|
||||
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext();
|
||||
}
|
||||
if (this.mongoTemplate == null){
|
||||
this.mongoTemplate = new MongoTemplate(this.mongoDbFactory, this.mongoConverter);
|
||||
}
|
||||
this.initialized = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleMessageInternal(Message<?> message) throws Exception {
|
||||
Assert.isTrue(this.initialized, "This class is not yet initialized. Invoke its afterPropertiesSet() method");
|
||||
String collectionName = this.collectionNameExpression.getValue(this.evaluationContext, message, String.class);
|
||||
Assert.notNull(collectionName, "'collectionNameExpression' must not evaluate to null");
|
||||
|
||||
Object payload = message.getPayload();
|
||||
|
||||
this.mongoTemplate.save(payload, collectionName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Provides classes related to the Mongo outbound channel adapters
|
||||
*/
|
||||
package org.springframework.integration.mongodb.outbound;
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.springframework.integration.mongodb.support;
|
||||
|
||||
/**
|
||||
* Pre-defined names and prefixes to be used for
|
||||
* for dealing with headers required by Mongo components
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 2.2
|
||||
*/
|
||||
public class MongoHeaders {
|
||||
|
||||
public static final String PREFIX = "mongo_";
|
||||
|
||||
public static final String COLLECTION_NAME = PREFIX + "collectionName";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Provides supporting classes for this module.
|
||||
*/
|
||||
package org.springframework.integration.mongodb.support;
|
||||
@@ -0,0 +1 @@
|
||||
http\://www.springframework.org/schema/integration/mongodb=org.springframework.integration.mongodb.config.MongoDbNamespaceHandler
|
||||
@@ -0,0 +1,2 @@
|
||||
http\://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb-2.2.xsd=org/springframework/integration/mongodb/config/spring-integration-mongodb-2.2.xsd
|
||||
http\://www.springframework.org/schema/integration/mongodb/spring-integration-mongodb.xsd=org/springframework/integration/mongodb/config/spring-integration-mongodb-2.2.xsd
|
||||
@@ -0,0 +1,4 @@
|
||||
# Tooling related information for the integration mongodb namespace
|
||||
http\://www.springframework.org/schema/integration/mongodb@name=integration mongodb Namespace
|
||||
http\://www.springframework.org/schema/integration/mongodb@prefix=int-mongodb
|
||||
http\://www.springframework.org/schema/integration/mongodb@icon=org/springframework/integration/mongodb/config/spring-integration-mongodb.gif
|
||||
@@ -0,0 +1,184 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsd:schema xmlns="http://www.springframework.org/schema/integration/mongodb"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:tool="http://www.springframework.org/schema/tool"
|
||||
xmlns:integration="http://www.springframework.org/schema/integration"
|
||||
targetNamespace="http://www.springframework.org/schema/integration/mongodb"
|
||||
elementFormDefault="qualified" attributeFormDefault="unqualified">
|
||||
|
||||
<xsd:import namespace="http://www.springframework.org/schema/beans" />
|
||||
<xsd:import namespace="http://www.springframework.org/schema/integration"
|
||||
schemaLocation="http://www.springframework.org/schema/integration/spring-integration-2.2.xsd" />
|
||||
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Defines the configuration elements for Spring Integration mongodb Adapters.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
|
||||
<xsd:element name="inbound-channel-adapter">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines mongodb inbound channel adapter that
|
||||
creates
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="mongodbAdapterType">
|
||||
<xsd:sequence>
|
||||
<xsd:element ref="integration:poller" minOccurs="0" maxOccurs="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="query" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
String representation of MongoDb Query (e.g.,
|
||||
query="{'name' : 'Bob'}").
|
||||
Please refer to MongoDb documentation for more query samples
|
||||
http://www.mongodb.org/display/DOCS/Querying
|
||||
This attribute is
|
||||
mutually exclusive with 'query-expression' attribute.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="query-expression" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
SpEL expression which should resolve to a
|
||||
String value identifying the
|
||||
MongoDb Query. Also see 'query' attribute
|
||||
This attribute is mutually exclusive with 'query' attribute.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="entity-class" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<xsd:documentation>
|
||||
The fully qualified name of the entity class to be passed to
|
||||
find(..) or findOne(..) method MongoTemplate.
|
||||
If this attribute is not provided the default value is com.mongodb.DBObject
|
||||
</xsd:documentation>
|
||||
<tool:annotation kind="direct">
|
||||
<tool:expected-type type="java.lang.Class" />
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="expect-single-result" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Allows you to manage find* method of MongoTemplate is used to query MongoDb. Default value for this
|
||||
attribute is 'false'. This means that we'll use find(..) method thus resulting in a Message with
|
||||
payload of type List of entities identified by 'entity-class' attribute. If you want/expect a single
|
||||
value set this attribute to 'true' which will result in invocation of findOne(..) method resulting in
|
||||
the payload of type identified by 'entity-class' attribute (default com.mongodb.DBObject)
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:extension>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="outbound-channel-adapter">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines mongodb outbound channel adapter that
|
||||
writes the contents of the
|
||||
Message into
|
||||
org.springframework.data.mongodb.support.collections.mongodbStore
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="mongodbAdapterType" />
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:complexType name="mongodbAdapterType">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Common configuration for mongodb adapters.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:attribute name="id" type="xsd:string" />
|
||||
<xsd:attribute name="channel" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Reference to a Message Channel that will be used
|
||||
by this adapter to
|
||||
'receiveFrom' or 'sendTo' Messages depending on the adapter type (e.g.,
|
||||
inbound/outbound)
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="mongodb-factory" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<xsd:documentation>
|
||||
Reference to an instance of
|
||||
org.springframework.data.mongodb.MongoDbFactory
|
||||
</xsd:documentation>
|
||||
<tool:expected-type
|
||||
type="org.springframework.data.mongodb.MongoDbFactory" />
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="mongo-template" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<xsd:documentation>
|
||||
Reference to an instance of
|
||||
org.springframework.data.mongodb.core.MongoTemplate
|
||||
</xsd:documentation>
|
||||
<tool:expected-type
|
||||
type="org.springframework.data.mongodb.core.MongoTemplate" />
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="collection-name" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Identifies the name of the MongoDb collection to
|
||||
use.
|
||||
This attribute is mutually exclusive with
|
||||
'collection-name-expression'
|
||||
attribute.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="collection-name-expression" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
SpEL expression which should resolve to a String
|
||||
value identifying the
|
||||
name of the MongoDb collection to use.
|
||||
This
|
||||
attribute is mutually exclusive with 'collection-name' attribute.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="mongo-converter" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation kind="ref">
|
||||
<tool:expected-type
|
||||
type="org.springframework.data.mongodb.core.convert.MongoConverter" />
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
<xsd:documentation>
|
||||
Reference to an instance of
|
||||
org.springframework.data.mongodb.core.convert.MongoConverter
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="auto-startup" type="xsd:string"
|
||||
default="true" />
|
||||
</xsd:complexType>
|
||||
</xsd:schema>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 552 B |
Reference in New Issue
Block a user