Merge pull request #489 from ghillert/INT-2546

* INT-2546:
  INT-2546 Move method PollerParser.configureTransactionAttributes to IntegrationNamespaceUtils
This commit is contained in:
Oleg Zhurakousky
2012-06-12 12:33:37 -04:00
5 changed files with 52 additions and 72 deletions

View File

@@ -1,11 +1,11 @@
/* /*
* Copyright 2002-2010 the original author or authors. * Copyright 2002-2012 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 * 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 * the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * 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 * 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 * 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. * specific language governing permissions and limitations under the License.
@@ -25,6 +25,10 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate; import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
import org.springframework.beans.factory.xml.ParserContext; import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.core.Conventions; import org.springframework.core.Conventions;
import org.springframework.integration.endpoint.AbstractPollingEndpoint;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils; import org.springframework.util.xml.DomUtils;
@@ -32,13 +36,15 @@ import org.w3c.dom.Element;
/** /**
* Shared utility methods for integration namespace parsers. * Shared utility methods for integration namespace parsers.
* *
* @author Mark Fisher * @author Mark Fisher
* @author Marius Bogoevici * @author Marius Bogoevici
* @author Alex Peters * @author Alex Peters
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
* @author Gary Russell * @author Gary Russell
* @author Artem Bilan * @author Artem Bilan
* @author Gunnar Hillert
*
*/ */
public abstract class IntegrationNamespaceUtils { public abstract class IntegrationNamespaceUtils {
@@ -53,7 +59,7 @@ public abstract class IntegrationNamespaceUtils {
/** /**
* Configures the provided bean definition builder with a property value corresponding to the attribute whose name * Configures the provided bean definition builder with a property value corresponding to the attribute whose name
* is provided if that attribute is defined in the given element. * is provided if that attribute is defined in the given element.
* *
* @param builder the bean definition builder to be configured * @param builder the bean definition builder to be configured
* @param element the XML element where the attribute should be defined * @param element the XML element where the attribute should be defined
* @param attributeName the name of the attribute whose value will be used to populate the property * @param attributeName the name of the attribute whose value will be used to populate the property
@@ -70,13 +76,13 @@ public abstract class IntegrationNamespaceUtils {
/** /**
* Configures the provided bean definition builder with a property value corresponding to the attribute whose name * Configures the provided bean definition builder with a property value corresponding to the attribute whose name
* is provided if that attribute is defined in the given element. * is provided if that attribute is defined in the given element.
* *
* <p> * <p>
* The property name will be the camel-case equivalent of the lower case hyphen separated attribute (e.g. the * The property name will be the camel-case equivalent of the lower case hyphen separated attribute (e.g. the
* "foo-bar" attribute would match the "fooBar" property). * "foo-bar" attribute would match the "fooBar" property).
* *
* @see Conventions#attributeNameToPropertyName(String) * @see Conventions#attributeNameToPropertyName(String)
* *
* @param builder the bean definition builder to be configured * @param builder the bean definition builder to be configured
* @param element - the XML element where the attribute should be defined * @param element - the XML element where the attribute should be defined
* @param attributeName - the name of the attribute whose value will be set on the property * @param attributeName - the name of the attribute whose value will be set on the property
@@ -90,7 +96,7 @@ public abstract class IntegrationNamespaceUtils {
* Configures the provided bean definition builder with a property reference to a bean. The bean reference is * Configures the provided bean definition builder with a property reference to a bean. The bean reference is
* identified by the value from the attribute whose name is provided if that attribute is defined in the given * identified by the value from the attribute whose name is provided if that attribute is defined in the given
* element. * element.
* *
* @param builder the bean definition builder to be configured * @param builder the bean definition builder to be configured
* @param element the XML element where the attribute should be defined * @param element the XML element where the attribute should be defined
* @param attributeName the name of the attribute whose value will be used as a bean reference to populate the * @param attributeName the name of the attribute whose value will be used as a bean reference to populate the
@@ -109,18 +115,18 @@ public abstract class IntegrationNamespaceUtils {
* Configures the provided bean definition builder with a property reference to a bean. The bean reference is * Configures the provided bean definition builder with a property reference to a bean. The bean reference is
* identified by the value from the attribute whose name is provided if that attribute is defined in the given * identified by the value from the attribute whose name is provided if that attribute is defined in the given
* element. * element.
* *
* <p> * <p>
* The property name will be the camel-case equivalent of the lower case hyphen separated attribute (e.g. the * The property name will be the camel-case equivalent of the lower case hyphen separated attribute (e.g. the
* "foo-bar" attribute would match the "fooBar" property). * "foo-bar" attribute would match the "fooBar" property).
* *
* @see Conventions#attributeNameToPropertyName(String) * @see Conventions#attributeNameToPropertyName(String)
* *
* @param builder the bean definition builder to be configured * @param builder the bean definition builder to be configured
* @param element - the XML element where the attribute should be defined * @param element - the XML element where the attribute should be defined
* @param attributeName - the name of the attribute whose value will be used as a bean reference to populate the * @param attributeName - the name of the attribute whose value will be used as a bean reference to populate the
* property * property
* *
* @see Conventions#attributeNameToPropertyName(String) * @see Conventions#attributeNameToPropertyName(String)
*/ */
public static void setReferenceIfAttributeDefined(BeanDefinitionBuilder builder, Element element, public static void setReferenceIfAttributeDefined(BeanDefinitionBuilder builder, Element element,
@@ -146,7 +152,7 @@ public abstract class IntegrationNamespaceUtils {
* Parse a "poller" element to provide a reference for the target BeanDefinitionBuilder. If the poller element does * Parse a "poller" element to provide a reference for the target BeanDefinitionBuilder. If the poller element does
* not contain a "ref" attribute, this will create and register a PollerMetadata instance and then add it as a * not contain a "ref" attribute, this will create and register a PollerMetadata instance and then add it as a
* property reference of the target builder. * property reference of the target builder.
* *
* @param pollerElement the "poller" element to parse * @param pollerElement the "poller" element to parse
* @param targetBuilder the builder that expects the "trigger" property * @param targetBuilder the builder that expects the "trigger" property
* @param parserContext the parserContext for the target builder * @param parserContext the parserContext for the target builder
@@ -185,7 +191,7 @@ public abstract class IntegrationNamespaceUtils {
/** /**
* Get a text value from a named attribute if it exists, otherwise check for a nested element of the same name. If * Get a text value from a named attribute if it exists, otherwise check for a nested element of the same name. If
* both are specified it is an error, but if neither is specified, just returns null. * both are specified it is an error, but if neither is specified, just returns null.
* *
* @param element a DOM node * @param element a DOM node
* @param name the name of the property (attribute or child element) * @param name the name of the property (attribute or child element)
* @param parserContext the current context * @param parserContext the current context
@@ -236,29 +242,50 @@ public abstract class IntegrationNamespaceUtils {
boolean hasHeaderMapper = element.hasAttribute("header-mapper"); boolean hasHeaderMapper = element.hasAttribute("header-mapper");
boolean hasMappedRequestHeaders = element.hasAttribute("mapped-request-headers"); boolean hasMappedRequestHeaders = element.hasAttribute("mapped-request-headers");
boolean hasMappedReplyHeaders = element.hasAttribute(replyHeaderValue); boolean hasMappedReplyHeaders = element.hasAttribute(replyHeaderValue);
if (hasHeaderMapper && (hasMappedRequestHeaders || hasMappedReplyHeaders)){ if (hasHeaderMapper && (hasMappedRequestHeaders || hasMappedReplyHeaders)){
parserContext.getReaderContext().error("The 'header-mapper' attribute is mutually exclusive with" + parserContext.getReaderContext().error("The 'header-mapper' attribute is mutually exclusive with" +
" 'mapped-request-headers' or 'mapped-reply-headers'. " + " 'mapped-request-headers' or 'mapped-reply-headers'. " +
"You can only use one or the others", element); "You can only use one or the others", element);
} }
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(rootBuilder, element, "header-mapper"); IntegrationNamespaceUtils.setReferenceIfAttributeDefined(rootBuilder, element, "header-mapper");
if (hasMappedRequestHeaders || hasMappedReplyHeaders){ if (hasMappedRequestHeaders || hasMappedReplyHeaders){
BeanDefinitionBuilder headerMapperBuilder = BeanDefinitionBuilder.genericBeanDefinition(headerMapperClass); BeanDefinitionBuilder headerMapperBuilder = BeanDefinitionBuilder.genericBeanDefinition(headerMapperClass);
if (hasMappedRequestHeaders) { if (hasMappedRequestHeaders) {
headerMapperBuilder.addPropertyValue("requestHeaderNames", element.getAttribute("mapped-request-headers")); headerMapperBuilder.addPropertyValue("requestHeaderNames", element.getAttribute("mapped-request-headers"));
} }
if (hasMappedReplyHeaders) { if (hasMappedReplyHeaders) {
headerMapperBuilder.addPropertyValue("replyHeaderNames", element.getAttribute(replyHeaderValue)); headerMapperBuilder.addPropertyValue("replyHeaderNames", element.getAttribute(replyHeaderValue));
} }
rootBuilder.addPropertyValue("headerMapper", headerMapperBuilder.getBeanDefinition()); rootBuilder.addPropertyValue("headerMapper", headerMapperBuilder.getBeanDefinition());
} }
} }
/**
* Parse a "transactional" element and configure a {@link TransactionInterceptor}
* with "transactionManager" and other "transactionDefinition" properties.
* For example, this advisor will be applied on the Polling Task proxy
*
* @see AbstractPollingEndpoint
*/
public static BeanDefinition configureTransactionAttributes(Element txElement) {
BeanDefinitionBuilder txDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(DefaultTransactionAttribute.class);
txDefinitionBuilder.addPropertyValue("propagationBehaviorName", "PROPAGATION_" + txElement.getAttribute("propagation"));
txDefinitionBuilder.addPropertyValue("isolationLevelName", "ISOLATION_" + txElement.getAttribute("isolation"));
txDefinitionBuilder.addPropertyValue("timeout", txElement.getAttribute("timeout"));
txDefinitionBuilder.addPropertyValue("readOnly", txElement.getAttribute("read-only"));
BeanDefinitionBuilder attributeSourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(MatchAlwaysTransactionAttributeSource.class);
attributeSourceBuilder.addPropertyValue("transactionAttribute", txDefinitionBuilder.getBeanDefinition());
BeanDefinitionBuilder txInterceptorBuilder = BeanDefinitionBuilder.genericBeanDefinition(TransactionInterceptor.class);
txInterceptorBuilder.addPropertyReference("transactionManager", txElement.getAttribute("transaction-manager"));
txInterceptorBuilder.addPropertyValue("transactionAttributeSource", attributeSourceBuilder.getBeanDefinition());
return txInterceptorBuilder.getBeanDefinition();
}
public static String[] generateAlias(Element element) { public static String[] generateAlias(Element element) {
String[] handlerAlias = null; String[] handlerAlias = null;
String id = element.getAttribute(ID_ATTRIBUTE); String id = element.getAttribute(ID_ATTRIBUTE);

View File

@@ -39,15 +39,12 @@ import org.springframework.integration.channel.MessagePublishingErrorHandler;
import org.springframework.integration.scheduling.PollerMetadata; import org.springframework.integration.scheduling.PollerMetadata;
import org.springframework.scheduling.support.CronTrigger; import org.springframework.scheduling.support.CronTrigger;
import org.springframework.scheduling.support.PeriodicTrigger; import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils; import org.springframework.util.xml.DomUtils;
/** /**
* Parser for the &lt;poller&gt; element. * Parser for the &lt;poller&gt; element.
* *
* @author Mark Fisher * @author Mark Fisher
* @author Marius Bogoevici * @author Marius Bogoevici
* @author Oleg Zhurakousky * @author Oleg Zhurakousky
@@ -162,25 +159,6 @@ public class PollerParser extends AbstractBeanDefinitionParser {
targetBuilder.addPropertyReference("trigger", triggerBeanNames.get(0)); targetBuilder.addPropertyReference("trigger", triggerBeanNames.get(0));
} }
/**
* Parse a "transactional" element and configure a TransactionInterceptor with "transactionManager"
* and other "transactionDefinition" properties. This advisor will be applied on the Polling Task proxy
* (see {@link org.springframework.integration.endpoint.AbstractPollingEndpoint}).
*/
private BeanDefinition configureTransactionAttributes(Element txElement) {
BeanDefinitionBuilder txDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(DefaultTransactionAttribute.class);
txDefinitionBuilder.addPropertyValue("propagationBehaviorName", "PROPAGATION_" + txElement.getAttribute("propagation"));
txDefinitionBuilder.addPropertyValue("isolationLevelName", "ISOLATION_" + txElement.getAttribute("isolation"));
txDefinitionBuilder.addPropertyValue("timeout", txElement.getAttribute("timeout"));
txDefinitionBuilder.addPropertyValue("readOnly", txElement.getAttribute("read-only"));
BeanDefinitionBuilder attributeSourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(MatchAlwaysTransactionAttributeSource.class);
attributeSourceBuilder.addPropertyValue("transactionAttribute", txDefinitionBuilder.getBeanDefinition());
BeanDefinitionBuilder txInterceptorBuilder = BeanDefinitionBuilder.genericBeanDefinition(TransactionInterceptor.class);
txInterceptorBuilder.addPropertyReference("transactionManager", txElement.getAttribute("transaction-manager"));
txInterceptorBuilder.addPropertyValue("transactionAttributeSource", attributeSourceBuilder.getBeanDefinition());
return txInterceptorBuilder.getBeanDefinition();
}
/** /**
* Parses the 'advice-chain' element's sub-elements. * Parses the 'advice-chain' element's sub-elements.
*/ */
@@ -189,7 +167,7 @@ public class PollerParser extends AbstractBeanDefinitionParser {
ManagedList adviceChain = new ManagedList(); ManagedList adviceChain = new ManagedList();
// Schema validation ensures txElement and adviceChainElement are mutually exclusive // Schema validation ensures txElement and adviceChainElement are mutually exclusive
if (txElement != null) { if (txElement != null) {
adviceChain.add(this.configureTransactionAttributes(txElement)); adviceChain.add(IntegrationNamespaceUtils.configureTransactionAttributes(txElement));
} }
if (adviceChainElement != null) { if (adviceChainElement != null) {
NodeList childNodes = adviceChainElement.getChildNodes(); NodeList childNodes = adviceChainElement.getChildNodes();

View File

@@ -56,7 +56,7 @@ public abstract class AbstractJpaOutboundGatewayParser extends AbstractConsumerE
final Element transactionalElement = DomUtils.getChildElementByTagName(gatewayElement, "transactional"); final Element transactionalElement = DomUtils.getChildElementByTagName(gatewayElement, "transactional");
if (transactionalElement != null) { if (transactionalElement != null) {
BeanDefinition txAdviceDefinition = JpaParserUtils.configureTransactionAttributes(transactionalElement); BeanDefinition txAdviceDefinition = IntegrationNamespaceUtils.configureTransactionAttributes(transactionalElement);
ManagedList<BeanDefinition> adviceChain = new ManagedList<BeanDefinition>(); ManagedList<BeanDefinition> adviceChain = new ManagedList<BeanDefinition>();
adviceChain.add(txAdviceDefinition); adviceChain.add(txAdviceDefinition);
jpaOutboundGatewayBuilder.addPropertyValue("adviceChain", adviceChain); jpaOutboundGatewayBuilder.addPropertyValue("adviceChain", adviceChain);

View File

@@ -70,7 +70,7 @@ public class JpaOutboundChannelAdapterParser extends AbstractOutboundChannelAdap
final Element transactionalElement = DomUtils.getChildElementByTagName(element, "transactional"); final Element transactionalElement = DomUtils.getChildElementByTagName(element, "transactional");
if(transactionalElement != null) { if(transactionalElement != null) {
BeanDefinition txAdviceDefinition = JpaParserUtils.configureTransactionAttributes(transactionalElement); BeanDefinition txAdviceDefinition = IntegrationNamespaceUtils.configureTransactionAttributes(transactionalElement);
ManagedList<BeanDefinition> adviceChain = new ManagedList<BeanDefinition>(); ManagedList<BeanDefinition> adviceChain = new ManagedList<BeanDefinition>();
adviceChain.add(txAdviceDefinition); adviceChain.add(txAdviceDefinition);
jpaOutboundChannelAdapterBuilder.addPropertyValue("adviceChain", adviceChain); jpaOutboundChannelAdapterBuilder.addPropertyValue("adviceChain", adviceChain);

View File

@@ -25,12 +25,8 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList; import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.ParserContext; import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils; import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.integration.endpoint.AbstractPollingEndpoint;
import org.springframework.integration.jpa.core.JpaExecutor; import org.springframework.integration.jpa.core.JpaExecutor;
import org.springframework.integration.jpa.support.JpaParameter; import org.springframework.integration.jpa.support.JpaParameter;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils; import org.springframework.util.xml.DomUtils;
@@ -214,25 +210,4 @@ public final class JpaParserUtils {
} }
/**
* Parse a "transactional" element and configure a TransactionInterceptor with "transactionManager"
* and other "transactionDefinition" properties. This advisor will be applied on the Polling Task proxy
* (see {@link AbstractPollingEndpoint}).
*
* FIXME Copied from {@link org.springframework.integration.config.xml.PollerParser} - should be refactored.
*/
public static BeanDefinition configureTransactionAttributes(Element txElement) {
BeanDefinitionBuilder txDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(DefaultTransactionAttribute.class);
txDefinitionBuilder.addPropertyValue("propagationBehaviorName", "PROPAGATION_" + txElement.getAttribute("propagation"));
txDefinitionBuilder.addPropertyValue("isolationLevelName", "ISOLATION_" + txElement.getAttribute("isolation"));
txDefinitionBuilder.addPropertyValue("timeout", txElement.getAttribute("timeout"));
txDefinitionBuilder.addPropertyValue("readOnly", txElement.getAttribute("read-only"));
BeanDefinitionBuilder attributeSourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(MatchAlwaysTransactionAttributeSource.class);
attributeSourceBuilder.addPropertyValue("transactionAttribute", txDefinitionBuilder.getBeanDefinition());
BeanDefinitionBuilder txInterceptorBuilder = BeanDefinitionBuilder.genericBeanDefinition(TransactionInterceptor.class);
txInterceptorBuilder.addPropertyReference("transactionManager", txElement.getAttribute("transaction-manager"));
txInterceptorBuilder.addPropertyValue("transactionAttributeSource", attributeSourceBuilder.getBeanDefinition());
return txInterceptorBuilder.getBeanDefinition();
}
} }