001/*
002 * Copyright 2002-2017 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;
025import org.springframework.lang.Nullable;
026
027/**
028 * A {@link JmsListenerContainerFactory} implementation to build a
029 * JCA-based {@link JmsMessageEndpointManager}.
030 *
031 * @author Stephane Nicoll
032 * @since 4.1
033 */
034public class DefaultJcaListenerContainerFactory extends JmsActivationSpecConfig
035                implements JmsListenerContainerFactory<JmsMessageEndpointManager> {
036
037        @Nullable
038        private ResourceAdapter resourceAdapter;
039
040        @Nullable
041        private JmsActivationSpecFactory activationSpecFactory;
042
043        @Nullable
044        private DestinationResolver destinationResolver;
045
046        @Nullable
047        private Object transactionManager;
048
049        @Nullable
050        private Integer phase;
051
052
053        /**
054         * @see JmsMessageEndpointManager#setResourceAdapter(ResourceAdapter)
055         */
056        public void setResourceAdapter(ResourceAdapter resourceAdapter) {
057                this.resourceAdapter = resourceAdapter;
058        }
059
060        /**
061         * @see JmsMessageEndpointManager#setActivationSpecFactory(JmsActivationSpecFactory)
062         */
063        public void setActivationSpecFactory(JmsActivationSpecFactory activationSpecFactory) {
064                this.activationSpecFactory = activationSpecFactory;
065        }
066
067        /**
068         * @see JmsMessageEndpointManager#setDestinationResolver(DestinationResolver)
069         */
070        public void setDestinationResolver(DestinationResolver destinationResolver) {
071                this.destinationResolver = destinationResolver;
072        }
073
074        /**
075         * @see JmsMessageEndpointManager#setTransactionManager(Object)
076         */
077        public void setTransactionManager(Object transactionManager) {
078                this.transactionManager = transactionManager;
079        }
080
081        /**
082         * @see JmsMessageEndpointManager#setPhase(int)
083         */
084        public void setPhase(int phase) {
085                this.phase = phase;
086        }
087
088
089        @Override
090        public JmsMessageEndpointManager createListenerContainer(JmsListenerEndpoint endpoint) {
091                if (this.destinationResolver != null && this.activationSpecFactory != null) {
092                        throw new IllegalStateException("Specify either 'activationSpecFactory' or " +
093                                        "'destinationResolver', not both. If you define a dedicated JmsActivationSpecFactory bean, " +
094                                        "specify the custom DestinationResolver there (if possible)");
095                }
096
097                JmsMessageEndpointManager instance = createContainerInstance();
098
099                if (this.resourceAdapter != null) {
100                        instance.setResourceAdapter(this.resourceAdapter);
101                }
102                if (this.activationSpecFactory != null) {
103                        instance.setActivationSpecFactory(this.activationSpecFactory);
104                }
105                if (this.destinationResolver != null) {
106                        instance.setDestinationResolver(this.destinationResolver);
107                }
108                if (this.transactionManager != null) {
109                        instance.setTransactionManager(this.transactionManager);
110                }
111                if (this.phase != null) {
112                        instance.setPhase(this.phase);
113                }
114
115                instance.setActivationSpecConfig(this);
116                endpoint.setupListenerContainer(instance);
117
118                return instance;
119        }
120
121        /**
122         * Create an empty container instance.
123         */
124        protected JmsMessageEndpointManager createContainerInstance() {
125                return new JmsMessageEndpointManager();
126        }
127
128}