From 5a0677af00a6b61b2524635da11bb2ef5708289e Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Wed, 17 Sep 2008 21:30:12 +0000 Subject: [PATCH] Added AbstractPollingInboundChannelAdapterParser. --- .../stream/config/ConsoleSourceParser.java | 4 +- ...actPollingInboundChannelAdapterParser.java | 103 ++++++++++++++++++ 2 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 org.springframework.integration/src/main/java/org/springframework/integration/config/AbstractPollingInboundChannelAdapterParser.java diff --git a/org.springframework.integration.stream/src/main/java/org/springframework/integration/stream/config/ConsoleSourceParser.java b/org.springframework.integration.stream/src/main/java/org/springframework/integration/stream/config/ConsoleSourceParser.java index 4d00f4e2db..f0ef8c019e 100644 --- a/org.springframework.integration.stream/src/main/java/org/springframework/integration/stream/config/ConsoleSourceParser.java +++ b/org.springframework.integration.stream/src/main/java/org/springframework/integration/stream/config/ConsoleSourceParser.java @@ -21,7 +21,7 @@ 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.AbstractInboundChannelAdapterParser; +import org.springframework.integration.config.AbstractPollingInboundChannelAdapterParser; import org.springframework.integration.stream.CharacterStreamSource; import org.springframework.util.StringUtils; @@ -30,7 +30,7 @@ import org.springframework.util.StringUtils; * * @author Mark Fisher */ -public class ConsoleSourceParser extends AbstractInboundChannelAdapterParser { +public class ConsoleSourceParser extends AbstractPollingInboundChannelAdapterParser { @Override protected String parseSource(Element element, ParserContext parserContext) { diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/config/AbstractPollingInboundChannelAdapterParser.java b/org.springframework.integration/src/main/java/org/springframework/integration/config/AbstractPollingInboundChannelAdapterParser.java new file mode 100644 index 0000000000..90fe576978 --- /dev/null +++ b/org.springframework.integration/src/main/java/org/springframework/integration/config/AbstractPollingInboundChannelAdapterParser.java @@ -0,0 +1,103 @@ +/* + * 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.integration.endpoint.InboundChannelAdapter; +import org.springframework.integration.scheduling.PollingSchedule; +import org.springframework.util.StringUtils; +import org.springframework.util.xml.DomUtils; + +/** + * Base parser for inbound Channel Adapters that poll a source. + * + * @author Mark Fisher + */ +public abstract class AbstractPollingInboundChannelAdapterParser extends AbstractBeanDefinitionParser { + + @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) { + 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(InboundChannelAdapter.class); + adapterBuilder.addPropertyReference("source", source); + if (StringUtils.hasText(channelName)) { + adapterBuilder.addPropertyReference("channel", channelName); + } + else { + adapterBuilder.addPropertyReference("channel", this.createDirectChannel(element, parserContext)); + } + if (pollerElement != null) { + IntegrationNamespaceUtils.configureSchedule(pollerElement, adapterBuilder); + IntegrationNamespaceUtils.setValueIfAttributeDefined(adapterBuilder, pollerElement, "max-messages-per-poll"); + Element txElement = DomUtils.getChildElementByTagName(pollerElement, "transactional"); + if (txElement != null) { + IntegrationNamespaceUtils.configureTransactionAttributes(txElement, adapterBuilder); + } + } + else { + adapterBuilder.addPropertyValue("schedule", new PollingSchedule(0)); + } + return adapterBuilder.getBeanDefinition(); + } + + /** + * Subclasses must implement this method to parse the PollableSource instance + * which the created Channel Adapter will poll. + */ + 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; + } + +}