KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > datasource > UserCredentialsDataSourceAdapter


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.jdbc.datasource;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21
22 import org.springframework.util.Assert;
23 import org.springframework.util.StringUtils;
24
25 /**
26  * An adapter for a target JDBC {@link javax.sql.DataSource}, applying the specified
27  * user credentials to every standard <code>getConnection()</code> call, implicitly
28  * invoking <code>getConnection(username, password)</code> on the target.
29  * All other methods simply delegate to the corresponding methods of the
30  * target DataSource.
31  *
32  * <p>Can be used to proxy a target JNDI DataSource that does not have user
33  * credentials configured. Client code can work with this DataSource as usual,
34  * using the standard <code>getConnection()</code> call.
35  *
36  * <p>In the following example, client code can simply transparently work with
37  * the preconfigured "myDataSource", implicitly accessing "myTargetDataSource"
38  * with the specified user credentials.
39  *
40  * <pre class="code">
41  * &lt;bean id="myTargetDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
42  * &lt;property name="jndiName" value="java:comp/env/jdbc/myds"/&gt;
43  * &lt;/bean&gt;
44  *
45  * &lt;bean id="myDataSource" class="org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter"&gt;
46  * &lt;property name="targetDataSource" ref="myTargetDataSource"/&gt;
47  * &lt;property name="username" value="myusername"/&gt;
48  * &lt;property name="password" value="mypassword"/&gt;
49  * &lt;/bean></pre>
50  *
51  * <p>If the "username" is empty, this proxy will simply delegate to the
52  * standard <code>getConnection()</code> method of the target DataSource.
53  * This can be used to keep a UserCredentialsDataSourceAdapter bean definition
54  * just for the <i>option</i> of implicitly passing in user credentials if
55  * the particular target DataSource requires it.
56  *
57  * @author Juergen Hoeller
58  * @since 1.0.2
59  * @see #getConnection
60  */

61 public class UserCredentialsDataSourceAdapter extends DelegatingDataSource {
62
63     private String JavaDoc username;
64
65     private String JavaDoc password;
66
67     private final ThreadLocal JavaDoc threadBoundCredentials = new ThreadLocal JavaDoc();
68
69
70     /**
71      * Set the default username that this adapter should use for retrieving Connections.
72      * <p>Default is no specific user. Note that an explicitly specified username
73      * will always override any username/password specified at the DataSource level.
74      * @see #setPassword
75      * @see #setCredentialsForCurrentThread(String, String)
76      * @see #getConnection(String, String)
77      */

78     public void setUsername(String JavaDoc username) {
79         this.username = username;
80     }
81
82     /**
83      * Set the default user's password that this adapter should use for retrieving Connections.
84      * <p>Default is no specific password. Note that an explicitly specified username
85      * will always override any username/password specified at the DataSource level.
86      * @see #setUsername
87      * @see #setCredentialsForCurrentThread(String, String)
88      * @see #getConnection(String, String)
89      */

90     public void setPassword(String JavaDoc password) {
91         this.password = password;
92     }
93
94
95     /**
96      * Set user credententials for this proxy and the current thread.
97      * The given username and password will be applied to all subsequent
98      * <code>getConnection()</code> calls on this DataSource proxy.
99      * <p>This will override any statically specified user credentials,
100      * that is, values of the "username" and "password" bean properties.
101      * @param username the username to apply
102      * @param password the password to apply
103      * @see #removeCredentialsFromCurrentThread
104      */

105     public void setCredentialsForCurrentThread(String JavaDoc username, String JavaDoc password) {
106         this.threadBoundCredentials.set(new String JavaDoc[] {username, password});
107     }
108
109     /**
110      * Remove any user credentials for this proxy from the current thread.
111      * Statically specified user credentials apply again afterwards.
112      * @see #setCredentialsForCurrentThread
113      */

114     public void removeCredentialsFromCurrentThread() {
115         this.threadBoundCredentials.set(null);
116     }
117
118
119     /**
120      * Determine whether there are currently thread-bound credentials,
121      * using them if available, falling back to the statically specified
122      * username and password (i.e. values of the bean properties) else.
123      * <p>Delegates to {@link #doGetConnection(String, String)} with the
124      * determined credentials as parameters.
125      */

126     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
127         String JavaDoc[] threadCredentials = (String JavaDoc[]) this.threadBoundCredentials.get();
128         if (threadCredentials != null) {
129             return doGetConnection(threadCredentials[0], threadCredentials[1]);
130         }
131         else {
132             return doGetConnection(this.username, this.password);
133         }
134     }
135
136     /**
137      * Simply delegates to {@link #doGetConnection(String, String)},
138      * keeping the given user credentials as-is.
139      */

140     public Connection JavaDoc getConnection(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc {
141         return doGetConnection(username, password);
142     }
143
144     /**
145      * This implementation delegates to the <code>getConnection(username, password)</code>
146      * method of the target DataSource, passing in the specified user credentials.
147      * If the specified username is empty, it will simply delegate to the standard
148      * <code>getConnection()</code> method of the target DataSource.
149      * @param username the username to use
150      * @param password the password to use
151      * @return the Connection
152      * @see javax.sql.DataSource#getConnection(String, String)
153      * @see javax.sql.DataSource#getConnection()
154      */

155     protected Connection JavaDoc doGetConnection(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc {
156         Assert.state(getTargetDataSource() != null, "'targetDataSource' is required");
157         if (StringUtils.hasLength(username)) {
158             return getTargetDataSource().getConnection(username, password);
159         }
160         else {
161             return getTargetDataSource().getConnection();
162         }
163     }
164
165 }
166
Popular Tags