INT-677 - added inner bean definition support for <splitter/> and <transformer/> message consumers

This commit is contained in:
Oleg Zhurakousky
2009-06-23 04:44:47 +00:00
parent 0165ba2247
commit 97fc78a619
7 changed files with 371 additions and 31 deletions

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2002-2009 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 java.util.List;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
/**
* Abstract Parser for any consumers that require capabilities to define
* its handler implementation as inner bean.<br>
* For example:<br>
* <pre>
* &lt;transformer id="testTransformer" input-channel="inChannel" output-channel="outChannel"&gt;
* &lt;beans:bean class="org.bar.TestTransformer"/&gt;
* &lt;/transformer&gt;
* </pre>
*
* @author Oleg Zhurakousky
* @since 1.0.3
*/
public abstract class AbstractInnerDefinitionAwareEndpointParser extends AbstractConsumerEndpointParser {
@Override
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
// parses out inner bean definition for concrete implementation if defined
List<Element> childElements = DomUtils.getChildElementsByTagName(element, "bean");
BeanDefinition innerDefinition = null;
if (childElements != null && childElements.size() == 1){
Element beanElement = childElements.get(0);
BeanDefinitionParserDelegate delegate = parserContext.getDelegate();
innerDefinition = delegate.parseBeanDefinitionElement(beanElement).getBeanDefinition();
}
String ref = element.getAttribute(REF_ATTRIBUTE);
Assert.isTrue(!(StringUtils.hasText(ref) && innerDefinition != null), "Ambiguous definition. Inner bean " +
(innerDefinition == null ? innerDefinition : innerDefinition.getBeanClassName()) + " declaration and \"ref\" " + ref +
" are not allowed together.");
return this.parseEndpoint(element, parserContext, innerDefinition);
}
/**
*
* @param element
* @param parserContext
* @param innerDefinition
* @return
*/
protected abstract BeanDefinitionBuilder parseEndpoint(Element element, ParserContext parserContext, BeanDefinition innerDefinition);
}

View File

@@ -18,6 +18,7 @@ package org.springframework.integration.config.xml;
import org.w3c.dom.Element;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
@@ -26,14 +27,17 @@ import org.springframework.util.StringUtils;
* Parser for the &lt;splitter/&gt; element.
*
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public class SplitterParser extends AbstractConsumerEndpointParser {
public class SplitterParser extends AbstractInnerDefinitionAwareEndpointParser {
@Override
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
protected BeanDefinitionBuilder parseEndpoint(Element element, ParserContext parserContext, BeanDefinition innerDefinition) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
IntegrationNamespaceUtils.BASE_PACKAGE + ".config.SplitterFactoryBean");
if (element.hasAttribute(REF_ATTRIBUTE)) {
if (innerDefinition != null){
builder.addPropertyValue("targetObject", innerDefinition);
} else if (element.hasAttribute(REF_ATTRIBUTE)) {
String ref = element.getAttribute(REF_ATTRIBUTE);
builder.addPropertyReference("targetObject", ref);
if (StringUtils.hasText(element.getAttribute(METHOD_ATTRIBUTE))) {

View File

@@ -16,34 +16,41 @@
package org.springframework.integration.config.xml;
import org.w3c.dom.Element;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* Parser for the &lt;transformer/&gt; element.
*
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public class TransformerParser extends AbstractConsumerEndpointParser {
public class TransformerParser extends AbstractInnerDefinitionAwareEndpointParser {
@Override
protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
protected BeanDefinitionBuilder parseEndpoint(Element element, ParserContext parserContext, BeanDefinition innerDefinition) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
IntegrationNamespaceUtils.BASE_PACKAGE + ".config.TransformerFactoryBean");
String ref = element.getAttribute(REF_ATTRIBUTE);
if (!StringUtils.hasText(ref)) {
parserContext.getReaderContext().error("The 'ref' attribute is required.", element);
return null;
}
builder.addPropertyReference("targetObject", ref);
if (innerDefinition != null){
builder.addPropertyValue("targetObject", innerDefinition);
} else {
String ref = element.getAttribute(REF_ATTRIBUTE);
if (!StringUtils.hasText(ref)) {
parserContext.getReaderContext().error("Either \"ref\" attribute or inner bean (<bean/>) definition of concrete implementation of " +
"this Transformer is required.", element);
return null;
}
builder.addPropertyReference("targetObject", ref);
}
String method = element.getAttribute(METHOD_ATTRIBUTE);
if (StringUtils.hasText(method)) {
builder.addPropertyValue("targetMethodName", method);
}
return builder;
}
}

View File

@@ -82,7 +82,8 @@
specified, it will be a
bounded queue.
A custom Queue implementation can be
A custom Queue implementation
can be
injected using the 'ref' attribute.
</xsd:documentation>
</xsd:annotation>
@@ -776,7 +777,7 @@
</xsd:complexType>
</xsd:element>
<xsd:element name="transformer" type="handlerEndpointType">
<xsd:element name="transformer" type="innerEndpointDefinitionAware">
<xsd:annotation>
<xsd:documentation>
Defines a Transformer.
@@ -843,12 +844,12 @@
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="routerType">
<xsd:sequence>
<xsd:sequence>
<xsd:element name="mapping" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="value" type="xsd:string"/>
<xsd:attribute name="channel" type="xsd:string"/>
</xsd:complexType>
<xsd:attribute name="value" type="xsd:string" />
<xsd:attribute name="channel" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="header-name" type="xsd:string">
@@ -873,12 +874,12 @@
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="routerType">
<xsd:sequence>
<xsd:sequence>
<xsd:element name="mapping" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType>
<xsd:attribute name="type" type="xsd:string"/>
<xsd:attribute name="channel" type="xsd:string"/>
</xsd:complexType>
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="channel" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:extension>
@@ -900,12 +901,12 @@
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:expected-type type="java.lang.Object"/>
<tool:expected-type type="java.lang.Object" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="method" type="xsd:string"/>
<xsd:attribute name="method" type="xsd:string" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
@@ -924,14 +925,15 @@
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="timeout" type="xsd:string"/>
<xsd:attribute name="resolution-required" type="xsd:string"/>
<xsd:attribute name="ignore-channel-name-resolution-failures" type="xsd:string"/>
<xsd:attribute name="timeout" type="xsd:string" />
<xsd:attribute name="resolution-required" type="xsd:string" />
<xsd:attribute name="ignore-channel-name-resolution-failures"
type="xsd:string" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="splitter" type="handlerEndpointType">
<xsd:element name="splitter" type="innerEndpointDefinitionAware">
<xsd:annotation>
<xsd:documentation>
Defines a Splitter.
@@ -1010,7 +1012,8 @@
</xsd:appinfo>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="correlation-strategy-method" type="xsd:string" />
<xsd:attribute name="correlation-strategy-method"
type="xsd:string" />
<xsd:attribute name="discard-channel" type="xsd:string">
<xsd:annotation>
<xsd:appinfo>
@@ -1164,4 +1167,14 @@
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:complexType name="innerEndpointDefinitionAware">
<xsd:complexContent>
<xsd:extension base="handlerEndpointType">
<xsd:choice>
<xsd:element ref="beans:bean" minOccurs="0" maxOccurs="1" />
</xsd:choice>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>