KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jms > core > support > JmsGatewaySupport


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jms.core.support;
18
19 import javax.jms.ConnectionFactory JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import org.springframework.beans.factory.BeanInitializationException;
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.jms.core.JmsTemplate;
27
28 /**
29  * Convenient super class for application classes that need JMS access.
30  *
31  * <p>Requires a ConnectionFactory or a JmsTemplate instance to be set.
32  * It will create its own JmsTemplate if a ConnectionFactory is passed in.
33  * A custom JmsTemplate instance can be created for a given ConnectionFactory
34  * through overriding the <code>createJmsTemplate</code> method.
35  *
36  * @author Mark Pollack
37  * @since 1.1.1
38  * @see #setConnectionFactory
39  * @see #setJmsTemplate
40  * @see #createJmsTemplate
41  * @see org.springframework.jms.core.JmsTemplate
42  */

43 public abstract class JmsGatewaySupport implements InitializingBean {
44
45     protected final Log logger = LogFactory.getLog(getClass());
46     
47     private JmsTemplate jmsTemplate;
48
49
50     /**
51      * Set the JMS connection factory to be used by the gateway.
52      * Will automatically create a JmsTemplate for the given ConnectionFactory.
53      * @see #createJmsTemplate
54      * @see #setConnectionFactory(javax.jms.ConnectionFactory)
55      * @param connectionFactory
56      */

57     public final void setConnectionFactory(ConnectionFactory JavaDoc connectionFactory) {
58         this.jmsTemplate = createJmsTemplate(connectionFactory);
59     }
60     
61     /**
62      * Create a JmsTemplate for the given ConnectionFactory.
63      * Only invoked if populating the gateway with a ConnectionFactory reference.
64      * <p>Can be overridden in subclasses to provide a JmsTemplate instance with
65      * a different configuration or the JMS 1.0.2 version, JmsTemplate102.
66      * @param connectionFactory the JMS ConnectionFactory to create a JmsTemplate for
67      * @return the new JmsTemplate instance
68      * @see #setConnectionFactory
69      * @see org.springframework.jms.core.JmsTemplate102
70      */

71     protected JmsTemplate createJmsTemplate(ConnectionFactory JavaDoc connectionFactory) {
72         return new JmsTemplate(connectionFactory);
73     }
74     
75     /**
76      * Return the JMS ConnectionFactory used by the gateway.
77      */

78     public final ConnectionFactory JavaDoc getConnectionFactory() {
79         return (this.jmsTemplate != null ? this.jmsTemplate.getConnectionFactory() : null);
80     }
81     
82     /**
83      * Set the JmsTemplate for the gateway.
84      * @param jmsTemplate
85      * @see #setConnectionFactory(javax.jms.ConnectionFactory)
86      */

87     public final void setJmsTemplate(JmsTemplate jmsTemplate) {
88         this.jmsTemplate = jmsTemplate;
89     }
90     
91     /**
92      * Return the JmsTemplate for the gateway.
93      */

94     public final JmsTemplate getJmsTemplate() {
95         return jmsTemplate;
96     }
97     
98     public final void afterPropertiesSet() throws IllegalArgumentException JavaDoc, BeanInitializationException {
99         if (this.jmsTemplate == null) {
100             throw new IllegalArgumentException JavaDoc("connectionFactory or jmsTemplate is required");
101         }
102         try {
103             initGateway();
104         }
105         catch (Exception JavaDoc ex) {
106             throw new BeanInitializationException("Initialization of JMS gateway failed: " + ex.getMessage(), ex);
107         }
108     }
109     
110     /**
111      * Subclasses can override this for custom initialization behavior.
112      * Gets called after population of this instance's bean properties.
113      * @throws java.lang.Exception if initialization fails
114      */

115     protected void initGateway() throws Exception JavaDoc {
116     }
117
118 }
119
Popular Tags