001/*
002 * Copyright 2002-2018 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.core.support;
018
019import javax.jms.ConnectionFactory;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023
024import org.springframework.beans.factory.BeanInitializationException;
025import org.springframework.beans.factory.InitializingBean;
026import org.springframework.jms.core.JmsTemplate;
027import org.springframework.lang.Nullable;
028
029/**
030 * Convenient super class for application classes that need JMS access.
031 *
032 * <p>Requires a ConnectionFactory or a JmsTemplate instance to be set.
033 * It will create its own JmsTemplate if a ConnectionFactory is passed in.
034 * A custom JmsTemplate instance can be created for a given ConnectionFactory
035 * through overriding the {@link #createJmsTemplate} method.
036 *
037 * @author Mark Pollack
038 * @since 1.1.1
039 * @see #setConnectionFactory
040 * @see #setJmsTemplate
041 * @see #createJmsTemplate
042 * @see org.springframework.jms.core.JmsTemplate
043 */
044public abstract class JmsGatewaySupport implements InitializingBean {
045
046        /** Logger available to subclasses. */
047        protected final Log logger = LogFactory.getLog(getClass());
048
049        @Nullable
050        private JmsTemplate jmsTemplate;
051
052
053        /**
054         * Set the JMS connection factory to be used by the gateway.
055         * Will automatically create a JmsTemplate for the given ConnectionFactory.
056         * @see #createJmsTemplate
057         * @see #setConnectionFactory(javax.jms.ConnectionFactory)
058         */
059        public final void setConnectionFactory(ConnectionFactory connectionFactory) {
060                this.jmsTemplate = createJmsTemplate(connectionFactory);
061        }
062
063        /**
064         * Create a JmsTemplate for the given ConnectionFactory.
065         * Only invoked if populating the gateway with a ConnectionFactory reference.
066         * <p>Can be overridden in subclasses to provide a JmsTemplate instance with
067         * a different configuration.
068         * @param connectionFactory the JMS ConnectionFactory to create a JmsTemplate for
069         * @return the new JmsTemplate instance
070         * @see #setConnectionFactory
071         */
072        protected JmsTemplate createJmsTemplate(ConnectionFactory connectionFactory) {
073                return new JmsTemplate(connectionFactory);
074        }
075
076        /**
077         * Return the JMS ConnectionFactory used by the gateway.
078         */
079        @Nullable
080        public final ConnectionFactory getConnectionFactory() {
081                return (this.jmsTemplate != null ? this.jmsTemplate.getConnectionFactory() : null);
082        }
083
084        /**
085         * Set the JmsTemplate for the gateway.
086         * @see #setConnectionFactory(javax.jms.ConnectionFactory)
087         */
088        public final void setJmsTemplate(@Nullable JmsTemplate jmsTemplate) {
089                this.jmsTemplate = jmsTemplate;
090        }
091
092        /**
093         * Return the JmsTemplate for the gateway.
094         */
095        @Nullable
096        public final JmsTemplate getJmsTemplate() {
097                return this.jmsTemplate;
098        }
099
100        @Override
101        public final void afterPropertiesSet() throws IllegalArgumentException, BeanInitializationException {
102                if (this.jmsTemplate == null) {
103                        throw new IllegalArgumentException("'connectionFactory' or 'jmsTemplate' is required");
104                }
105                try {
106                        initGateway();
107                }
108                catch (Exception ex) {
109                        throw new BeanInitializationException("Initialization of JMS gateway failed: " + ex.getMessage(), ex);
110                }
111        }
112
113        /**
114         * Subclasses can override this for custom initialization behavior.
115         * Gets called after population of this instance's bean properties.
116         * @throws java.lang.Exception if initialization fails
117         */
118        protected void initGateway() throws Exception {
119        }
120
121}