Added AbstractChannelAdapterParser base class.

This commit is contained in:
Mark Fisher
2008-09-19 23:24:15 +00:00
parent f3632fd330
commit a14246bfca
3 changed files with 87 additions and 85 deletions

View File

@@ -0,0 +1,79 @@
/*
* 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.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.util.StringUtils;
/**
* Base parser for Channel Adapters.
*
* @author Mark Fisher
*/
public abstract class AbstractChannelAdapterParser extends AbstractBeanDefinitionParser {
@Override
protected final String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) throws BeanDefinitionStoreException {
String id = element.getAttribute("id");
if (!element.hasAttribute("channel")) {
// the created channel will get the 'id', so the adapter's bean name includes a suffix
id = id + ".adapter";
}
else if (!StringUtils.hasText(id)) {
id = parserContext.getReaderContext().generateBeanName(definition);
}
return id;
}
@Override
protected final AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
String channelName = element.getAttribute("channel");
if (!StringUtils.hasText(channelName)) {
channelName = this.createDirectChannel(element, parserContext);
}
return doParse(element, parserContext, channelName);
}
private String createDirectChannel(Element element, ParserContext parserContext) {
String channelId = element.getAttribute("id");
if (!StringUtils.hasText(channelId)) {
throw new ConfigurationException("The channel-adapter's 'id' attribute is required when no 'channel' "
+ "reference has been provided, because that 'id' would be used for the created channel.");
}
BeanDefinitionBuilder channelBuilder = BeanDefinitionBuilder.genericBeanDefinition(DirectChannel.class);
BeanDefinitionHolder holder = new BeanDefinitionHolder(channelBuilder.getBeanDefinition(), channelId);
BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry());
return channelId;
}
/**
* Subclasses must implement this method to parse the adapter element.
* The name of the MessageChannel bean is provided.
*/
protected abstract AbstractBeanDefinition doParse(Element element, ParserContext parserContext, String channelName);
}

View File

@@ -18,15 +18,10 @@ package org.springframework.integration.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
import org.springframework.integration.scheduling.PollingSchedule;
import org.springframework.util.StringUtils;
@@ -37,37 +32,18 @@ import org.springframework.util.xml.DomUtils;
*
* @author Mark Fisher
*/
public abstract class AbstractPollingInboundChannelAdapterParser extends AbstractBeanDefinitionParser {
public abstract class AbstractPollingInboundChannelAdapterParser extends AbstractChannelAdapterParser {
@Override
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) throws BeanDefinitionStoreException {
String id = element.getAttribute("id");
if (!element.hasAttribute("channel")) {
// the created channel will get the 'id', so the adapter's bean name includes a suffix
id = id + ".adapter";
}
else if (!StringUtils.hasText(id)) {
id = parserContext.getReaderContext().generateBeanName(definition);
}
return id;
}
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
protected AbstractBeanDefinition doParse(Element element, ParserContext parserContext, String channelName) {
String source = this.parseSource(element, parserContext);
if (!StringUtils.hasText(source)) {
throw new ConfigurationException("failed to parse source");
}
String channelName = element.getAttribute("channel");
Element pollerElement = DomUtils.getChildElementByTagName(element, "poller");
BeanDefinitionBuilder adapterBuilder = BeanDefinitionBuilder.genericBeanDefinition(SourcePollingChannelAdapter.class);
adapterBuilder.addPropertyReference("source", source);
if (StringUtils.hasText(channelName)) {
adapterBuilder.addPropertyReference("outputChannel", channelName);
}
else {
adapterBuilder.addPropertyReference("outputChannel", this.createDirectChannel(element, parserContext));
}
adapterBuilder.addPropertyReference("outputChannel", channelName);
if (pollerElement != null) {
IntegrationNamespaceUtils.configureSchedule(pollerElement, adapterBuilder);
IntegrationNamespaceUtils.setValueIfAttributeDefined(adapterBuilder, pollerElement, "max-messages-per-poll");
@@ -88,16 +64,4 @@ public abstract class AbstractPollingInboundChannelAdapterParser extends Abstrac
*/
protected abstract String parseSource(Element element, ParserContext parserContext);
private String createDirectChannel(Element element, ParserContext parserContext) {
String channelId = element.getAttribute("id");
if (!StringUtils.hasText(channelId)) {
throw new ConfigurationException("The channel-adapter's 'id' attribute is required when no 'channel' "
+ "reference has been provided, because that 'id' would be used for the created channel.");
}
BeanDefinitionBuilder channelBuilder = BeanDefinitionBuilder.genericBeanDefinition(DirectChannel.class);
BeanDefinitionHolder holder = new BeanDefinitionHolder(channelBuilder.getBeanDefinition(), channelId);
BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry());
return channelId;
}
}

View File

@@ -18,17 +18,13 @@ package org.springframework.integration.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
import org.springframework.integration.endpoint.OutboundChannelAdapter;
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
import org.springframework.integration.handler.MethodInvokingConsumer;
import org.springframework.integration.message.MethodInvokingSource;
import org.springframework.util.StringUtils;
@@ -39,26 +35,12 @@ import org.springframework.util.xml.DomUtils;
*
* @author Mark Fisher
*/
public class ChannelAdapterParser extends AbstractBeanDefinitionParser {
public class ChannelAdapterParser extends AbstractChannelAdapterParser {
@Override
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) throws BeanDefinitionStoreException {
String id = element.getAttribute("id");
if (!element.hasAttribute("channel")) {
// the created channel will get the 'id', so the adapter's bean name includes a suffix
id = id + ".adapter";
}
else if (!StringUtils.hasText(id)) {
id = parserContext.getReaderContext().generateBeanName(definition);
}
return id;
}
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
protected AbstractBeanDefinition doParse(Element element, ParserContext parserContext, String channelName) {
String source = element.getAttribute("source");
String target = element.getAttribute("target");
String channelName = element.getAttribute("channel");
String methodName = element.getAttribute("method");
Element pollerElement = DomUtils.getChildElementByTagName(element, "poller");
BeanDefinitionBuilder adapterBuilder = null;
@@ -74,12 +56,7 @@ public class ChannelAdapterParser extends AbstractBeanDefinitionParser {
}
adapterBuilder = BeanDefinitionBuilder.genericBeanDefinition(SourcePollingChannelAdapter.class);
adapterBuilder.addPropertyReference("source", source);
if (StringUtils.hasText(channelName)) {
adapterBuilder.addPropertyReference("outputChannel", channelName);
}
else {
adapterBuilder.addPropertyReference("outputChannel", this.createDirectChannel(element, parserContext));
}
adapterBuilder.addPropertyReference("outputChannel", channelName);
if (pollerElement != null) {
IntegrationNamespaceUtils.configureSchedule(pollerElement, adapterBuilder);
IntegrationNamespaceUtils.setValueIfAttributeDefined(adapterBuilder, pollerElement, "max-messages-per-poll");
@@ -108,13 +85,7 @@ public class ChannelAdapterParser extends AbstractBeanDefinitionParser {
IntegrationNamespaceUtils.configureTransactionAttributes(txElement, adapterBuilder);
}
}
if (StringUtils.hasText(channelName)) {
adapterBuilder.addPropertyReference("inputChannel", channelName);
}
else {
adapterBuilder.addPropertyReference("inputChannel",
this.createDirectChannel(element, parserContext));
}
adapterBuilder.addPropertyReference("inputChannel", channelName);
}
else {
throw new ConfigurationException("either 'source' or 'target' is required");
@@ -122,16 +93,4 @@ public class ChannelAdapterParser extends AbstractBeanDefinitionParser {
return adapterBuilder.getBeanDefinition();
}
private String createDirectChannel(Element element, ParserContext parserContext) {
String channelId = element.getAttribute("id");
if (!StringUtils.hasText(channelId)) {
throw new ConfigurationException("The channel-adapter's 'id' attribute is required when no 'channel' "
+ "reference has been provided, because that 'id' would be used for the created channel.");
}
BeanDefinitionBuilder channelBuilder = BeanDefinitionBuilder.genericBeanDefinition(DirectChannel.class);
BeanDefinitionHolder holder = new BeanDefinitionHolder(channelBuilder.getBeanDefinition(), channelId);
BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry());
return channelId;
}
}