Class UserCredentialsConnectionFactoryAdapter

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

public class UserCredentialsConnectionFactoryAdapter extends Object implements jakarta.jms.ConnectionFactory, jakarta.jms.QueueConnectionFactory, jakarta.jms.TopicConnectionFactory, InitializingBean
An adapter for a target JMS ConnectionFactory, applying the given user credentials to every standard methods that can also be used with authentication, this createConnection() and createContext(). In other words, it is implicitly invoking createConnection(username, password) or createContext(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() and createContext() 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() or createContext() 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.

As of Spring Framework 5, this class delegates JMS 2.0 JMSContext calls and therefore requires the JMS 2.0 API to be present at runtime. It may nevertheless run against a JMS 1.1 driver (bound to the JMS 2.0 API) as long as no actual JMS 2.0 calls are triggered by the application's setup.

Since:
1.2
Author:
Juergen Hoeller, Stephane Nicoll
See Also:
  • Constructor Details

    • UserCredentialsConnectionFactoryAdapter

      public UserCredentialsConnectionFactoryAdapter()
  • Method Details

    • setTargetConnectionFactory

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

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

      public void setPassword(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 the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

      This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

      Specified by:
      afterPropertiesSet in interface InitializingBean
    • setCredentialsForCurrentThread

      public void setCredentialsForCurrentThread(String username, String password)
      Set user credentials 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

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

      public final jakarta.jms.Connection createConnection() throws jakarta.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) otherwise.
      Specified by:
      createConnection in interface jakarta.jms.ConnectionFactory
      Throws:
      jakarta.jms.JMSException
      See Also:
    • createConnection

      public jakarta.jms.Connection createConnection(String username, String password) throws jakarta.jms.JMSException
      Delegate the call straight to the target ConnectionFactory.
      Specified by:
      createConnection in interface jakarta.jms.ConnectionFactory
      Throws:
      jakarta.jms.JMSException
    • doCreateConnection

      protected jakarta.jms.Connection doCreateConnection(@Nullable String username, @Nullable String password) throws jakarta.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:
      jakarta.jms.JMSException
      See Also:
      • ConnectionFactory.createConnection(String, String)
      • ConnectionFactory.createConnection()
    • createQueueConnection

      public final jakarta.jms.QueueConnection createQueueConnection() throws jakarta.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 jakarta.jms.QueueConnectionFactory
      Throws:
      jakarta.jms.JMSException
      See Also:
    • createQueueConnection

      public jakarta.jms.QueueConnection createQueueConnection(String username, String password) throws jakarta.jms.JMSException
      Delegate the call straight to the target QueueConnectionFactory.
      Specified by:
      createQueueConnection in interface jakarta.jms.QueueConnectionFactory
      Throws:
      jakarta.jms.JMSException
    • doCreateQueueConnection

      protected jakarta.jms.QueueConnection doCreateQueueConnection(@Nullable String username, @Nullable String password) throws jakarta.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:
      jakarta.jms.JMSException
      See Also:
      • QueueConnectionFactory.createQueueConnection(String, String)
      • QueueConnectionFactory.createQueueConnection()
    • createTopicConnection

      public final jakarta.jms.TopicConnection createTopicConnection() throws jakarta.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 jakarta.jms.TopicConnectionFactory
      Throws:
      jakarta.jms.JMSException
      See Also:
    • createTopicConnection

      public jakarta.jms.TopicConnection createTopicConnection(String username, String password) throws jakarta.jms.JMSException
      Delegate the call straight to the target TopicConnectionFactory.
      Specified by:
      createTopicConnection in interface jakarta.jms.TopicConnectionFactory
      Throws:
      jakarta.jms.JMSException
    • doCreateTopicConnection

      protected jakarta.jms.TopicConnection doCreateTopicConnection(@Nullable String username, @Nullable String password) throws jakarta.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:
      jakarta.jms.JMSException
      See Also:
      • TopicConnectionFactory.createTopicConnection(String, String)
      • TopicConnectionFactory.createTopicConnection()
    • createContext

      public jakarta.jms.JMSContext createContext()
      Specified by:
      createContext in interface jakarta.jms.ConnectionFactory
    • doCreateContext

      protected jakarta.jms.JMSContext doCreateContext(@Nullable String username, @Nullable String password)
    • createContext

      public jakarta.jms.JMSContext createContext(String userName, String password)
      Specified by:
      createContext in interface jakarta.jms.ConnectionFactory
    • createContext

      public jakarta.jms.JMSContext createContext(String userName, String password, int sessionMode)
      Specified by:
      createContext in interface jakarta.jms.ConnectionFactory
    • createContext

      public jakarta.jms.JMSContext createContext(int sessionMode)
      Specified by:
      createContext in interface jakarta.jms.ConnectionFactory
    • doCreateContext

      protected jakarta.jms.JMSContext doCreateContext(@Nullable String username, @Nullable String password, int sessionMode)