fix & refactor DefaultInboundChannelAdapterParser, plus some improvements
This commit is contained in:
Artem Bilan
2011-11-13 16:31:50 +02:00
committed by Mark Fisher
parent df440d5059
commit 785d10263c
11 changed files with 248 additions and 153 deletions

View File

@@ -18,6 +18,10 @@ package org.springframework.integration.config.xml;
import java.util.List;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.integration.config.ExpressionFactoryBean;
import org.springframework.integration.endpoint.ExpressionEvaluatingMessageSource;
import org.springframework.integration.endpoint.MethodInvokingMessageSource;
import org.w3c.dom.Element;
import org.springframework.beans.BeanMetadataElement;
@@ -34,41 +38,49 @@ import org.springframework.util.xml.DomUtils;
/**
* Parser for the <inbound-channel-adapter/> element.
*
*
* @author Mark Fisher
* @author Artem Bilan
*/
public class MethodInvokingInboundChannelAdapterParser extends AbstractPollingInboundChannelAdapterParser {
public class DefaultInboundChannelAdapterParser extends AbstractPollingInboundChannelAdapterParser {
@Override
protected BeanMetadataElement parseSource(Element element, ParserContext parserContext) {
BeanMetadataElement result = null;
BeanComponentDefinition innnerBeanDef = IntegrationNamespaceUtils.parseInnerHandlerDefinition(element, parserContext);
String sourceRef = element.getAttribute("ref");
String methodName = element.getAttribute("method");
String expressionString = element.getAttribute("expression");
if (innnerBeanDef != null) {
if (StringUtils.hasText(sourceRef)) {
parserContext.getReaderContext().error(
"inner bean and a 'ref' attribute are mutually exclusive options", element);
}
if (StringUtils.hasText(methodName)) {
String sourceRef = element.getAttribute(IntegrationNamespaceUtils.REF_ATTRIBUTE);
String methodName = element.getAttribute(IntegrationNamespaceUtils.METHOD_ATTRIBUTE);
String expressionString = element.getAttribute(IntegrationNamespaceUtils.EXPRESSION_ATTRIBUTE);
boolean isInnerDef = innnerBeanDef != null;
boolean isRef = StringUtils.hasText(sourceRef);
boolean isExpression = StringUtils.hasText(expressionString);
boolean hasMethod = StringUtils.hasText(methodName);
if (!(isInnerDef ^ (isRef ^ isExpression))) {
parserContext.getReaderContext().error(
"Exactly one of the 'ref', 'expression' or inner bean is required.", element);
}
if (isInnerDef) {
if (hasMethod) {
result = this.parseMethodInvokingSource(innnerBeanDef, methodName, element, parserContext);
}
else {
result = innnerBeanDef;
}
}
else if (StringUtils.hasText(expressionString)) {
if (StringUtils.hasText(sourceRef)) {
else if (isExpression) {
if (hasMethod) {
parserContext.getReaderContext().error(
"the 'expression' and 'ref' attributes are mutually exclusive options", element);
"The 'method' attribute can't be used with 'expression' attribute.", element);
}
String expressionBeanName = this.parseExpression(expressionString, element, parserContext);
result = new RuntimeBeanReference(expressionBeanName);
}
else if (StringUtils.hasText(sourceRef)) {
BeanMetadataElement sourceValue = new RuntimeBeanReference(sourceRef);
if (StringUtils.hasText(methodName)) {
else if (isRef) {
BeanMetadataElement sourceValue = new RuntimeBeanReference(sourceRef);
if (hasMethod) {
result = this.parseMethodInvokingSource(sourceValue, methodName, element, parserContext);
}
else {
@@ -83,8 +95,7 @@ public class MethodInvokingInboundChannelAdapterParser extends AbstractPollingIn
}
private BeanMetadataElement parseMethodInvokingSource(BeanMetadataElement targetObject, String methodName, Element element, ParserContext parserContext) {
BeanDefinitionBuilder sourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(
IntegrationNamespaceUtils.BASE_PACKAGE + ".endpoint.MethodInvokingMessageSource");
BeanDefinitionBuilder sourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(MethodInvokingMessageSource.class);
sourceBuilder.addPropertyValue("object", targetObject);
sourceBuilder.addPropertyValue("methodName", methodName);
this.parseHeaderExpressions(sourceBuilder, element, parserContext);
@@ -94,12 +105,11 @@ public class MethodInvokingInboundChannelAdapterParser extends AbstractPollingIn
}
private String parseExpression(String expressionString, Element element, ParserContext parserContext) {
BeanDefinitionBuilder sourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(
"org.springframework.integration.endpoint.ExpressionEvaluatingMessageSource");
RootBeanDefinition expressionDef = new RootBeanDefinition("org.springframework.integration.config.ExpressionFactoryBean");
BeanDefinitionBuilder sourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(ExpressionEvaluatingMessageSource.class);
RootBeanDefinition expressionDef = new RootBeanDefinition(ExpressionFactoryBean.class);
expressionDef.getConstructorArgumentValues().addGenericArgumentValue(expressionString);
sourceBuilder.addConstructorArgValue(expressionDef);
sourceBuilder.addConstructorArgValue(null); // TODO: add support for expectedType?
sourceBuilder.addConstructorArgValue(null);
this.parseHeaderExpressions(sourceBuilder, element, parserContext);
return BeanDefinitionReaderUtils.registerWithGeneratedName(sourceBuilder.getBeanDefinition(), parserContext.getRegistry());
}
@@ -121,11 +131,11 @@ public class MethodInvokingInboundChannelAdapterParser extends AbstractPollingIn
}
RootBeanDefinition expressionDef = null;
if (hasValue) {
expressionDef = new RootBeanDefinition("org.springframework.expression.common.LiteralExpression");
expressionDef = new RootBeanDefinition(LiteralExpression.class);
expressionDef.getConstructorArgumentValues().addGenericArgumentValue(headerValue);
}
else {
expressionDef = new RootBeanDefinition("org.springframework.integration.config.ExpressionFactoryBean");
expressionDef = new RootBeanDefinition(ExpressionFactoryBean.class);
expressionDef.getConstructorArgumentValues().addGenericArgumentValue(headerExpression);
}
headerExpressions.put(headerName, expressionDef);

View File

@@ -0,0 +1,105 @@
/*
* 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.config.xml;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.integration.config.ExpressionFactoryBean;
import org.springframework.integration.handler.ExpressionEvaluatingMessageHandler;
import org.springframework.integration.handler.MethodInvokingMessageHandler;
import org.w3c.dom.Element;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Parser for the <outbound-channel-adapter/> element.
*
* @author Mark Fisher
* @author Oleg Zhurakousky
* @author Artem Bilan
*/
public class DefaultOutboundChannelAdapterParser extends AbstractOutboundChannelAdapterParser {
protected String parseAndRegisterConsumer(Element element, ParserContext parserContext) {
BeanComponentDefinition innerConsumerDefinition = IntegrationNamespaceUtils.parseInnerHandlerDefinition(element, parserContext);
String consumerRef = element.getAttribute(IntegrationNamespaceUtils.REF_ATTRIBUTE);
String methodName = element.getAttribute(IntegrationNamespaceUtils.METHOD_ATTRIBUTE);
String consumerExpressionString = element.getAttribute(IntegrationNamespaceUtils.EXPRESSION_ATTRIBUTE);
boolean isInnerConsumer = innerConsumerDefinition != null;
boolean isRef = StringUtils.hasText(consumerRef);
boolean isExpression = StringUtils.hasText(consumerExpressionString);
boolean hasMethod = StringUtils.hasText(methodName);
if (!(isInnerConsumer ^ (isRef ^ isExpression))) {
parserContext.getReaderContext().error(
"Exactly one of the 'ref', 'expression' or inner bean is required.", element);
}
if (hasMethod & isExpression) {
parserContext.getReaderContext().error(
"The 'method' attribute can't be used with 'expression' attribute.", element);
}
if (hasMethod | isExpression) {
BeanDefinitionBuilder consumerBuilder = null;
if (hasMethod) {
consumerBuilder = BeanDefinitionBuilder.genericBeanDefinition(MethodInvokingMessageHandler.class);
if (isRef) {
consumerBuilder.addConstructorArgReference(consumerRef);
}
else {
consumerBuilder.addConstructorArgValue(innerConsumerDefinition);
}
consumerBuilder.addConstructorArgValue(methodName);
}
else {
consumerBuilder = BeanDefinitionBuilder.genericBeanDefinition(ExpressionEvaluatingMessageHandler.class);
RootBeanDefinition expressionDef = new RootBeanDefinition(ExpressionFactoryBean.class);
expressionDef.getConstructorArgumentValues().addGenericArgumentValue(consumerExpressionString);
consumerBuilder.addConstructorArgValue(expressionDef);
}
consumerBuilder.addPropertyValue("componentType", "outbound-channel-adapter");
String order = element.getAttribute(IntegrationNamespaceUtils.ORDER);
if (StringUtils.hasText(order)) {
consumerBuilder.addPropertyValue(IntegrationNamespaceUtils.ORDER, order);
}
consumerRef = BeanDefinitionReaderUtils.registerWithGeneratedName(consumerBuilder.getBeanDefinition(), parserContext.getRegistry());
}
else if (isInnerConsumer) {
consumerRef = innerConsumerDefinition.getBeanName();
}
Assert.hasText(consumerRef, "Can not determine consumer for 'outbound-channel-adapter'");
return consumerRef;
}
@Override
protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) {
throw new UnsupportedOperationException();
}
}

View File

@@ -52,9 +52,9 @@ public class IntegrationNamespaceHandler extends AbstractIntegrationNamespaceHan
registerBeanDefinitionParser("payload-deserializing-transformer", new PayloadDeserializingTransformerParser());
registerBeanDefinitionParser("claim-check-in", new ClaimCheckInParser());
registerBeanDefinitionParser("claim-check-out", new ClaimCheckOutParser());
registerBeanDefinitionParser("inbound-channel-adapter", new MethodInvokingInboundChannelAdapterParser());
registerBeanDefinitionParser("inbound-channel-adapter", new DefaultInboundChannelAdapterParser());
registerBeanDefinitionParser("resource-inbound-channel-adapter", new ResourceInboundChannelAdapterParser());
registerBeanDefinitionParser("outbound-channel-adapter", new MethodInvokingOutboundChannelAdapterParser());
registerBeanDefinitionParser("outbound-channel-adapter", new DefaultOutboundChannelAdapterParser());
registerBeanDefinitionParser("logging-channel-adapter", new LoggingChannelAdapterParser());
registerBeanDefinitionParser("gateway", new GatewayParser());
registerBeanDefinitionParser("delayer", new DelayerParser());

View File

@@ -36,6 +36,7 @@ import org.w3c.dom.Element;
* @author Alex Peters
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Artem Bilan
*/
public abstract class IntegrationNamespaceUtils {
@@ -43,6 +44,7 @@ public abstract class IntegrationNamespaceUtils {
static final String REF_ATTRIBUTE = "ref";
static final String METHOD_ATTRIBUTE = "method";
static final String ORDER = "order";
static final String EXPRESSION_ATTRIBUTE = "expression";
/**
* Configures the provided bean definition builder with a property value corresponding to the attribute whose name

View File

@@ -1,77 +0,0 @@
/*
* 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.config.xml;
import org.w3c.dom.Element;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Parser for the <outbound-channel-adapter/> element.
*
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public class MethodInvokingOutboundChannelAdapterParser extends AbstractOutboundChannelAdapterParser {
protected String parseAndRegisterConsumer(Element element, ParserContext parserContext) {
BeanComponentDefinition consumerDefinition = IntegrationNamespaceUtils.parseInnerHandlerDefinition(element, parserContext);
String consumerRef = null;
if (consumerDefinition == null){
consumerRef = element.getAttribute(IntegrationNamespaceUtils.REF_ATTRIBUTE);
} else {
consumerRef = consumerDefinition.getBeanName();
}
if (element.hasAttribute(IntegrationNamespaceUtils.METHOD_ATTRIBUTE)) {
consumerRef = BeanDefinitionReaderUtils.registerWithGeneratedName(
this.parseConsumer(element, parserContext), parserContext.getRegistry());
}
Assert.hasText(consumerRef, "Can not determine consumer for 'outbound-channel-adapter'");
return consumerRef;
}
@Override
protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) {
BeanDefinitionBuilder invokerBuilder = BeanDefinitionBuilder.genericBeanDefinition(
IntegrationNamespaceUtils.BASE_PACKAGE + ".handler.MethodInvokingMessageHandler");
invokerBuilder.addPropertyValue("componentType", "outbound-channel-adapter");
BeanComponentDefinition innerHandlerDefinition =
IntegrationNamespaceUtils.parseInnerHandlerDefinition(element, parserContext);
if (innerHandlerDefinition == null){
Assert.hasText(element.getAttribute(IntegrationNamespaceUtils.REF_ATTRIBUTE),
"You must provide 'ref' attribute or register inner bean for " +
"Outbound Channel consumer.");
invokerBuilder.addConstructorArgReference(element.getAttribute(IntegrationNamespaceUtils.REF_ATTRIBUTE));
} else {
invokerBuilder.addConstructorArgValue(innerHandlerDefinition);
}
invokerBuilder.addConstructorArgValue(element.getAttribute(IntegrationNamespaceUtils.METHOD_ATTRIBUTE));
String order = element.getAttribute(IntegrationNamespaceUtils.ORDER);
if (StringUtils.hasText(order)) {
invokerBuilder.addPropertyValue(IntegrationNamespaceUtils.ORDER, order);
}
return invokerBuilder.getBeanDefinition();
}
}

View File

@@ -94,7 +94,8 @@ public class BeanFactoryTypeConverter implements TypeConverter, BeanFactoryAware
}
public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) {
if (targetType.getType() == Void.class || targetType.getType() == Void.TYPE) {
// TODO maybe tentative decision... Echoes with org.springframework.expression.common.ExpressionUtils.convertTypedValue()
if ((targetType.getType() == Void.class || targetType.getType() == Void.TYPE) && value == null) {
return null;
}
if (conversionService.canConvert(sourceType, targetType)) {

View File

@@ -1,46 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd">
<channel id="queueChannel">
<queue capacity="1"/>
</channel>
<channel id="queueChannelForHeadersTest">
<queue capacity="1"/>
</channel>
<outbound-channel-adapter id="outboundWithImplicitChannel" ref="consumer"/>
<outbound-channel-adapter id="methodInvokingConsumer" ref="testBean" method="store"/>
<inbound-channel-adapter id="methodInvokingSource" ref="testBean" method="getMessage"
channel="queueChannel" auto-startup="false">
<poller max-messages-per-poll="1" fixed-delay="10000"/>
</inbound-channel-adapter>
<channel id="withTimeoutChannel">
<queue/>
</channel>
<inbound-channel-adapter id="methodInvokingSourceWithTimeout" ref="testBean" method="getMessage"
channel="withTimeoutChannel" auto-startup="false"
send-timeout="999">
<poller max-messages-per-poll="1" fixed-rate="800"/>
</inbound-channel-adapter>
<inbound-channel-adapter id="methodInvokingSourceWithHeaders" ref="testBean" method="getMessage" channel="queueChannelForHeadersTest" auto-startup="false">
<poller max-messages-per-poll="1" fixed-delay="10000"/>
<header name="foo" value="ABC"/>
<header name="bar" expression="new Integer(123)"/>
</inbound-channel-adapter>
<beans:bean id="consumer" class="org.springframework.integration.config.TestConsumer"/>
<beans:bean id="testBean" class="org.springframework.integration.config.TestBean"/>
<channel id="queueChannel">
<queue capacity="1"/>
</channel>
</beans:beans>
<channel id="queueChannelForHeadersTest">
<queue capacity="1"/>
</channel>
<outbound-channel-adapter id="outboundWithImplicitChannel" ref="consumer"/>
<outbound-channel-adapter id="methodInvokingConsumer" ref="testBean" method="store"/>
<outbound-channel-adapter id="expressionConsumer" expression="@testBean.store(payload)"/>
<inbound-channel-adapter id="methodInvokingSource" ref="testBean" method="getMessage"
channel="queueChannel" auto-startup="false">
<poller max-messages-per-poll="1" fixed-delay="10000"/>
</inbound-channel-adapter>
<channel id="withTimeoutChannel">
<queue/>
</channel>
<inbound-channel-adapter id="methodInvokingSourceWithTimeout" ref="testBean" method="getMessage"
channel="withTimeoutChannel" auto-startup="false"
send-timeout="999">
<poller max-messages-per-poll="1" fixed-rate="800"/>
</inbound-channel-adapter>
<inbound-channel-adapter id="methodInvokingSourceWithHeaders" ref="testBean" method="getMessage" channel="queueChannelForHeadersTest" auto-startup="false">
<poller max-messages-per-poll="1" fixed-delay="10000"/>
<header name="foo" value="ABC"/>
<header name="bar" expression="new Integer(123)"/>
</inbound-channel-adapter>
<beans:bean id="consumer" class="org.springframework.integration.config.TestConsumer"/>
<beans:bean id="testBean" class="org.springframework.integration.config.TestBean"/>
</beans:beans>

View File

@@ -23,13 +23,16 @@ import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.MessageHandler;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
@@ -41,6 +44,7 @@ import org.springframework.integration.test.util.TestUtils;
/**
* @author Mark Fisher
* @author Oleg Zhurakousky
* @author Artem Bilan
*/
public class ChannelAdapterParserTests {
@@ -134,6 +138,27 @@ public class ChannelAdapterParserTests {
assertEquals("consumer test", testBean.getMessage());
}
@Test
/**
* @since 2.1
*/
public void expressionConsumer() {
String beanName = "expressionConsumer";
Object channel = this.applicationContext.getBean(beanName);
assertTrue(channel instanceof DirectChannel);
BeanFactoryChannelResolver channelResolver = new BeanFactoryChannelResolver(this.applicationContext);
assertNotNull(channelResolver.resolveChannelName(beanName));
Object adapter = this.applicationContext.getBean(beanName + ".adapter");
assertNotNull(adapter);
assertTrue(adapter instanceof EventDrivenConsumer);
TestBean testBean = (TestBean) this.applicationContext.getBean("testBean");
assertNull(testBean.getMessage());
Message<?> message = new GenericMessage<String>("consumer test expression");
assertTrue(((MessageChannel) channel).send(message));
assertNotNull(testBean.getMessage());
assertEquals("consumer test expression", testBean.getMessage());
}
@Test
public void methodInvokingSource() {
String beanName = "methodInvokingSource";
@@ -221,19 +246,24 @@ public class ChannelAdapterParserTests {
BeanFactoryChannelResolver channelResolver = new BeanFactoryChannelResolver(this.applicationContext);
channelResolver.resolveChannelName("methodInvokingSource");
}
@Test
public void methodInvokingSourceWithSendTimeout() throws Exception{
public void methodInvokingSourceWithSendTimeout() throws Exception {
String beanName = "methodInvokingSourceWithTimeout";
SourcePollingChannelAdapter adapter =
this.applicationContext.getBean(beanName, SourcePollingChannelAdapter.class);
SourcePollingChannelAdapter adapter =
this.applicationContext.getBean(beanName, SourcePollingChannelAdapter.class);
assertNotNull(adapter);
long sendTimeout = TestUtils.getPropertyValue(adapter, "messagingTemplate.sendTimeout", Long.class);
assertEquals(999, sendTimeout);
}
public static class SampleBean{
@Test(expected = BeanDefinitionParsingException.class)
public void innerBeanAndExpressionFail() throws Exception {
new ClassPathXmlApplicationContext("InboundChannelAdapterInnerBeanWithExpression-fail-context.xml", this.getClass());
}
public static class SampleBean {
private String message = "hello";
String getMessage() {
@@ -241,3 +271,4 @@ public class ChannelAdapterParserTests {
}
}
}

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd">
<channel id="channel"/>
<inbound-channel-adapter id="pojoWithExpression" expression="'test'" method="getMessage"
channel="channel">
<poller fixed-delay="1234"/>
<beans:bean class="org.springframework.integration.config.ChannelAdapterParserTests$SampleBean"/>
</inbound-channel-adapter>
</beans:beans>