JcaListenerContainerParser.java 5.6 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
2
 * Copyright 2002-2014 the original author or authors.
A
Arjen Poutsma 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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.jms.config;

19 20
import org.w3c.dom.Element;

21 22
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValues;
A
Arjen Poutsma 已提交
23 24 25 26 27 28
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;

/**
29
 * Parser for the JMS {@code <jca-listener-container>} element.
A
Arjen Poutsma 已提交
30 31
 *
 * @author Juergen Hoeller
32
 * @author Stephane Nicoll
A
Arjen Poutsma 已提交
33 34 35 36 37 38 39 40 41
 * @since 2.5
 */
class JcaListenerContainerParser extends AbstractListenerContainerParser {

	private static final String RESOURCE_ADAPTER_ATTRIBUTE = "resource-adapter";

	private static final String ACTIVATION_SPEC_FACTORY_ATTRIBUTE = "activation-spec-factory";


42
	@Override
43 44
	protected RootBeanDefinition createContainerFactory(String factoryId, Element containerEle, ParserContext parserContext,
			PropertyValues commonContainerProperties, PropertyValues specificContainerProperties) {
45

46 47
		RootBeanDefinition factoryDef = new RootBeanDefinition();
		factoryDef.setBeanClassName("org.springframework.jms.config.DefaultJcaListenerContainerFactory");
48

49 50
		factoryDef.getPropertyValues().addPropertyValues(commonContainerProperties);
		factoryDef.getPropertyValues().addPropertyValues(specificContainerProperties);
51

52
		return factoryDef;
53 54 55
	}

	@Override
56 57 58
	protected RootBeanDefinition createContainer(Element containerEle, Element listenerEle, ParserContext parserContext,
			PropertyValues commonContainerProperties, PropertyValues specificContainerProperties) {

A
Arjen Poutsma 已提交
59
		RootBeanDefinition containerDef = new RootBeanDefinition();
60
		containerDef.setSource(parserContext.extractSource(containerEle));
A
Arjen Poutsma 已提交
61
		containerDef.setBeanClassName("org.springframework.jms.listener.endpoint.JmsMessageEndpointManager");
62
		containerDef.getPropertyValues().addPropertyValues(specificContainerProperties);
63

64 65 66 67 68
		RootBeanDefinition configDef = new RootBeanDefinition();
		configDef.setSource(parserContext.extractSource(containerEle));
		configDef.setBeanClassName("org.springframework.jms.listener.endpoint.JmsActivationSpecConfig");
		configDef.getPropertyValues().addPropertyValues(commonContainerProperties);
		parseListenerConfiguration(listenerEle, parserContext, configDef.getPropertyValues());
69

70
		containerDef.getPropertyValues().add("activationSpecConfig", configDef);
71

72 73
		return containerDef;
	}
74

75 76 77 78 79 80 81
	@Override
	protected MutablePropertyValues parseCommonContainerProperties(Element containerEle, ParserContext parserContext) {
		MutablePropertyValues properties = super.parseCommonContainerProperties(containerEle, parserContext);

		Integer acknowledgeMode = parseAcknowledgeMode(containerEle, parserContext);
		if (acknowledgeMode != null) {
			properties.add("acknowledgeMode", acknowledgeMode);
82 83
		}

84 85 86 87
		String concurrency = containerEle.getAttribute(CONCURRENCY_ATTRIBUTE);
		if (StringUtils.hasText(concurrency)) {
			properties.add("concurrency", concurrency);
		}
88

89 90 91 92
		String prefetch = containerEle.getAttribute(PREFETCH_ATTRIBUTE);
		if (StringUtils.hasText(prefetch)) {
			properties.add("prefetchSize", new Integer(prefetch));
		}
93

94
		return properties;
95 96 97
	}

	@Override
98 99
	protected MutablePropertyValues parseSpecificContainerProperties(Element containerEle, ParserContext parserContext) {
		MutablePropertyValues properties = new MutablePropertyValues();
100

A
Arjen Poutsma 已提交
101
		if (containerEle.hasAttribute(RESOURCE_ADAPTER_ATTRIBUTE)) {
102
			String resourceAdapterBeanName = containerEle.getAttribute(RESOURCE_ADAPTER_ATTRIBUTE);
A
Arjen Poutsma 已提交
103 104 105 106
			if (!StringUtils.hasText(resourceAdapterBeanName)) {
				parserContext.getReaderContext().error(
						"Listener container 'resource-adapter' attribute contains empty value.", containerEle);
			}
107
			else {
108
				properties.add("resourceAdapter", new RuntimeBeanReference(resourceAdapterBeanName));
109
			}
A
Arjen Poutsma 已提交
110 111 112 113 114 115 116 117 118 119
		}

		String activationSpecFactoryBeanName = containerEle.getAttribute(ACTIVATION_SPEC_FACTORY_ATTRIBUTE);
		String destinationResolverBeanName = containerEle.getAttribute(DESTINATION_RESOLVER_ATTRIBUTE);
		if (StringUtils.hasText(activationSpecFactoryBeanName)) {
			if (StringUtils.hasText(destinationResolverBeanName)) {
				parserContext.getReaderContext().error("Specify either 'activation-spec-factory' or " +
						"'destination-resolver', not both. If you define a dedicated JmsActivationSpecFactory bean, " +
						"specify the custom DestinationResolver there (if possible).", containerEle);
			}
120
			properties.add("activationSpecFactory", new RuntimeBeanReference(activationSpecFactoryBeanName));
A
Arjen Poutsma 已提交
121 122
		}
		if (StringUtils.hasText(destinationResolverBeanName)) {
123
			properties.add("destinationResolver", new RuntimeBeanReference(destinationResolverBeanName));
A
Arjen Poutsma 已提交
124 125
		}

126 127
		String transactionManagerBeanName = containerEle.getAttribute(TRANSACTION_MANAGER_ATTRIBUTE);
		if (StringUtils.hasText(transactionManagerBeanName)) {
128
			properties.add("transactionManager", new RuntimeBeanReference(transactionManagerBeanName));
A
Arjen Poutsma 已提交
129 130
		}

131 132 133
		String phase = containerEle.getAttribute(PHASE_ATTRIBUTE);
		if (StringUtils.hasText(phase)) {
			properties.add("phase", phase);
134 135
		}

136
		return properties;
A
Arjen Poutsma 已提交
137 138 139
	}

}