INT-774 Added EL-based Splitter
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.integration.config;
|
||||
import org.springframework.integration.message.MessageHandler;
|
||||
import org.springframework.integration.splitter.AbstractMessageSplitter;
|
||||
import org.springframework.integration.splitter.DefaultMessageSplitter;
|
||||
import org.springframework.integration.splitter.ExpressionEvaluatingSplitter;
|
||||
import org.springframework.integration.splitter.MethodInvokingSplitter;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -39,6 +40,11 @@ public class SplitterFactoryBean extends AbstractMessageHandlerFactoryBean {
|
||||
: new MethodInvokingSplitter(targetObject);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MessageHandler createExpressionEvaluatingHandler(String expression) {
|
||||
return new ExpressionEvaluatingSplitter(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MessageHandler createDefaultHandler() {
|
||||
return new DefaultMessageSplitter();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2009 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.
|
||||
@@ -33,18 +33,41 @@ public class SplitterParser extends AbstractConsumerEndpointParser {
|
||||
|
||||
@Override
|
||||
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
|
||||
BeanDefinition innerDefinition = IntegrationNamespaceUtils.parseInnerHandlerDefinition(element, parserContext);
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||
IntegrationNamespaceUtils.BASE_PACKAGE + ".config.SplitterFactoryBean");
|
||||
if (innerDefinition != null){
|
||||
builder.addPropertyValue("targetObject", innerDefinition);
|
||||
} else if (element.hasAttribute(REF_ATTRIBUTE)) {
|
||||
String ref = element.getAttribute(REF_ATTRIBUTE);
|
||||
builder.addPropertyReference("targetObject", ref);
|
||||
if (StringUtils.hasText(element.getAttribute(METHOD_ATTRIBUTE))) {
|
||||
String method = element.getAttribute(METHOD_ATTRIBUTE);
|
||||
builder.addPropertyValue("targetMethodName", method);
|
||||
|
||||
BeanDefinition innerDefinition = IntegrationNamespaceUtils.parseInnerHandlerDefinition(element, parserContext);
|
||||
String ref = element.getAttribute(REF_ATTRIBUTE);
|
||||
String expression = element.getAttribute(EXPRESSION_ATTRIBUTE);
|
||||
boolean hasRef = StringUtils.hasText(ref);
|
||||
boolean hasExpression = StringUtils.hasText(expression);
|
||||
|
||||
if (innerDefinition != null) {
|
||||
if (hasRef || hasExpression) {
|
||||
parserContext.getReaderContext().error(
|
||||
"Neither 'ref' nor 'expression' are permitted when an inner bean (<bean/>) is configured.", element);
|
||||
return null;
|
||||
}
|
||||
builder.addPropertyValue("targetObject", innerDefinition);
|
||||
}
|
||||
else if (hasRef) {
|
||||
builder.addPropertyReference("targetObject", ref);
|
||||
}
|
||||
else if (hasExpression) {
|
||||
builder.addPropertyValue("expression", expression);
|
||||
}
|
||||
else {
|
||||
// will create a DefaultSplitter
|
||||
return builder;
|
||||
}
|
||||
|
||||
String method = element.getAttribute(METHOD_ATTRIBUTE);
|
||||
if (StringUtils.hasText(method)) {
|
||||
if (hasExpression) {
|
||||
parserContext.getReaderContext().error(
|
||||
"A 'method' attribute is not permitted when configuring an 'expression'.", element);
|
||||
}
|
||||
builder.addPropertyValue("targetMethodName", method);
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
@@ -36,16 +36,20 @@ import org.springframework.integration.message.MessageHandlingException;
|
||||
*/
|
||||
public class ExpressionEvaluatingMessageProcessor implements MessageProcessor {
|
||||
|
||||
private final ExpressionParser parser = new SpelExpressionParser(
|
||||
/*private final ExpressionParser parser = new SpelExpressionParser(
|
||||
SpelExpressionParserConfiguration.CreateObjectIfAttemptToReferenceNull |
|
||||
SpelExpressionParserConfiguration.GrowListsOnIndexBeyondSize);
|
||||
*/
|
||||
|
||||
private final Expression expression;
|
||||
|
||||
|
||||
public ExpressionEvaluatingMessageProcessor(String expression) {
|
||||
try {
|
||||
this.expression = this.parser.parseExpression(expression);
|
||||
ExpressionParser parser = new SpelExpressionParser(
|
||||
SpelExpressionParserConfiguration.CreateObjectIfAttemptToReferenceNull |
|
||||
SpelExpressionParserConfiguration.GrowListsOnIndexBeyondSize);
|
||||
this.expression = parser.parseExpression(expression);
|
||||
}
|
||||
catch (ParseException e) {
|
||||
throw new IllegalArgumentException("Failed to parse expression.", e);
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.splitter;
|
||||
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.handler.MessageProcessor;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Base class for Message Splitter implementations that delegate to a
|
||||
* {@link MessageProcessor} instance.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
abstract class AbstractMessageProcessingSplitter extends AbstractMessageSplitter {
|
||||
|
||||
private final MessageProcessor messageProcessor;
|
||||
|
||||
|
||||
protected AbstractMessageProcessingSplitter(MessageProcessor messageProcessor) {
|
||||
Assert.notNull(messageProcessor, "messageProcessor must not be null");
|
||||
this.messageProcessor = messageProcessor;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Object splitMessage(Message<?> message) {
|
||||
return this.messageProcessor.processMessage(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.splitter;
|
||||
|
||||
import org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor;
|
||||
|
||||
/**
|
||||
* A Message Splitter implementation that evaluates the specified SpEL
|
||||
* expression. The result of evaluation will typically be a Collection or
|
||||
* Array. If the result is not a Collection or Array, then the single Object
|
||||
* will be returned as the payload of a single reply Message.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
public class ExpressionEvaluatingSplitter extends AbstractMessageProcessingSplitter {
|
||||
|
||||
public ExpressionEvaluatingSplitter(String expression) {
|
||||
super(new ExpressionEvaluatingMessageProcessor(expression));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2009 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.
|
||||
@@ -19,7 +19,6 @@ package org.springframework.integration.splitter;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.integration.annotation.Splitter;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.handler.MessageMappingMethodInvoker;
|
||||
|
||||
/**
|
||||
@@ -31,27 +30,18 @@ import org.springframework.integration.handler.MessageMappingMethodInvoker;
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class MethodInvokingSplitter extends AbstractMessageSplitter {
|
||||
|
||||
private final MessageMappingMethodInvoker invoker;
|
||||
|
||||
public class MethodInvokingSplitter extends AbstractMessageProcessingSplitter {
|
||||
|
||||
public MethodInvokingSplitter(Object object, Method method) {
|
||||
this.invoker = new MessageMappingMethodInvoker(object, method);
|
||||
super(new MessageMappingMethodInvoker(object, method));
|
||||
}
|
||||
|
||||
public MethodInvokingSplitter(Object object, String methodName) {
|
||||
this.invoker = new MessageMappingMethodInvoker(object, methodName);
|
||||
super(new MessageMappingMethodInvoker(object, methodName));
|
||||
}
|
||||
|
||||
public MethodInvokingSplitter(Object object) {
|
||||
this.invoker = new MessageMappingMethodInvoker(object, Splitter.class);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Object splitMessage(Message<?> message) {
|
||||
return this.invoker.processMessage(message);
|
||||
super(new MessageMappingMethodInvoker(object, Splitter.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -959,19 +959,12 @@
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="transformer">
|
||||
<xsd:element name="transformer" type="expressionOrInnerEndpointDefinitionAware">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines a Transformer.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="innerEndpointDefinitionAware">
|
||||
<xsd:attribute name="expression" type="xsd:string"/>
|
||||
</xsd:extension>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="object-to-string-transformer">
|
||||
@@ -1205,7 +1198,7 @@
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:element name="splitter" type="innerEndpointDefinitionAware">
|
||||
<xsd:element name="splitter" type="expressionOrInnerEndpointDefinitionAware">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Defines a Splitter.
|
||||
@@ -1440,6 +1433,14 @@
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="expressionOrInnerEndpointDefinitionAware">
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="innerEndpointDefinitionAware">
|
||||
<xsd:attribute name="expression" type="xsd:string"/>
|
||||
</xsd:extension>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="innerEndpointDefinitionAware">
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="handlerEndpointType">
|
||||
|
||||
Reference in New Issue
Block a user