org.springframework.jdbc.datasource
Class UserCredentialsDataSourceAdapter

java.lang.Object
  extended byorg.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter
All Implemented Interfaces:
javax.sql.DataSource, InitializingBean

public class UserCredentialsDataSourceAdapter
extends java.lang.Object
implements javax.sql.DataSource, InitializingBean

An adapter for a target DataSource, applying the given user credentials to every standard getConnection() call, that is, implicitly invoking getConnection(username, password) on the target. All other methods simply delegate to the corresponding methods of the target DataSource.

Can be used to proxy a target JNDI DataSource that does not have user credentials configured. Client code can work with the DataSource without passing in username and password on every getConnection() call.

In the following example, client code can simply transparently work with the preconfigured "myDataSource", implicitly accessing "myTargetDataSource" with the specified user credentials.

 <bean id="myTargetDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
   <property name="jndiName"><value>java:comp/env/jdbc/myds</value></property>
 </bean>

 <bean id="myDataSource" class="org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter">
   <property name="targetDataSource"><ref bean="myTargetDataSource"/></property>
   <property name="username"><value>myusername</value></property>
   <property name="password"><value>mypassword</value></property>
 </bean>

If the "username" is empty, this proxy will simply delegate to the standard getConnection() method of the target DataSource. This can be used to keep a UserCredentialsDataSourceAdapter bean definition just for the option of implicitly passing in user credentials if a particular target DataSource requires it.

Since:
28.05.2004
Author:
Juergen Hoeller
See Also:
getConnection()

Constructor Summary
UserCredentialsDataSourceAdapter()
           
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
protected  java.sql.Connection doGetConnection(java.lang.String username, java.lang.String password)
          This implementation delegates to the getConnection(username, password) method of the target DataSource, passing in the specified user credentials.
 java.sql.Connection getConnection()
          Determine whether there are currently thread-bound credentials, using them if available, falling back to the statically specified username and password (i.e. values of the bean properties) else.
 java.sql.Connection getConnection(java.lang.String username, java.lang.String password)
           
 int getLoginTimeout()
           
 java.io.PrintWriter getLogWriter()
           
 void removeCredentialsFromCurrentThread()
          Remove any user credentials for this proxy from the current thread.
 void setCredentialsForCurrentThread(java.lang.String username, java.lang.String password)
          Set user credententials for this proxy and the current thread.
 void setLoginTimeout(int seconds)
           
 void setLogWriter(java.io.PrintWriter out)
           
 void setPassword(java.lang.String password)
          Set the password that this adapter should use for retrieving Connections.
 void setTargetDataSource(javax.sql.DataSource targetDataSource)
          Set the target DataSource that this adapter should delegate to.
 void setUsername(java.lang.String username)
          Set the username that this adapter should use for retrieving Connections.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UserCredentialsDataSourceAdapter

public UserCredentialsDataSourceAdapter()
Method Detail

setTargetDataSource

public void setTargetDataSource(javax.sql.DataSource targetDataSource)
Set the target DataSource that this adapter should delegate to.


setUsername

public void setUsername(java.lang.String username)
Set the username that this adapter should use for retrieving Connections. Default is the empty string, i.e. no specific user.


setPassword

public void setPassword(java.lang.String password)
Set the password that this adapter should use for retrieving Connections. Default is the empty string, i.e. no specific password.


afterPropertiesSet

public void afterPropertiesSet()
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

Specified by:
afterPropertiesSet in interface InitializingBean

setCredentialsForCurrentThread

public void setCredentialsForCurrentThread(java.lang.String username,
                                           java.lang.String password)
Set user credententials for this proxy and the current thread. The given username and password will be applied to all subsequent getConnection() calls on this DataSource proxy.

This will override any statically specified user credentials, that is, values of the "username" and "password" bean properties.

Parameters:
username - the username to apply
password - the password to apply
See Also:
removeCredentialsFromCurrentThread()

removeCredentialsFromCurrentThread

public void removeCredentialsFromCurrentThread()
Remove any user credentials for this proxy from the current thread. Statically specified user credentials apply again afterwards.

See Also:
setCredentialsForCurrentThread(java.lang.String, java.lang.String)

getConnection

public final java.sql.Connection getConnection()
                                        throws java.sql.SQLException
Determine whether there are currently thread-bound credentials, using them if available, falling back to the statically specified username and password (i.e. values of the bean properties) else.

Specified by:
getConnection in interface javax.sql.DataSource
Throws:
java.sql.SQLException
See Also:
doGetConnection(java.lang.String, java.lang.String)

doGetConnection

protected java.sql.Connection doGetConnection(java.lang.String username,
                                              java.lang.String password)
                                       throws java.sql.SQLException
This implementation delegates to the getConnection(username, password) method of the target DataSource, passing in the specified user credentials. If the specified username is empty, it will simply delegate to the standard getConnection() method of the target DataSource.

Parameters:
username - the username to use
password - the password to use
Returns:
the Connection
Throws:
java.sql.SQLException
See Also:
DataSource.getConnection(String, String), DataSource.getConnection()

getConnection

public java.sql.Connection getConnection(java.lang.String username,
                                         java.lang.String password)
                                  throws java.sql.SQLException
Specified by:
getConnection in interface javax.sql.DataSource
Throws:
java.sql.SQLException

getLoginTimeout

public int getLoginTimeout()
                    throws java.sql.SQLException
Specified by:
getLoginTimeout in interface javax.sql.DataSource
Throws:
java.sql.SQLException

setLoginTimeout

public void setLoginTimeout(int seconds)
                     throws java.sql.SQLException
Specified by:
setLoginTimeout in interface javax.sql.DataSource
Throws:
java.sql.SQLException

getLogWriter

public java.io.PrintWriter getLogWriter()
                                 throws java.sql.SQLException
Specified by:
getLogWriter in interface javax.sql.DataSource
Throws:
java.sql.SQLException

setLogWriter

public void setLogWriter(java.io.PrintWriter out)
                  throws java.sql.SQLException
Specified by:
setLogWriter in interface javax.sql.DataSource
Throws:
java.sql.SQLException


Copyright (C) 2003-2004 The Spring Framework Project.