KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.springframework.util.StringUtils;
30
31 /**
32  * An adapter for a target JMS {@link javax.jms.ConnectionFactory}, applying the
33  * given user credentials to every standard <code>createConnection()</code> call,
34  * that is, implicitly invoking <code>createConnection(username, password)</code>
35  * on the target. All other methods simply delegate to the corresponding methods
36  * of the target ConnectionFactory.
37  *
38  * <p>Can be used to proxy a target JNDI ConnectionFactory that does not have user
39  * credentials configured. Client code can work with the ConnectionFactory without
40  * passing in username and password on every <code>createConnection()</code> call.
41  *
42  * <p>In the following example, client code can simply transparently work
43  * with the preconfigured "myConnectionFactory", implicitly accessing
44  * "myTargetConnectionFactory" with the specified user credentials.
45  *
46  * <pre class="code">
47  * &lt;bean id="myTargetConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
48  * &lt;property name="jndiName" value="java:comp/env/jms/mycf"/&gt;
49  * &lt;/bean&gt;
50  *
51  * &lt;bean id="myConnectionFactory" class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter"&gt;
52  * &lt;property name="targetConnectionFactory" ref="myTargetConnectionFactory"/&gt;
53  * &lt;property name="username" value="myusername"/&gt;
54  * &lt;property name="password" value="mypassword"/&gt;
55  * &lt;/bean></pre>
56  *
57  * <p>If the "username" is empty, this proxy will simply delegate to the standard
58  * <code>createConnection()</code> method of the target ConnectionFactory.
59  * This can be used to keep a UserCredentialsConnectionFactoryAdapter bean
60  * definition just for the <i>option</i> of implicitly passing in user credentials
61  * if the particular target ConnectionFactory requires it.
62  *
63  * @author Juergen Hoeller
64  * @since 1.2
65  * @see #createConnection
66  * @see #createQueueConnection
67  * @see #createTopicConnection
68  */

69 public class UserCredentialsConnectionFactoryAdapter
70         implements ConnectionFactory JavaDoc, QueueConnectionFactory JavaDoc, TopicConnectionFactory JavaDoc, InitializingBean {
71
72     private ConnectionFactory JavaDoc targetConnectionFactory;
73
74     private String JavaDoc username;
75
76     private String JavaDoc password;
77
78     private final ThreadLocal JavaDoc threadBoundCredentials = new ThreadLocal JavaDoc();
79
80
81     /**
82      * Set the target ConnectionFactory that this ConnectionFactory should delegate to.
83      */

84     public void setTargetConnectionFactory(ConnectionFactory JavaDoc targetConnectionFactory) {
85         Assert.notNull(targetConnectionFactory, "'targetConnectionFactory' must not be null");
86         this.targetConnectionFactory = targetConnectionFactory;
87     }
88
89     /**
90      * Set the username that this adapter should use for retrieving Connections.
91      * Default is no specific user.
92      */

93     public void setUsername(String JavaDoc username) {
94         this.username = username;
95     }
96
97     /**
98      * Set the password that this adapter should use for retrieving Connections.
99      * Default is no specific password.
100      */

101     public void setPassword(String JavaDoc password) {
102         this.password = password;
103     }
104
105     public void afterPropertiesSet() {
106         if (this.targetConnectionFactory == null) {
107             throw new IllegalArgumentException JavaDoc("Property 'targetConnectionFactory' is required");
108         }
109     }
110
111
112     /**
113      * Set user credententials for this proxy and the current thread.
114      * The given username and password will be applied to all subsequent
115      * <code>createConnection()</code> calls on this ConnectionFactory proxy.
116      * <p>This will override any statically specified user credentials,
117      * that is, values of the "username" and "password" bean properties.
118      * @param username the username to apply
119      * @param password the password to apply
120      * @see #removeCredentialsFromCurrentThread
121      */

122     public void setCredentialsForCurrentThread(String JavaDoc username, String JavaDoc password) {
123         this.threadBoundCredentials.set(new String JavaDoc[] {username, password});
124     }
125
126     /**
127      * Remove any user credentials for this proxy from the current thread.
128      * Statically specified user credentials apply again afterwards.
129      * @see #setCredentialsForCurrentThread
130      */

131     public void removeCredentialsFromCurrentThread() {
132         this.threadBoundCredentials.set(null);
133     }
134
135
136     /**
137      * Determine whether there are currently thread-bound credentials,
138      * using them if available, falling back to the statically specified
139      * username and password (i.e. values of the bean properties) else.
140      * @see #doCreateConnection
141      */

142     public final Connection JavaDoc createConnection() throws JMSException JavaDoc {
143         String JavaDoc[] threadCredentials = (String JavaDoc[]) this.threadBoundCredentials.get();
144         if (threadCredentials != null) {
145             return doCreateConnection(threadCredentials[0], threadCredentials[1]);
146         }
147         else {
148             return doCreateConnection(this.username, this.password);
149         }
150     }
151
152     /**
153      * Delegate the call straight to the target ConnectionFactory.
154      */

155     public Connection JavaDoc createConnection(String JavaDoc username, String JavaDoc password) throws JMSException JavaDoc {
156         return doCreateConnection(username, password);
157     }
158
159     /**
160      * This implementation delegates to the <code>createConnection(username, password)</code>
161      * method of the target ConnectionFactory, passing in the specified user credentials.
162      * If the specified username is empty, it will simply delegate to the standard
163      * <code>createConnection()</code> method of the target ConnectionFactory.
164      * @param username the username to use
165      * @param password the password to use
166      * @return the Connection
167      * @see javax.jms.ConnectionFactory#createConnection(String, String)
168      * @see javax.jms.ConnectionFactory#createConnection()
169      */

170     protected Connection JavaDoc doCreateConnection(String JavaDoc username, String JavaDoc password) throws JMSException JavaDoc {
171         Assert.state(this.targetConnectionFactory != null, "'targetConnectionFactory' is required");
172         if (StringUtils.hasLength(username)) {
173             return this.targetConnectionFactory.createConnection(username, password);
174         }
175         else {
176             return this.targetConnectionFactory.createConnection();
177         }
178     }
179
180
181     /**
182      * Determine whether there are currently thread-bound credentials,
183      * using them if available, falling back to the statically specified
184      * username and password (i.e. values of the bean properties) else.
185      * @see #doCreateQueueConnection
186      */

187     public final QueueConnection JavaDoc createQueueConnection() throws JMSException JavaDoc {
188         String JavaDoc[] threadCredentials = (String JavaDoc[]) this.threadBoundCredentials.get();
189         if (threadCredentials != null) {
190             return doCreateQueueConnection(threadCredentials[0], threadCredentials[1]);
191         }
192         else {
193             return doCreateQueueConnection(this.username, this.password);
194         }
195     }
196
197     /**
198      * Delegate the call straight to the target QueueConnectionFactory.
199      */

200     public QueueConnection JavaDoc createQueueConnection(String JavaDoc username, String JavaDoc password) throws JMSException JavaDoc {
201         return doCreateQueueConnection(username, password);
202     }
203
204     /**
205      * This implementation delegates to the <code>createQueueConnection(username, password)</code>
206      * method of the target QueueConnectionFactory, passing in the specified user credentials.
207      * If the specified username is empty, it will simply delegate to the standard
208      * <code>createQueueConnection()</code> method of the target ConnectionFactory.
209      * @param username the username to use
210      * @param password the password to use
211      * @return the Connection
212      * @see javax.jms.QueueConnectionFactory#createQueueConnection(String, String)
213      * @see javax.jms.QueueConnectionFactory#createQueueConnection()
214      */

215     protected QueueConnection JavaDoc doCreateQueueConnection(String JavaDoc username, String JavaDoc password) throws JMSException JavaDoc {
216         Assert.state(this.targetConnectionFactory != null, "'targetConnectionFactory' is required");
217         if (!(this.targetConnectionFactory instanceof QueueConnectionFactory JavaDoc)) {
218             throw new javax.jms.IllegalStateException JavaDoc("'targetConnectionFactory' is not a QueueConnectionFactory");
219         }
220         QueueConnectionFactory JavaDoc queueFactory = (QueueConnectionFactory JavaDoc) this.targetConnectionFactory;
221         if (StringUtils.hasLength(username)) {
222             return queueFactory.createQueueConnection(username, password);
223         }
224         else {
225             return queueFactory.createQueueConnection();
226         }
227     }
228
229
230     /**
231      * Determine whether there are currently thread-bound credentials,
232      * using them if available, falling back to the statically specified
233      * username and password (i.e. values of the bean properties) else.
234      * @see #doCreateTopicConnection
235      */

236     public final TopicConnection JavaDoc createTopicConnection() throws JMSException JavaDoc {
237         String JavaDoc[] threadCredentials = (String JavaDoc[]) this.threadBoundCredentials.get();
238         if (threadCredentials != null) {
239             return doCreateTopicConnection(threadCredentials[0], threadCredentials[1]);
240         }
241         else {
242             return doCreateTopicConnection(this.username, this.password);
243         }
244     }
245
246     /**
247      * Delegate the call straight to the target TopicConnectionFactory.
248      */

249     public TopicConnection JavaDoc createTopicConnection(String JavaDoc username, String JavaDoc password) throws JMSException JavaDoc {
250         return doCreateTopicConnection(username, password);
251     }
252
253     /**
254      * This implementation delegates to the <code>createTopicConnection(username, password)</code>
255      * method of the target TopicConnectionFactory, passing in the specified user credentials.
256      * If the specified username is empty, it will simply delegate to the standard
257      * <code>createTopicConnection()</code> method of the target ConnectionFactory.
258      * @param username the username to use
259      * @param password the password to use
260      * @return the Connection
261      * @see javax.jms.TopicConnectionFactory#createTopicConnection(String, String)
262      * @see javax.jms.TopicConnectionFactory#createTopicConnection()
263      */

264     protected TopicConnection JavaDoc doCreateTopicConnection(String JavaDoc username, String JavaDoc password) throws JMSException JavaDoc {
265         Assert.state(this.targetConnectionFactory != null, "'targetConnectionFactory' is required");
266         if (!(this.targetConnectionFactory instanceof TopicConnectionFactory JavaDoc)) {
267             throw new javax.jms.IllegalStateException JavaDoc("'targetConnectionFactory' is not a TopicConnectionFactory");
268         }
269         TopicConnectionFactory JavaDoc queueFactory = (TopicConnectionFactory JavaDoc) this.targetConnectionFactory;
270         if (StringUtils.hasLength(username)) {
271             return queueFactory.createTopicConnection(username, password);
272         }
273         else {
274             return queueFactory.createTopicConnection();
275         }
276     }
277
278 }
279
Popular Tags