Spring Framework

org.springframework.jms.connection
Class UserCredentialsConnectionFactoryAdapter

java.lang.Object
  extended by org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter
All Implemented Interfaces:
javax.jms.ConnectionFactory, javax.jms.QueueConnectionFactory, javax.jms.TopicConnectionFactory, InitializingBean

public class UserCredentialsConnectionFactoryAdapter
extends java.lang.Object
implements javax.jms.ConnectionFactory, javax.jms.QueueConnectionFactory, javax.jms.TopicConnectionFactory, InitializingBean

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

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

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

 <bean id="myTargetConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
   <property name="jndiName" value="java:comp/env/jms/mycf"/>
 </bean>

 <bean id="myConnectionFactory" class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
   <property name="targetConnectionFactory" ref="myTargetConnectionFactory"/>
   <property name="username" value="myusername"/>
   <property name="password" value="mypassword"/>
 </bean>

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

Since:
1.2
Author:
Juergen Hoeller
See Also:
createConnection(), createQueueConnection(), createTopicConnection()

Constructor Summary
UserCredentialsConnectionFactoryAdapter()
           
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
 javax.jms.Connection createConnection()
          Determine whether there are currently thread-bound credentials, using them if available, falling back to the statically specified username and password (i.e.
 javax.jms.Connection createConnection(java.lang.String username, java.lang.String password)
          Delegate the call straight to the target ConnectionFactory.
 javax.jms.QueueConnection createQueueConnection()
          Determine whether there are currently thread-bound credentials, using them if available, falling back to the statically specified username and password (i.e.
 javax.jms.QueueConnection createQueueConnection(java.lang.String username, java.lang.String password)
          Delegate the call straight to the target QueueConnectionFactory.
 javax.jms.TopicConnection createTopicConnection()
          Determine whether there are currently thread-bound credentials, using them if available, falling back to the statically specified username and password (i.e.
 javax.jms.TopicConnection createTopicConnection(java.lang.String username, java.lang.String password)
          Delegate the call straight to the target TopicConnectionFactory.
protected  javax.jms.Connection doCreateConnection(java.lang.String username, java.lang.String password)
          This implementation delegates to the createConnection(username, password) method of the target ConnectionFactory, passing in the specified user credentials.
protected  javax.jms.QueueConnection doCreateQueueConnection(java.lang.String username, java.lang.String password)
          This implementation delegates to the createQueueConnection(username, password) method of the target QueueConnectionFactory, passing in the specified user credentials.
protected  javax.jms.TopicConnection doCreateTopicConnection(java.lang.String username, java.lang.String password)
          This implementation delegates to the createTopicConnection(username, password) method of the target TopicConnectionFactory, passing in the specified user credentials.
 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 setPassword(java.lang.String password)
          Set the password that this adapter should use for retrieving Connections.
 void setTargetConnectionFactory(javax.jms.ConnectionFactory targetConnectionFactory)
          Set the target ConnectionFactory that this ConnectionFactory 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

UserCredentialsConnectionFactoryAdapter

public UserCredentialsConnectionFactoryAdapter()
Method Detail

setTargetConnectionFactory

public void setTargetConnectionFactory(javax.jms.ConnectionFactory targetConnectionFactory)
Set the target ConnectionFactory that this ConnectionFactory should delegate to.


setUsername

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


setPassword

public void setPassword(java.lang.String password)
Set the password that this adapter should use for retrieving Connections. Default is 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 createConnection() calls on this ConnectionFactory 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)

createConnection

public final javax.jms.Connection createConnection()
                                            throws javax.jms.JMSException
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:
createConnection in interface javax.jms.ConnectionFactory
Throws:
javax.jms.JMSException
See Also:
doCreateConnection(java.lang.String, java.lang.String)

createConnection

public javax.jms.Connection createConnection(java.lang.String username,
                                             java.lang.String password)
                                      throws javax.jms.JMSException
Delegate the call straight to the target ConnectionFactory.

Specified by:
createConnection in interface javax.jms.ConnectionFactory
Throws:
javax.jms.JMSException

doCreateConnection

protected javax.jms.Connection doCreateConnection(java.lang.String username,
                                                  java.lang.String password)
                                           throws javax.jms.JMSException
This implementation delegates to the createConnection(username, password) method of the target ConnectionFactory, passing in the specified user credentials. If the specified username is empty, it will simply delegate to the standard createConnection() method of the target ConnectionFactory.

Parameters:
username - the username to use
password - the password to use
Returns:
the Connection
Throws:
javax.jms.JMSException
See Also:
ConnectionFactory.createConnection(String, String), ConnectionFactory.createConnection()

createQueueConnection

public final javax.jms.QueueConnection createQueueConnection()
                                                      throws javax.jms.JMSException
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:
createQueueConnection in interface javax.jms.QueueConnectionFactory
Throws:
javax.jms.JMSException
See Also:
doCreateQueueConnection(java.lang.String, java.lang.String)

createQueueConnection

public javax.jms.QueueConnection createQueueConnection(java.lang.String username,
                                                       java.lang.String password)
                                                throws javax.jms.JMSException
Delegate the call straight to the target QueueConnectionFactory.

Specified by:
createQueueConnection in interface javax.jms.QueueConnectionFactory
Throws:
javax.jms.JMSException

doCreateQueueConnection

protected javax.jms.QueueConnection doCreateQueueConnection(java.lang.String username,
                                                            java.lang.String password)
                                                     throws javax.jms.JMSException
This implementation delegates to the createQueueConnection(username, password) method of the target QueueConnectionFactory, passing in the specified user credentials. If the specified username is empty, it will simply delegate to the standard createQueueConnection() method of the target ConnectionFactory.

Parameters:
username - the username to use
password - the password to use
Returns:
the Connection
Throws:
javax.jms.JMSException
See Also:
QueueConnectionFactory.createQueueConnection(String, String), QueueConnectionFactory.createQueueConnection()

createTopicConnection

public final javax.jms.TopicConnection createTopicConnection()
                                                      throws javax.jms.JMSException
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:
createTopicConnection in interface javax.jms.TopicConnectionFactory
Throws:
javax.jms.JMSException
See Also:
doCreateTopicConnection(java.lang.String, java.lang.String)

createTopicConnection

public javax.jms.TopicConnection createTopicConnection(java.lang.String username,
                                                       java.lang.String password)
                                                throws javax.jms.JMSException
Delegate the call straight to the target TopicConnectionFactory.

Specified by:
createTopicConnection in interface javax.jms.TopicConnectionFactory
Throws:
javax.jms.JMSException

doCreateTopicConnection

protected javax.jms.TopicConnection doCreateTopicConnection(java.lang.String username,
                                                            java.lang.String password)
                                                     throws javax.jms.JMSException
This implementation delegates to the createTopicConnection(username, password) method of the target TopicConnectionFactory, passing in the specified user credentials. If the specified username is empty, it will simply delegate to the standard createTopicConnection() method of the target ConnectionFactory.

Parameters:
username - the username to use
password - the password to use
Returns:
the Connection
Throws:
javax.jms.JMSException
See Also:
TopicConnectionFactory.createTopicConnection(String, String), TopicConnectionFactory.createTopicConnection()

Spring Framework