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