INT-1528 IntegrationNamespaceUtils.parseInnerHandlerDefinition(..) no longer registers the inner bean definition. Relatedly, all polling inbound adapter parsers now return BeanMetadataElement rather than a pre-registered bean's name (flexibility: RuntimeBeanReference, BeanDefintion, etc.)

This commit is contained in:
Mark Fisher
2010-10-27 15:59:29 -04:00
committed by Chris Beams
parent 5a04779bbe
commit 1e9c3c6a8d
15 changed files with 202 additions and 122 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2010 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.
@@ -18,10 +18,10 @@ package org.springframework.integration.config.xml;
import org.w3c.dom.Element;
import org.springframework.beans.BeanMetadataElement;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
/**
@@ -33,13 +33,13 @@ public abstract class AbstractPollingInboundChannelAdapterParser extends Abstrac
@Override
protected AbstractBeanDefinition doParse(Element element, ParserContext parserContext, String channelName) {
String source = this.parseSource(element, parserContext);
if (!StringUtils.hasText(source)) {
BeanMetadataElement source = this.parseSource(element, parserContext);
if (source == null) {
parserContext.getReaderContext().error("failed to parse source", element);
}
BeanDefinitionBuilder adapterBuilder = BeanDefinitionBuilder.genericBeanDefinition(
IntegrationNamespaceUtils.BASE_PACKAGE + ".config.SourcePollingChannelAdapterFactoryBean");
adapterBuilder.addPropertyReference("source", source);
adapterBuilder.addPropertyValue("source", source);
adapterBuilder.addPropertyReference("outputChannel", channelName);
Element pollerElement = DomUtils.getChildElementByTagName(element, "poller");
if (pollerElement != null) {
@@ -53,6 +53,6 @@ public abstract class AbstractPollingInboundChannelAdapterParser extends Abstrac
* Subclasses must implement this method to parse the PollableSource instance
* which the created Channel Adapter will poll.
*/
protected abstract String parseSource(Element element, ParserContext parserContext);
protected abstract BeanMetadataElement parseSource(Element element, ParserContext parserContext);
}

View File

@@ -191,8 +191,7 @@ public abstract class IntegrationNamespaceUtils {
}
public static BeanComponentDefinition parseInnerHandlerDefinition(Element element, ParserContext parserContext) {
// parses out inner bean definition for concrete implementation if
// defined
// parses out the inner bean definition for concrete implementation if defined
List<Element> childElements = DomUtils.getChildElementsByTagName(element, "bean");
BeanComponentDefinition innerComponentDefinition = null;
if (childElements != null && childElements.size() == 1) {
@@ -202,15 +201,13 @@ public abstract class IntegrationNamespaceUtils {
bdHolder = delegate.decorateBeanDefinitionIfRequired(beanElement, bdHolder);
BeanDefinition inDef = bdHolder.getBeanDefinition();
innerComponentDefinition = new BeanComponentDefinition(inDef, bdHolder.getBeanName());
parserContext.registerBeanComponent(innerComponentDefinition);
}
String ref = element.getAttribute(REF_ATTRIBUTE);
Assert.isTrue(!(StringUtils.hasText(ref) && innerComponentDefinition != null),
"Ambiguous definition. Inner bean "
+ (innerComponentDefinition == null ? innerComponentDefinition : innerComponentDefinition
.getBeanDefinition().getBeanClassName()) + " declaration and \"ref\" " + ref
+ " are not allowed together.");
"Ambiguous definition. Inner bean " + (innerComponentDefinition == null ? innerComponentDefinition
: innerComponentDefinition.getBeanDefinition().getBeanClassName())
+ " declaration and \"ref\" " + ref + " are not allowed together.");
return innerComponentDefinition;
}
}

View File

@@ -20,6 +20,8 @@ import java.util.List;
import org.w3c.dom.Element;
import org.springframework.beans.BeanMetadataElement;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
@@ -38,39 +40,57 @@ import org.springframework.util.xml.DomUtils;
public class MethodInvokingInboundChannelAdapterParser extends AbstractPollingInboundChannelAdapterParser {
@Override
protected String parseSource(Element element, ParserContext parserContext) {
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);
}
sourceRef = innnerBeanDef.getBeanName();
if (StringUtils.hasText(methodName)) {
result = this.parseMethodInvokingSource(innnerBeanDef, methodName, element, parserContext);
}
else {
result = innnerBeanDef;
}
}
else if (StringUtils.hasText(expressionString)) {
if (StringUtils.hasText(sourceRef)) {
parserContext.getReaderContext().error(
"the 'expression' and 'ref' attributes are mutually exclusive options", element);
}
sourceRef = this.parseExpression(expressionString, element, parserContext);
String expressionBeanName = this.parseExpression(expressionString, element, parserContext);
result = new RuntimeBeanReference(expressionBeanName);
}
if (!StringUtils.hasText(sourceRef)) {
else if (StringUtils.hasText(sourceRef)) {
BeanMetadataElement sourceValue = new RuntimeBeanReference(sourceRef);
if (StringUtils.hasText(methodName)) {
result = this.parseMethodInvokingSource(sourceValue, methodName, element, parserContext);
}
else {
result = sourceValue;
}
}
else {
parserContext.getReaderContext().error("One of the following is required: " +
"'ref' attribute, 'expression' attribute, or an inner-bean definition.", element);
}
String methodName = element.getAttribute("method");
if (StringUtils.hasText(methodName)) {
BeanDefinitionBuilder sourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(
IntegrationNamespaceUtils.BASE_PACKAGE + ".endpoint.MethodInvokingMessageSource");
sourceBuilder.addPropertyReference("object", sourceRef);
sourceBuilder.addPropertyValue("methodName", methodName);
this.parseHeaderExpressions(sourceBuilder, element, parserContext);
sourceRef = BeanDefinitionReaderUtils.registerWithGeneratedName(
sourceBuilder.getBeanDefinition(), parserContext.getRegistry());
}
return sourceRef;
return result;
}
private BeanMetadataElement parseMethodInvokingSource(BeanMetadataElement targetObject, String methodName, Element element, ParserContext parserContext) {
BeanDefinitionBuilder sourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(
IntegrationNamespaceUtils.BASE_PACKAGE + ".endpoint.MethodInvokingMessageSource");
sourceBuilder.addPropertyValue("object", targetObject);
sourceBuilder.addPropertyValue("methodName", methodName);
this.parseHeaderExpressions(sourceBuilder, element, parserContext);
String sourceRef = BeanDefinitionReaderUtils.registerWithGeneratedName(
sourceBuilder.getBeanDefinition(), parserContext.getRegistry());
return new RuntimeBeanReference(sourceRef);
}
private String parseExpression(String expressionString, Element element, ParserContext parserContext) {

View File

@@ -0,0 +1,15 @@
<?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">
<!-- see INT-1528 -->
<service-activator id="testEndpoint" input-channel="input">
<beans:bean class="org.springframework.integration.config.xml.InnerBeanConfigTests$TestBean"/>
</service-activator>
</beans:beans>

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2002-2010 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 static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class InnerBeanConfigTests {
@Autowired
private EventDrivenConsumer testEndpoint;
@Autowired
private ApplicationContext context;
// INT-1528: the inner bean should not be registered in the context
@Test(expected = NoSuchBeanDefinitionException.class)
public void checkInnerBean() {
Object innerBean = TestUtils.getPropertyValue(testEndpoint, "handler.processor.delegate.targetObject");
assertNotNull(innerBean);
context.getBean(TestBean.class);
}
public static class TestBean {
public String echo(String value) {
return value;
}
}
}