UserCredentialsDataSourceAdapter.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 5k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2002-2004 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. package org.springframework.jdbc.datasource;
  17. import java.sql.Connection;
  18. import java.sql.SQLException;
  19. /**
  20.  * An adapter for a target DataSource, applying the given user credentials to
  21.  * every standard <code>getConnection()</code> call, that is, implicitly
  22.  * invoking <code>getConnection(username, password)</code> on the target.
  23.  * All other methods simply delegate to the corresponding methods of the
  24.  * target DataSource.
  25.  *
  26.  * <p>Can be used to proxy a target JNDI DataSource that does not have user
  27.  * credentials configured. Client code can work with the DataSource without
  28.  * passing in username and password on every <code>getConnection()</code> call.
  29.  *
  30.  * <p>In the following example, client code can simply transparently work with
  31.  * the preconfigured "myDataSource", implicitly accessing "myTargetDataSource"
  32.  * with the specified user credentials.
  33.  *
  34.  * <pre>
  35.  * &lt;bean id="myTargetDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  36.  *   &lt;property name="jndiName">&lt;value>java:comp/env/jdbc/myds&lt;/value>&lt;/property>
  37.  * &lt;/bean>
  38.  *
  39.  * &lt;bean id="myDataSource" class="org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter">
  40.  *   &lt;property name="targetDataSource">&lt;ref bean="myTargetDataSource"/>&lt;/property>
  41.  *   &lt;property name="username">&lt;value>myusername&lt;/value>&lt;/property>
  42.  *   &lt;property name="password">&lt;value>mypassword&lt;/value>&lt;/property>
  43.  * &lt;/bean></pre>
  44.  *
  45.  * <p>If the "username" is empty, this proxy will simply delegate to the
  46.  * standard <code>getConnection()</code> method of the target DataSource.
  47.  * This can be used to keep a UserCredentialsDataSourceAdapter bean definition
  48.  * just for the <i>option</i> of implicitly passing in user credentials if
  49.  * a particular target DataSource requires it.
  50.  *
  51.  * @author Juergen Hoeller
  52.  * @since 28.05.2004
  53.  * @see #getConnection
  54.  */
  55. public class UserCredentialsDataSourceAdapter extends DelegatingDataSource {
  56. private String username = "";
  57. private String password = "";
  58. private final ThreadLocal threadBoundCredentials = new ThreadLocal();
  59. /**
  60.  * Set the username that this adapter should use for retrieving Connections.
  61.  * Default is the empty string, i.e. no specific user.
  62.  */
  63. public void setUsername(String username) {
  64. this.username = username;
  65. }
  66. /**
  67.  * Set the password that this adapter should use for retrieving Connections.
  68.  * Default is the empty string, i.e. no specific password.
  69.  */
  70. public void setPassword(String password) {
  71. this.password = password;
  72. }
  73. /**
  74.  * Set user credententials for this proxy and the current thread.
  75.  * The given username and password will be applied to all subsequent
  76.  * <code>getConnection()</code> calls on this DataSource proxy.
  77.  * <p>This will override any statically specified user credentials,
  78.  * that is, values of the "username" and "password" bean properties.
  79.  * @param username the username to apply
  80.  * @param password the password to apply
  81.  * @see #removeCredentialsFromCurrentThread
  82.  */
  83. public void setCredentialsForCurrentThread(String username, String password) {
  84. this.threadBoundCredentials.set(new String[] {username, password});
  85. }
  86. /**
  87.  * Remove any user credentials for this proxy from the current thread.
  88.  * Statically specified user credentials apply again afterwards.
  89.  * @see #setCredentialsForCurrentThread
  90.  */
  91. public void removeCredentialsFromCurrentThread() {
  92. this.threadBoundCredentials.set(null);
  93. }
  94. /**
  95.  * Determine whether there are currently thread-bound credentials,
  96.  * using them if available, falling back to the statically specified
  97.  * username and password (i.e. values of the bean properties) else.
  98.  * @see #doGetConnection
  99.  */
  100. public final Connection getConnection() throws SQLException {
  101. String[] threadCredentials = (String[]) this.threadBoundCredentials.get();
  102. if (threadCredentials != null) {
  103. return doGetConnection(threadCredentials[0], threadCredentials[1]);
  104. }
  105. else {
  106. return doGetConnection(this.username, this.password);
  107. }
  108. }
  109. /**
  110.  * This implementation delegates to the <code>getConnection(username, password)</code>
  111.  * method of the target DataSource, passing in the specified user credentials.
  112.  * If the specified username is empty, it will simply delegate to the standard
  113.  * <code>getConnection()</code> method of the target DataSource.
  114.  * @param username the username to use
  115.  * @param password the password to use
  116.  * @return the Connection
  117.  * @see javax.sql.DataSource#getConnection(String, String)
  118.  * @see javax.sql.DataSource#getConnection()
  119.  */
  120. protected Connection doGetConnection(String username, String password) throws SQLException {
  121. if (!"".equals(username)) {
  122. return getTargetDataSource().getConnection(username, password);
  123. }
  124. else {
  125. return getTargetDataSource().getConnection();
  126. }
  127. }
  128. }