ChannelAdapterParser is now MethodInvokingAdapterParser and only creates the source or target. The result should be configured with a SourceEndpoint or TargetEndpoint.

This commit is contained in:
Mark Fisher
2008-04-24 00:43:08 +00:00
parent 5eb87c218e
commit 28f914748b
6 changed files with 93 additions and 155 deletions

View File

@@ -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;
}
}

View File

@@ -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.

View File

@@ -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());

View File

@@ -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);
}
}

View File

@@ -107,39 +107,20 @@
</xsd:complexType>
</xsd:element>
<xsd:element name="source-adapter">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines a source (inbound) channel adapter.
</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="ref" type="xsd:string" use="required"/>
<xsd:attribute name="method" type="xsd:string" use="required"/>
<xsd:attribute name="channel" type="xsd:string" use="required"/>
<xsd:attribute name="period" type="xsd:int"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="source-adapter" type="methodInvokingAdapterType">
<xsd:annotation>
<xsd:documentation>
Defines a MethodInvokingSource.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="target-adapter">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines a target (outbound) channel adapter.
</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="ref" type="xsd:string" use="required"/>
<xsd:attribute name="method" type="xsd:string" use="required"/>
<xsd:attribute name="channel" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="target-adapter" type="methodInvokingAdapterType">
<xsd:annotation>
<xsd:documentation>
Defines a MethodInvokingTarget.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="endpoint">
@@ -295,4 +276,18 @@
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="methodInvokingAdapterType">
<xsd:annotation>
<xsd:documentation>
Base type for method-invoking adapters.
</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="ref" type="xsd:string" use="required"/>
<xsd:attribute name="method" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>

View File

@@ -11,9 +11,15 @@
<si:channel id="channel"/>
<si:source-adapter ref="source" method="foo" channel="channel" period="100"/>
<si:source-endpoint source="sourceAdapter" channel="channel">
<si:schedule period="100"/>
</si:source-endpoint>
<si:target-adapter ref="sink" method="store" channel="channel"/>
<si:endpoint handler="targetAdapter" input-channel="channel"/>
<si:source-adapter id="sourceAdapter" ref="source" method="foo"/>
<si:target-adapter id="targetAdapter" ref="sink" method="store"/>
<bean id="source" class="org.springframework.integration.adapter.TestSource"/>