001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jms.config;
018
019import javax.resource.spi.ResourceAdapter;
020
021import org.springframework.jms.listener.endpoint.JmsActivationSpecConfig;
022import org.springframework.jms.listener.endpoint.JmsActivationSpecFactory;
023import org.springframework.jms.listener.endpoint.JmsMessageEndpointManager;
024import org.springframework.jms.support.destination.DestinationResolver;
025
026/**
027 * A {@link JmsListenerContainerFactory} implementation to build a
028 * JCA-based {@link JmsMessageEndpointManager}.
029 *
030 * @author Stephane Nicoll
031 * @since 4.1
032 */
033public class DefaultJcaListenerContainerFactory extends JmsActivationSpecConfig
034                implements JmsListenerContainerFactory<JmsMessageEndpointManager> {
035
036        private ResourceAdapter resourceAdapter;
037
038        private JmsActivationSpecFactory activationSpecFactory;
039
040        private DestinationResolver destinationResolver;
041
042        private Object transactionManager;
043
044        private Integer phase;
045
046
047        /**
048         * @see JmsMessageEndpointManager#setResourceAdapter(ResourceAdapter)
049         */
050        public void setResourceAdapter(ResourceAdapter resourceAdapter) {
051                this.resourceAdapter = resourceAdapter;
052        }
053
054        /**
055         * @see JmsMessageEndpointManager#setActivationSpecFactory(JmsActivationSpecFactory)
056         */
057        public void setActivationSpecFactory(JmsActivationSpecFactory activationSpecFactory) {
058                this.activationSpecFactory = activationSpecFactory;
059        }
060
061        /**
062         * @see JmsMessageEndpointManager#setDestinationResolver(DestinationResolver)
063         */
064        public void setDestinationResolver(DestinationResolver destinationResolver) {
065                this.destinationResolver = destinationResolver;
066        }
067
068        /**
069         * @see JmsMessageEndpointManager#setTransactionManager(Object)
070         */
071        public void setTransactionManager(Object transactionManager) {
072                this.transactionManager = transactionManager;
073        }
074
075        /**
076         * @see JmsMessageEndpointManager#setPhase(int)
077         */
078        public void setPhase(int phase) {
079                this.phase = phase;
080        }
081
082
083        @Override
084        public JmsMessageEndpointManager createListenerContainer(JmsListenerEndpoint endpoint) {
085                if (this.destinationResolver != null && this.activationSpecFactory != null) {
086                        throw new IllegalStateException("Specify either 'activationSpecFactory' or " +
087                                        "'destinationResolver', not both. If you define a dedicated JmsActivationSpecFactory bean, " +
088                                        "specify the custom DestinationResolver there (if possible)");
089                }
090
091                JmsMessageEndpointManager instance = createContainerInstance();
092
093                if (this.resourceAdapter != null) {
094                        instance.setResourceAdapter(this.resourceAdapter);
095                }
096                if (this.activationSpecFactory != null) {
097                        instance.setActivationSpecFactory(this.activationSpecFactory);
098                }
099                if (this.destinationResolver != null) {
100                        instance.setDestinationResolver(this.destinationResolver);
101                }
102                if (this.transactionManager != null) {
103                        instance.setTransactionManager(this.transactionManager);
104                }
105                if (this.phase != null) {
106                        instance.setPhase(this.phase);
107                }
108
109                instance.setActivationSpecConfig(this);
110                endpoint.setupListenerContainer(instance);
111
112                return instance;
113        }
114
115        /**
116         * Create an empty container instance.
117         */
118        protected JmsMessageEndpointManager createContainerInstance() {
119                return new JmsMessageEndpointManager();
120        }
121
122}