KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jms > connection > DelegatingConnectionFactory


1 /*
2  * Copyright 2002-2007 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.connection;
18
19 import javax.jms.Connection JavaDoc;
20 import javax.jms.ConnectionFactory JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.QueueConnection JavaDoc;
23 import javax.jms.QueueConnectionFactory JavaDoc;
24 import javax.jms.TopicConnection JavaDoc;
25 import javax.jms.TopicConnectionFactory JavaDoc;
26
27 import org.springframework.beans.factory.InitializingBean;
28 import org.springframework.util.Assert;
29
30 /**
31  * {@link javax.jms.ConnectionFactory} implementation that delegates all calls
32  * to a given target {@link javax.jms.ConnectionFactory}.
33  *
34  * <p>This class allows for being subclassed, with subclasses overriding only
35  * those methods (such as {@link #createConnection()}) that should not simply
36  * delegate to the target ConnectionFactory.
37  *
38  * <p>Can also be defined as-is, wrapping a specific target ConnectionFactory,
39  * using the "shouldStopConnections" flag to indicate whether Connections
40  * obtained from the target factory are supposed to be stopped before closed.
41  * The latter may be necessary for some connection pools that simply return
42  * released connections to the pool, not stopping them while they sit in the pool.
43  *
44  * @author Juergen Hoeller
45  * @since 2.0.2
46  * @see #createConnection()
47  * @see #setShouldStopConnections
48  * @see ConnectionFactoryUtils#releaseConnection
49  */

50 public class DelegatingConnectionFactory
51         implements SmartConnectionFactory, QueueConnectionFactory JavaDoc, TopicConnectionFactory JavaDoc, InitializingBean {
52
53     private ConnectionFactory JavaDoc targetConnectionFactory;
54
55     private boolean shouldStopConnections = false;
56
57
58     /**
59      * Set the target ConnectionFactory that this ConnectionFactory should delegate to.
60      */

61     public void setTargetConnectionFactory(ConnectionFactory JavaDoc targetConnectionFactory) {
62         Assert.notNull(targetConnectionFactory, "'targetConnectionFactory' must not be null");
63         this.targetConnectionFactory = targetConnectionFactory;
64     }
65
66     /**
67      * Return the target ConnectionFactory that this ConnectionFactory delegates to.
68      */

69     public ConnectionFactory JavaDoc getTargetConnectionFactory() {
70         return this.targetConnectionFactory;
71     }
72
73     /**
74      * Indicate whether Connections obtained from the target factory are supposed
75      * to be stopped before closed ("true") or simply closed ("false").
76      * The latter may be necessary for some connection pools that simply return
77      * released connections to the pool, not stopping them while they sit in the pool.
78      * <p>Default is "false", simply closing Connections.
79      * @see ConnectionFactoryUtils#releaseConnection
80      */

81     public void setShouldStopConnections(boolean shouldStopConnections) {
82         this.shouldStopConnections = shouldStopConnections;
83     }
84
85     public void afterPropertiesSet() {
86         if (getTargetConnectionFactory() == null) {
87             throw new IllegalArgumentException JavaDoc("'targetConnectionFactory' is required");
88         }
89     }
90
91
92     public Connection JavaDoc createConnection() throws JMSException JavaDoc {
93         return getTargetConnectionFactory().createConnection();
94     }
95
96     public Connection JavaDoc createConnection(String JavaDoc username, String JavaDoc password) throws JMSException JavaDoc {
97         return getTargetConnectionFactory().createConnection(username, password);
98     }
99
100     public QueueConnection JavaDoc createQueueConnection() throws JMSException JavaDoc {
101         ConnectionFactory JavaDoc cf = getTargetConnectionFactory();
102         if (!(cf instanceof QueueConnectionFactory JavaDoc)) {
103             throw new javax.jms.IllegalStateException JavaDoc("'targetConnectionFactory' is not a QueueConnectionFactory");
104         }
105         return ((QueueConnectionFactory JavaDoc) cf).createQueueConnection();
106     }
107
108     public QueueConnection JavaDoc createQueueConnection(String JavaDoc username, String JavaDoc password) throws JMSException JavaDoc {
109         ConnectionFactory JavaDoc cf = getTargetConnectionFactory();
110         if (!(cf instanceof QueueConnectionFactory JavaDoc)) {
111             throw new javax.jms.IllegalStateException JavaDoc("'targetConnectionFactory' is not a QueueConnectionFactory");
112         }
113         return ((QueueConnectionFactory JavaDoc) cf).createQueueConnection(username, password);
114     }
115
116     public TopicConnection JavaDoc createTopicConnection() throws JMSException JavaDoc {
117         ConnectionFactory JavaDoc cf = getTargetConnectionFactory();
118         if (!(cf instanceof TopicConnectionFactory JavaDoc)) {
119             throw new javax.jms.IllegalStateException JavaDoc("'targetConnectionFactory' is not a TopicConnectionFactory");
120         }
121         return ((TopicConnectionFactory JavaDoc) cf).createTopicConnection();
122     }
123
124     public TopicConnection JavaDoc createTopicConnection(String JavaDoc username, String JavaDoc password) throws JMSException JavaDoc {
125         ConnectionFactory JavaDoc cf = getTargetConnectionFactory();
126         if (!(cf instanceof TopicConnectionFactory JavaDoc)) {
127             throw new javax.jms.IllegalStateException JavaDoc("'targetConnectionFactory' is not a TopicConnectionFactory");
128         }
129         return ((TopicConnectionFactory JavaDoc) cf).createTopicConnection(username, password);
130     }
131
132     public boolean shouldStop(Connection JavaDoc con) {
133         return this.shouldStopConnections;
134     }
135
136 }
137
Popular Tags