AbstractTransformerParser now creates the actual endpoint rather than requiring a "ref" to an object that implements Transformer. Therefore the element being parsed must provide 'input-channel' and 'output-channel'.
This commit is contained in:
@@ -27,6 +27,7 @@ import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.ConfigurationException;
|
||||
import org.springframework.integration.endpoint.MessageEndpoint;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.xml.DomUtils;
|
||||
|
||||
@@ -54,8 +55,18 @@ public abstract class AbstractEndpointParser extends AbstractSingleBeanDefinitio
|
||||
private static final String INTERCEPTORS_ELEMENT = "interceptors";
|
||||
|
||||
|
||||
/**
|
||||
* Subclasses may override this method to specify whether the endpoint type
|
||||
* expects a "ref" (and possibly a "method") in order to adapt an Object.
|
||||
*
|
||||
* <p>The default is <em>true</em>.
|
||||
*/
|
||||
protected boolean requiresBeanReference() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> getBeanClass(Element element) {
|
||||
protected final Class<?> getBeanClass(Element element) {
|
||||
return this.getEndpointClass();
|
||||
}
|
||||
|
||||
@@ -71,17 +82,19 @@ public abstract class AbstractEndpointParser extends AbstractSingleBeanDefinitio
|
||||
|
||||
@Override
|
||||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
String ref = element.getAttribute(REF_ATTRIBUTE);
|
||||
if (!StringUtils.hasText(ref)) {
|
||||
throw new ConfigurationException("The '" + REF_ATTRIBUTE + "' attribute is required.");
|
||||
}
|
||||
if (StringUtils.hasText(element.getAttribute(METHOD_ATTRIBUTE))) {
|
||||
String method = element.getAttribute(METHOD_ATTRIBUTE);
|
||||
String adapterBeanName = this.parseAdapter(ref, method, element, parserContext);
|
||||
builder.addConstructorArgReference(adapterBeanName);
|
||||
}
|
||||
else {
|
||||
builder.addConstructorArgReference(ref);
|
||||
if (this.requiresBeanReference()) {
|
||||
String ref = element.getAttribute(REF_ATTRIBUTE);
|
||||
if (!StringUtils.hasText(ref)) {
|
||||
throw new ConfigurationException("The '" + REF_ATTRIBUTE + "' attribute is required.");
|
||||
}
|
||||
if (StringUtils.hasText(element.getAttribute(METHOD_ATTRIBUTE))) {
|
||||
String method = element.getAttribute(METHOD_ATTRIBUTE);
|
||||
String adapterBeanName = this.parseAdapter(ref, method, element, parserContext);
|
||||
builder.addConstructorArgReference(adapterBeanName);
|
||||
}
|
||||
else {
|
||||
builder.addConstructorArgReference(ref);
|
||||
}
|
||||
}
|
||||
String inputChannel = element.getAttribute(INPUT_CHANNEL_ATTRIBUTE);
|
||||
if (!StringUtils.hasText(inputChannel)) {
|
||||
@@ -110,10 +123,14 @@ public abstract class AbstractEndpointParser extends AbstractSingleBeanDefinitio
|
||||
}
|
||||
|
||||
private String parseAdapter(String ref, String method, Element element, ParserContext parserContext) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(this.getMethodInvokingAdapterClass());
|
||||
Class<?> adapterClass = this.getMethodInvokingAdapterClass();
|
||||
Assert.state(adapterClass != null,
|
||||
"Parser needs to create an adapter but 'getMethodInvokingAdapterClass()' returned null.");
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(adapterClass);
|
||||
builder.addConstructorArgReference(ref);
|
||||
builder.addConstructorArgValue(method);
|
||||
String adapterBeanName = BeanDefinitionReaderUtils.generateBeanName(builder.getBeanDefinition(), parserContext.getRegistry());
|
||||
String adapterBeanName = BeanDefinitionReaderUtils.generateBeanName(
|
||||
builder.getBeanDefinition(), parserContext.getRegistry());
|
||||
BeanDefinitionHolder holder = new BeanDefinitionHolder(builder.getBeanDefinition(), adapterBeanName);
|
||||
parserContext.registerBeanComponent(new BeanComponentDefinition(holder));
|
||||
return adapterBeanName;
|
||||
@@ -125,8 +142,14 @@ public abstract class AbstractEndpointParser extends AbstractSingleBeanDefinitio
|
||||
protected void postProcess(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclasses must override this if the adapted object is created from
|
||||
* the "ref" and "method" attribute values.
|
||||
*/
|
||||
protected Class<?> getMethodInvokingAdapterClass() {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected abstract Class<? extends MessageEndpoint> getEndpointClass();
|
||||
|
||||
protected abstract Class<?> getMethodInvokingAdapterClass();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,55 +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.transformer.config;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.transformer.AbstractPayloadTransformer;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public abstract class AbstractPayloadTransformerParser extends AbstractSingleBeanDefinitionParser {
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateId() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateIdAsFallback() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> getBeanClass(Element element) {
|
||||
return this.getTransformerClass();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
this.parsePayloadTransformer(element, parserContext, builder);
|
||||
}
|
||||
|
||||
protected abstract Class<? extends AbstractPayloadTransformer<?, ?>> getTransformerClass();
|
||||
|
||||
protected abstract void parsePayloadTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.transformer.config;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.config.AbstractEndpointParser;
|
||||
import org.springframework.integration.endpoint.MessageEndpoint;
|
||||
import org.springframework.integration.transformer.Transformer;
|
||||
import org.springframework.integration.transformer.TransformerEndpoint;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public abstract class AbstractTransformerParser extends AbstractEndpointParser {
|
||||
|
||||
@Override
|
||||
protected boolean requiresBeanReference() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<? extends MessageEndpoint> getEndpointClass() {
|
||||
return TransformerEndpoint.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void postProcess(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
BeanDefinitionBuilder transformerBuilder =
|
||||
BeanDefinitionBuilder.genericBeanDefinition(this.getTransformerClass());
|
||||
this.parsePayloadTransformer(element, parserContext, transformerBuilder);
|
||||
String transformerBeanName = BeanDefinitionReaderUtils.registerWithGeneratedName(
|
||||
transformerBuilder.getBeanDefinition(), parserContext.getRegistry());
|
||||
builder.addConstructorArgReference(transformerBeanName);
|
||||
}
|
||||
|
||||
protected abstract Class<? extends Transformer> getTransformerClass();
|
||||
|
||||
protected abstract void parsePayloadTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user