Removed 'handler' and 'handler-chain' elements.

This commit is contained in:
Mark Fisher
2008-09-05 01:03:45 +00:00
parent 4fb3480320
commit e22bf81927
8 changed files with 0 additions and 329 deletions

View File

@@ -1,128 +0,0 @@
/*
* Copyright 2002-2008 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;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.handler.DefaultMessageHandler;
import org.springframework.integration.handler.MessageHandlerChain;
import org.springframework.util.StringUtils;
/**
* Parser for the <handler/> element.
*
* @author Mark Fisher
*/
public class HandlerParser implements BeanDefinitionParser {
private static final String HANDLER_CHAIN_ELEMENT = "handler-chain";
private static final String HANDLER_ELEMENT = "handler";
private static final String HANDLERS_PROPERTY = "handlers";
private static final String OBJECT_PROPERTY = "object";
private static final String METHOD_NAME_PROPERTY = "methodName";
public BeanDefinition parse(Element element, ParserContext parserContext) {
if (HANDLER_CHAIN_ELEMENT.equals(element.getLocalName())) {
return this.parseHandlerChain(element, parserContext);
}
else if (HANDLER_ELEMENT.equals(element.getLocalName())) {
return this.parseHandler(element, parserContext, null);
}
return null;
}
private BeanDefinition parseHandlerChain(Element element, ParserContext parserContext) {
RootBeanDefinition beanDefinition = new RootBeanDefinition(MessageHandlerChain.class);
ManagedList handlers = new ManagedList();
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node child = childNodes.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
String localName = child.getLocalName();
if (HANDLER_ELEMENT.equals(localName)) {
parseHandler((Element) child, parserContext, handlers);
}
}
}
beanDefinition.getPropertyValues().addPropertyValue(HANDLERS_PROPERTY, handlers);
String id = element.getAttribute("id");
String beanName = (StringUtils.hasText(id)) ? id : parserContext.getReaderContext().generateBeanName(beanDefinition);
parserContext.registerBeanComponent(new BeanComponentDefinition(beanDefinition, beanName));
return beanDefinition;
}
@SuppressWarnings("unchecked")
private BeanDefinition parseHandler(Element element, ParserContext parserContext, ManagedList handlers) {
boolean isInnerHandler = (handlers != null);
String ref = element.getAttribute("ref");
String method = element.getAttribute("method");
String id = element.getAttribute("id");
if (!isInnerHandler && (!StringUtils.hasText(id) || !StringUtils.hasText(ref) || !StringUtils.hasText(method))) {
parserContext.getReaderContext().error("Top-level <handler> elements must provide 'id', 'ref', and 'method' attributes.",
parserContext.extractSource(element));
}
if (isInnerHandler && StringUtils.hasText(id)) {
parserContext.getReaderContext().error("The 'id' attribute is only supported for top-level <handler> elements.",
parserContext.extractSource(element));
}
if (StringUtils.hasText(method)) {
BeanDefinitionHolder bdh = this.parseHandlerAdapter(id, ref, method, parserContext, isInnerHandler);
if (handlers != null) {
handlers.add(bdh.getBeanDefinition());
return null;
}
return bdh.getBeanDefinition();
}
if (StringUtils.hasText(id)) {
parserContext.getReaderContext().error("The 'id' attribute is only supported for handler adapters (when 'method' is also provided).",
parserContext.extractSource(element));
}
if (handlers != null) {
handlers.add(new RuntimeBeanReference(ref));
}
return null;
}
private BeanDefinitionHolder parseHandlerAdapter(String id, String handlerRef, String handlerMethod, ParserContext parserContext, boolean isInnerHandler) {
BeanDefinition handlerDef = new RootBeanDefinition(DefaultMessageHandler.class);
handlerDef.getPropertyValues().addPropertyValue(OBJECT_PROPERTY, new RuntimeBeanReference(handlerRef));
handlerDef.getPropertyValues().addPropertyValue(METHOD_NAME_PROPERTY, handlerMethod);
String handlerBeanName = (StringUtils.hasText(id)) ? id :
BeanDefinitionReaderUtils.generateBeanName(handlerDef, parserContext.getRegistry(), isInnerHandler);
if (!isInnerHandler) {
parserContext.registerBeanComponent(new BeanComponentDefinition(handlerDef, handlerBeanName));
}
return new BeanDefinitionHolder(handlerDef, handlerBeanName);
}
}

View File

@@ -55,8 +55,6 @@ public class IntegrationNamespaceHandler extends NamespaceHandlerSupport {
registerBeanDefinitionParser("service-activator", new ServiceActivatorParser());
registerBeanDefinitionParser("channel-adapter", new ChannelAdapterParser());
registerBeanDefinitionParser("gateway", new GatewayParser());
registerBeanDefinitionParser("handler", new HandlerParser());
registerBeanDefinitionParser("handler-chain", new HandlerParser());
registerBeanDefinitionParser("selector-chain", new SelectorChainParser());
registerBeanDefinitionParser("router", new RouterParser());
registerBeanDefinitionParser("splitter", new SplitterParser());

View File

@@ -27,7 +27,6 @@ import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
@@ -54,31 +53,6 @@ public class EndpointParserTests {
assertEquals("test", handler.getMessageString());
}
@Test
public void testHandlerAdapterEndpoint() throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"handlerAdapterEndpointTests.xml", this.getClass());
context.start();
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
TestBean bean = (TestBean) context.getBean("testBean");
assertNull(bean.getMessage());
channel.send(new GenericMessage<String>("test"));
bean.getLatch().await(500, TimeUnit.MILLISECONDS);
assertEquals("test", bean.getMessage());
}
@Test
public void testHandlerChainEndpoint() throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"endpointWithHandlerChainElement.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
PollableChannel replyChannel = (PollableChannel) context.getBean("replyChannel");
channel.send(new StringMessage("test"));
Message<?> reply = replyChannel.receive(500);
assertNotNull(reply);
assertEquals("test-1-2-3", reply.getPayload());
}
@Test
public void testEndpointWithSelectorAccepts() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(

View File

@@ -1,62 +0,0 @@
/*
* Copyright 2002-2008 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;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class HandlerParserTests {
@Test
public void testTopLevelHandlerAdapter() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"handlerAdapterParserTests.xml", HandlerParserTests.class);
MessageHandler adapter = (MessageHandler) context.getBean("handlerAdapter");
assertNotNull(adapter);
Message<?> reply = adapter.handle(new StringMessage("foo"));
assertNotNull(reply);
assertEquals("bar", reply.getPayload());
}
@Test
public void testHandlerChain() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"handlerChainParserTests.xml", HandlerParserTests.class);
TestBean testBean = (TestBean) context.getBean("testBean");
assertNull(testBean.getMessage());
MessageHandler handlerChain = (MessageHandler) context.getBean("handlerChain");
assertNotNull(handlerChain);
Message<?> reply = handlerChain.handle(new StringMessage("test"));
assertNotNull(reply);
assertEquals(0, testBean.getLatch().getCount());
assertEquals("foo", testBean.getMessage());
assertEquals("bar", reply.getPayload());
}
}

View File

@@ -1,42 +0,0 @@
<?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-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-core-1.0.xsd">
<message-bus/>
<channel id="testChannel">
<queue capacity="5"/>
</channel>
<channel id="replyChannel">
<queue capacity="5"/>
</channel>
<handler-chain id="chain">
<handler ref="handler1"/>
<handler ref="handler2"/>
<handler ref="handler3"/>
</handler-chain>
<service-activator input-channel="testChannel" ref="chain" output-channel="replyChannel">
<poller period="100"/>
</service-activator>
<beans:bean id="handler1" class="org.springframework.integration.config.TestConcatenatingHandler">
<beans:constructor-arg value="-1"/>
</beans:bean>
<beans:bean id="handler2" class="org.springframework.integration.config.TestConcatenatingHandler">
<beans:constructor-arg value="-2"/>
</beans:bean>
<beans:bean id="handler3" class="org.springframework.integration.config.TestConcatenatingHandler">
<beans:constructor-arg value="-3"/>
</beans:bean>
</beans:beans>

View File

@@ -1,24 +0,0 @@
<?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-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-core-1.0.xsd">
<beans:bean id="messageBus" class="org.springframework.integration.bus.DefaultMessageBus"/>
<channel id="testChannel">
<queue capacity="50"/>
</channel>
<service-activator input-channel="testChannel" ref="testBean" method="store">
<poller period="100"/>
</service-activator>
<beans:bean id="testBean" class="org.springframework.integration.config.TestBean">
<beans:constructor-arg value="1"/>
</beans:bean>
</beans:beans>

View File

@@ -1,17 +0,0 @@
<?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-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-core-1.0.xsd">
<handler id="handlerAdapter" ref="testBean" method="store"/>
<beans:bean id="testBean" class="org.springframework.integration.config.TestBean">
<beans:constructor-arg value="1"/>
<beans:property name="replyMessageText" value="bar"/>
</beans:bean>
</beans:beans>

View File

@@ -1,28 +0,0 @@
<?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-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-core-1.0.xsd">
<handler-chain id="handlerChain">
<handler ref="handler1"/>
<handler ref="handler2"/>
<handler ref="testBean" method="store"/>
</handler-chain>
<handler id="handler1" ref="testBean" method="store"/>
<beans:bean id="handler2" class="org.springframework.integration.config.TestHandler">
<beans:constructor-arg value="1"/>
<beans:property name="replyMessageText" value="foo"/>
</beans:bean>
<beans:bean id="testBean" class="org.springframework.integration.config.TestBean">
<beans:constructor-arg value="1"/>
<beans:property name="replyMessageText" value="bar"/>
</beans:bean>
</beans:beans>