From 28f914748b9f8bd30f40c4f8de87cc202f36d242 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Thu, 24 Apr 2008 00:43:08 +0000 Subject: [PATCH] ChannelAdapterParser is now MethodInvokingAdapterParser and only creates the source or target. The result should be configured with a SourceEndpoint or TargetEndpoint. --- .../config/ChannelAdapterParser.java | 118 ------------------ .../integration/config/HandlerParser.java | 2 +- .../config/IntegrationNamespaceHandler.java | 6 +- .../config/MethodInvokingAdapterParser.java | 55 ++++++++ .../config/spring-integration-core-1.0.xsd | 57 ++++----- .../adapter/adapterTestsWithNamespace.xml | 10 +- 6 files changed, 93 insertions(+), 155 deletions(-) delete mode 100644 spring-integration-core/src/main/java/org/springframework/integration/config/ChannelAdapterParser.java create mode 100644 spring-integration-core/src/main/java/org/springframework/integration/config/MethodInvokingAdapterParser.java diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/ChannelAdapterParser.java b/spring-integration-core/src/main/java/org/springframework/integration/config/ChannelAdapterParser.java deleted file mode 100644 index 6d9e48ec72..0000000000 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/ChannelAdapterParser.java +++ /dev/null @@ -1,118 +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.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.config.RuntimeBeanReference; -import org.springframework.beans.factory.parsing.BeanComponentDefinition; -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.ConfigurationException; -import org.springframework.integration.adapter.MethodInvokingSource; -import org.springframework.integration.adapter.MethodInvokingTarget; -import org.springframework.integration.endpoint.HandlerEndpoint; -import org.springframework.integration.endpoint.PollingSourceEndpoint; -import org.springframework.integration.scheduling.PollingSchedule; -import org.springframework.integration.scheduling.Subscription; -import org.springframework.util.StringUtils; - -/** - * Parser for inbound and outbound channel adapters. - * - * @author Mark Fisher - */ -public class ChannelAdapterParser implements BeanDefinitionParser { - - private static final String ID_ATTRIBUTE = "id"; - - private static final String REF_ATTRIBUTE = "ref"; - - private static final String METHOD_ATTRIBUTE = "method"; - - private static final String CHANNEL_ATTRIBUTE = "channel"; - - private static final String PERIOD_ATTRIBUTE = "period"; - - - private final boolean isInbound; - - - public ChannelAdapterParser(boolean isInbound) { - this.isInbound = isInbound; - } - - - public BeanDefinition parse(Element element, ParserContext parserContext) { - String ref = element.getAttribute(REF_ATTRIBUTE); - String method = element.getAttribute(METHOD_ATTRIBUTE); - String channel = element.getAttribute(CHANNEL_ATTRIBUTE); - if (!StringUtils.hasText(ref)) { - throw new ConfigurationException("'ref' is required"); - } - if (!StringUtils.hasText(method)) { - throw new ConfigurationException("'method' is required"); - } - if (!StringUtils.hasText(channel)) { - throw new ConfigurationException("'channel' is required"); - } - RootBeanDefinition adapterDef = null; - RootBeanDefinition invokerDef = null; - if (this.isInbound) { - adapterDef = new RootBeanDefinition(PollingSourceEndpoint.class); - invokerDef = new RootBeanDefinition(MethodInvokingSource.class); - invokerDef.getPropertyValues().addPropertyValue("object", new RuntimeBeanReference(ref)); - invokerDef.getPropertyValues().addPropertyValue("method", method); - String invokerBeanName = parserContext.getReaderContext().generateBeanName(invokerDef); - parserContext.registerBeanComponent(new BeanComponentDefinition(invokerDef, invokerBeanName)); - String period = element.getAttribute(PERIOD_ATTRIBUTE); - if (!StringUtils.hasText(period)) { - throw new ConfigurationException("'period' is required"); - } - PollingSchedule schedule = new PollingSchedule(Integer.valueOf(period)); - adapterDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference(invokerBeanName)); - adapterDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference(channel)); - adapterDef.getConstructorArgumentValues().addGenericArgumentValue(schedule); - } - else { - adapterDef = new RootBeanDefinition(MethodInvokingTarget.class); - adapterDef.getPropertyValues().addPropertyValue("object", new RuntimeBeanReference(ref)); - adapterDef.getPropertyValues().addPropertyValue("method", method); - } - adapterDef.setSource(parserContext.extractSource(element)); - String beanName = element.getAttribute(ID_ATTRIBUTE); - if (!StringUtils.hasText(beanName)) { - beanName = parserContext.getReaderContext().generateBeanName(adapterDef); - } - if (!this.isInbound) { - RootBeanDefinition endpointDef = new RootBeanDefinition(HandlerEndpoint.class); - RootBeanDefinition subscriptionDef = new RootBeanDefinition(Subscription.class); - subscriptionDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference(channel)); - String subscriptionBeanName = parserContext.getReaderContext().generateBeanName(subscriptionDef); - parserContext.registerBeanComponent(new BeanComponentDefinition(subscriptionDef, subscriptionBeanName)); - endpointDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference(beanName)); - endpointDef.getPropertyValues().addPropertyValue("subscription", new RuntimeBeanReference(subscriptionBeanName)); - String endpointBeanName = parserContext.getReaderContext().generateBeanName(endpointDef); - parserContext.registerBeanComponent(new BeanComponentDefinition(endpointDef, endpointBeanName)); - } - parserContext.registerBeanComponent(new BeanComponentDefinition(adapterDef, beanName)); - return adapterDef; - } - -} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/HandlerParser.java b/spring-integration-core/src/main/java/org/springframework/integration/config/HandlerParser.java index e19bc808a3..5e1d3131a9 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/HandlerParser.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/HandlerParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * 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. diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationNamespaceHandler.java b/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationNamespaceHandler.java index 6bbcc71740..92f491a259 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationNamespaceHandler.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationNamespaceHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * 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. @@ -49,8 +49,8 @@ public class IntegrationNamespaceHandler extends NamespaceHandlerSupport { registerBeanDefinitionParser("annotation-driven", new AnnotationDrivenParser()); registerBeanDefinitionParser("channel", new ChannelParser()); registerBeanDefinitionParser("priority-channel", new ChannelParser()); - registerBeanDefinitionParser("source-adapter", new ChannelAdapterParser(true)); - registerBeanDefinitionParser("target-adapter", new ChannelAdapterParser(false)); + registerBeanDefinitionParser("source-adapter", new MethodInvokingAdapterParser()); + registerBeanDefinitionParser("target-adapter", new MethodInvokingAdapterParser()); registerBeanDefinitionParser("source-endpoint", new SourceEndpointParser()); registerBeanDefinitionParser("endpoint", new EndpointParser()); registerBeanDefinitionParser("handler", new HandlerParser()); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/MethodInvokingAdapterParser.java b/spring-integration-core/src/main/java/org/springframework/integration/config/MethodInvokingAdapterParser.java new file mode 100644 index 0000000000..86c2611f8a --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/MethodInvokingAdapterParser.java @@ -0,0 +1,55 @@ +/* + * 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.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; +import org.springframework.integration.ConfigurationException; +import org.springframework.integration.adapter.MethodInvokingSource; +import org.springframework.integration.adapter.MethodInvokingTarget; +import org.springframework.util.StringUtils; + +/** + * Parser for <source-adapter> and <target-adapter>. + * Creates a {@link MethodInvokingSource} or {@link MethodInvokingTarget}. + * + * @author Mark Fisher + */ +public class MethodInvokingAdapterParser extends AbstractSingleBeanDefinitionParser { + + @Override + protected Class getBeanClass(Element element) { + return "source-adapter".equals(element.getLocalName()) ? MethodInvokingSource.class : MethodInvokingTarget.class; + } + + @Override + protected void doParse(Element element, BeanDefinitionBuilder builder) { + String ref = element.getAttribute("ref"); + String method = element.getAttribute("method"); + if (!StringUtils.hasText(ref)) { + throw new ConfigurationException("The 'ref' attribute is required."); + } + if (!StringUtils.hasText(method)) { + throw new ConfigurationException("The 'method' attribute is required."); + } + builder.addPropertyReference("object", ref); + builder.addPropertyValue("method", method); + } + +} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/spring-integration-core-1.0.xsd b/spring-integration-core/src/main/java/org/springframework/integration/config/spring-integration-core-1.0.xsd index 8e682b716e..3b19eda2f6 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/spring-integration-core-1.0.xsd +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/spring-integration-core-1.0.xsd @@ -107,39 +107,20 @@ - - - - - Defines a source (inbound) channel adapter. - - - - - - - - - - - + + + + Defines a MethodInvokingSource. + + - - - - - Defines a target (outbound) channel adapter. - - - - - - - - - - + + + + Defines a MethodInvokingTarget. + + @@ -295,4 +276,18 @@ + + + + Base type for method-invoking adapters. + + + + + + + + + + \ No newline at end of file diff --git a/spring-integration-core/src/test/java/org/springframework/integration/adapter/adapterTestsWithNamespace.xml b/spring-integration-core/src/test/java/org/springframework/integration/adapter/adapterTestsWithNamespace.xml index e7700d7a05..7f2f6a67f9 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/adapter/adapterTestsWithNamespace.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/adapter/adapterTestsWithNamespace.xml @@ -11,9 +11,15 @@ - + + + - + + + + +