From 85c3263e1e19faf166d1ebffaa311ea20c041420 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Tue, 4 May 2010 14:31:20 +0000 Subject: [PATCH] INT-1076 added support for the 'ignore-channel-name-resolution-failures' attribute on elements. --- .../xml/config/XPathRouterParser.java | 45 ++++++------------- .../xml/config/spring-integration-xml-2.0.xsd | 15 ++++++- .../TestXmlApplicationContextHelper.java | 9 ++-- .../xml/config/XPathRouterParserTests.java | 26 +++++++++++ 4 files changed, 59 insertions(+), 36 deletions(-) diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathRouterParser.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathRouterParser.java index 4078de8c19..c7f4ca8ddc 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathRouterParser.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathRouterParser.java @@ -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. @@ -23,13 +23,15 @@ import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.config.xml.AbstractConsumerEndpointParser; -import org.springframework.integration.xml.router.XPathMultiChannelRouter; -import org.springframework.integration.xml.router.XPathSingleChannelRouter; +import org.springframework.integration.config.xml.IntegrationNamespaceUtils; import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** + * Parser for the <xpath-router/> element. + * * @author Jonas Partner + * @author Mark Fisher */ public class XPathRouterParser extends AbstractConsumerEndpointParser { @@ -48,29 +50,19 @@ public class XPathRouterParser extends AbstractConsumerEndpointParser { @Override protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) { - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(); - - boolean multiChannel = Boolean.parseBoolean(element.getAttribute("multi-channel")); - String xPathExpressionRef = element.getAttribute("xpath-expression-ref"); - String channelResolver = element.getAttribute("channel-resolver"); - String resolutionRequired = element.getAttribute("resolution-required"); - String defaultOutputChannel = element.getAttribute("default-output-channel"); - NodeList xPathExpressionNodes = element.getElementsByTagNameNS( element.getNamespaceURI(), "xpath-expression"); Assert.isTrue(xPathExpressionNodes.getLength() < 2, "Only one xpath-expression child can be specified."); + String xPathExpressionRef = element.getAttribute("xpath-expression-ref"); boolean xPathExpressionChildPresent = (xPathExpressionNodes.getLength() == 1); boolean xPathReferencePresent = StringUtils.hasText(xPathExpressionRef); - Assert.isTrue(xPathExpressionChildPresent ^ xPathReferencePresent, "Exactly one of 'xpath-expression' or 'xpath-expression-ref' is required."); - if (multiChannel) { - builder.getBeanDefinition().setBeanClass(XPathMultiChannelRouter.class); - } - else { - builder.getBeanDefinition().setBeanClass(XPathSingleChannelRouter.class); - } + boolean multiChannel = Boolean.parseBoolean(element.getAttribute("multi-channel")); + String classname = "org.springframework.integration.xml.router." + + ((multiChannel) ? "XPathMultiChannelRouter" : "XPathSingleChannelRouter"); + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(classname); if (xPathExpressionChildPresent) { BeanDefinition beanDefinition = this.xpathParser.parse( (Element) xPathExpressionNodes.item(0), parserContext); @@ -79,19 +71,10 @@ public class XPathRouterParser extends AbstractConsumerEndpointParser { else { builder.addConstructorArgReference(xPathExpressionRef); } - - if(StringUtils.hasText(resolutionRequired)){ - builder.addPropertyValue("resolutionRequired", Boolean.parseBoolean(resolutionRequired)); - } - - if(StringUtils.hasText(channelResolver)){ - builder.addPropertyReference("channelResolver", channelResolver); - } - - if(StringUtils.hasText(defaultOutputChannel)){ - builder.addPropertyReference("defaultOutputChannel", defaultOutputChannel); - } - + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "resolution-required"); + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "ignore-channel-name-resolution-failures"); + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "channel-resolver"); + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "default-output-channel"); return builder; } diff --git a/org.springframework.integration.xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.0.xsd b/org.springframework.integration.xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.0.xsd index 1c4f2bb159..869b6fd82e 100644 --- a/org.springframework.integration.xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.0.xsd +++ b/org.springframework.integration.xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.0.xsd @@ -197,7 +197,20 @@ - + + + + Specify whether this router should always be required to return at least one channel or name. + + + + + + + Specify whether a failure to resolve a channel name returned by this router should be ignored. + + + diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/TestXmlApplicationContextHelper.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/TestXmlApplicationContextHelper.java index ed50ea040f..ff915e9c47 100644 --- a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/TestXmlApplicationContextHelper.java +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/TestXmlApplicationContextHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.xml.config; public class TestXmlApplicationContextHelper { @@ -34,11 +35,11 @@ public class TestXmlApplicationContextHelper { + "'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-1.0.xsd " + + "http://www.springframework.org/schema/integration/spring-integration.xsd " + "http://www.springframework.org/schema/integration/xml " - + "http://www.springframework.org/schema/integration/xml/spring-integration-xml-1.0.xsd " + + "http://www.springframework.org/schema/integration/xml/spring-integration-xml.xsd " + "http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd " + - "http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd' >" + + "http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd' >" + ""; private final static String footer = ""; diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathRouterParserTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathRouterParserTests.java index 0904921341..8e2cd17711 100644 --- a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathRouterParserTests.java +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XPathRouterParserTests.java @@ -36,6 +36,7 @@ import org.w3c.dom.Document; /** * @author Jonas Partner + * @author Mark Fisher */ @ContextConfiguration public class XPathRouterParserTests { @@ -155,7 +156,32 @@ public class XPathRouterParserTests { assertEquals("Resolution required not set to true ", true, resolutionRequired); } + @Test + public void testIgnoreChannelNameResolutionFailuresFalse() throws Exception { + StringBuffer contextBuffer = new StringBuffer( + ""); + EventDrivenConsumer consumer = buildContext(contextBuffer.toString()); + + DirectFieldAccessor accessor = new DirectFieldAccessor(consumer); + Object handler = accessor.getPropertyValue("handler"); + accessor = new DirectFieldAccessor(handler); + Object ignoreChannelNameResolutionFailures = accessor.getPropertyValue("ignoreChannelNameResolutionFailures"); + assertEquals("ignoreChannelNameResolutionFailures not set to false", false, ignoreChannelNameResolutionFailures); + } + @Test + public void testIgnoreChannelNameResolutionFailuresTrue() throws Exception { + StringBuffer contextBuffer = new StringBuffer( + ""); + EventDrivenConsumer consumer = buildContext(contextBuffer.toString()); + + DirectFieldAccessor accessor = new DirectFieldAccessor(consumer); + Object handler = accessor.getPropertyValue("handler"); + accessor = new DirectFieldAccessor(handler); + Object ignoreChannelNameResolutionFailures = accessor.getPropertyValue("ignoreChannelNameResolutionFailures"); + assertEquals("ignoreChannelNameResolutionFailures not set to true ", true, ignoreChannelNameResolutionFailures); + } + @Test public void testSetDefaultOutputChannel() throws Exception { StringBuffer contextBuffer = new StringBuffer("");