public class UserCredentialsConnectionFactoryAdapter extends java.lang.Object implements InitializingBean
javax.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.
createConnection()
,
createQueueConnection()
,
createTopicConnection()
Modifier and Type | Class and Description |
---|---|
private static class |
UserCredentialsConnectionFactoryAdapter.JmsUserCredentials
Inner class used as ThreadLocal value.
|
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
password |
private ConnectionFactory |
targetConnectionFactory |
private java.lang.ThreadLocal<UserCredentialsConnectionFactoryAdapter.JmsUserCredentials> |
threadBoundCredentials |
private java.lang.String |
username |
Constructor and Description |
---|
UserCredentialsConnectionFactoryAdapter() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
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.
|
Connection |
createConnection(java.lang.String username,
java.lang.String password)
Delegate the call straight to the target ConnectionFactory.
|
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.
|
QueueConnection |
createQueueConnection(java.lang.String username,
java.lang.String password)
Delegate the call straight to the target QueueConnectionFactory.
|
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.
|
TopicConnection |
createTopicConnection(java.lang.String username,
java.lang.String password)
Delegate the call straight to the target TopicConnectionFactory.
|
protected 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 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 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(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.
|
private ConnectionFactory targetConnectionFactory
private java.lang.String username
private java.lang.String password
private final java.lang.ThreadLocal<UserCredentialsConnectionFactoryAdapter.JmsUserCredentials> threadBoundCredentials
public UserCredentialsConnectionFactoryAdapter()
public void setTargetConnectionFactory(ConnectionFactory targetConnectionFactory)
public void setUsername(java.lang.String username)
public void setPassword(java.lang.String password)
public void afterPropertiesSet()
InitializingBean
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.
afterPropertiesSet
in interface InitializingBean
public void setCredentialsForCurrentThread(java.lang.String username, java.lang.String password)
createConnection()
calls on this ConnectionFactory proxy.
This will override any statically specified user credentials, that is, values of the "username" and "password" bean properties.
username
- the username to applypassword
- the password to applyremoveCredentialsFromCurrentThread()
public void removeCredentialsFromCurrentThread()
public final Connection createConnection() throws JMSException
JMSException
doCreateConnection(java.lang.String, java.lang.String)
public Connection createConnection(java.lang.String username, java.lang.String password) throws JMSException
JMSException
protected Connection doCreateConnection(java.lang.String username, java.lang.String password) throws JMSException
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.username
- the username to usepassword
- the password to useJMSException
javax.jms.ConnectionFactory#createConnection(String, String)
,
javax.jms.ConnectionFactory#createConnection()
public final QueueConnection createQueueConnection() throws JMSException
JMSException
doCreateQueueConnection(java.lang.String, java.lang.String)
public QueueConnection createQueueConnection(java.lang.String username, java.lang.String password) throws JMSException
JMSException
protected QueueConnection doCreateQueueConnection(java.lang.String username, java.lang.String password) throws JMSException
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.username
- the username to usepassword
- the password to useJMSException
javax.jms.QueueConnectionFactory#createQueueConnection(String, String)
,
javax.jms.QueueConnectionFactory#createQueueConnection()
public final TopicConnection createTopicConnection() throws JMSException
JMSException
doCreateTopicConnection(java.lang.String, java.lang.String)
public TopicConnection createTopicConnection(java.lang.String username, java.lang.String password) throws JMSException
JMSException
protected TopicConnection doCreateTopicConnection(java.lang.String username, java.lang.String password) throws JMSException
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.username
- the username to usepassword
- the password to useJMSException
javax.jms.TopicConnectionFactory#createTopicConnection(String, String)
,
javax.jms.TopicConnectionFactory#createTopicConnection()