INT-1906
fix & refactor DefaultInboundChannelAdapterParser, plus some improvements
This commit is contained in:
@@ -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);
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
/*
|
||||
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user