1 16 17 package org.springframework.jms.connection; 18 19 import javax.jms.Connection ; 20 import javax.jms.ConnectionFactory ; 21 import javax.jms.JMSException ; 22 import javax.jms.QueueConnection ; 23 import javax.jms.QueueConnectionFactory ; 24 import javax.jms.TopicConnection ; 25 import javax.jms.TopicConnectionFactory ; 26 27 import org.springframework.beans.factory.InitializingBean; 28 import org.springframework.util.Assert; 29 import org.springframework.util.StringUtils; 30 31 69 public class UserCredentialsConnectionFactoryAdapter 70 implements ConnectionFactory , QueueConnectionFactory , TopicConnectionFactory , InitializingBean { 71 72 private ConnectionFactory targetConnectionFactory; 73 74 private String username; 75 76 private String password; 77 78 private final ThreadLocal threadBoundCredentials = new ThreadLocal (); 79 80 81 84 public void setTargetConnectionFactory(ConnectionFactory targetConnectionFactory) { 85 Assert.notNull(targetConnectionFactory, "'targetConnectionFactory' must not be null"); 86 this.targetConnectionFactory = targetConnectionFactory; 87 } 88 89 93 public void setUsername(String username) { 94 this.username = username; 95 } 96 97 101 public void setPassword(String password) { 102 this.password = password; 103 } 104 105 public void afterPropertiesSet() { 106 if (this.targetConnectionFactory == null) { 107 throw new IllegalArgumentException ("Property 'targetConnectionFactory' is required"); 108 } 109 } 110 111 112 122 public void setCredentialsForCurrentThread(String username, String password) { 123 this.threadBoundCredentials.set(new String [] {username, password}); 124 } 125 126 131 public void removeCredentialsFromCurrentThread() { 132 this.threadBoundCredentials.set(null); 133 } 134 135 136 142 public final Connection createConnection() throws JMSException { 143 String [] threadCredentials = (String []) this.threadBoundCredentials.get(); 144 if (threadCredentials != null) { 145 return doCreateConnection(threadCredentials[0], threadCredentials[1]); 146 } 147 else { 148 return doCreateConnection(this.username, this.password); 149 } 150 } 151 152 155 public Connection createConnection(String username, String password) throws JMSException { 156 return doCreateConnection(username, password); 157 } 158 159 170 protected Connection doCreateConnection(String username, String password) throws JMSException { 171 Assert.state(this.targetConnectionFactory != null, "'targetConnectionFactory' is required"); 172 if (StringUtils.hasLength(username)) { 173 return this.targetConnectionFactory.createConnection(username, password); 174 } 175 else { 176 return this.targetConnectionFactory.createConnection(); 177 } 178 } 179 180 181 187 public final QueueConnection createQueueConnection() throws JMSException { 188 String [] threadCredentials = (String []) this.threadBoundCredentials.get(); 189 if (threadCredentials != null) { 190 return doCreateQueueConnection(threadCredentials[0], threadCredentials[1]); 191 } 192 else { 193 return doCreateQueueConnection(this.username, this.password); 194 } 195 } 196 197 200 public QueueConnection createQueueConnection(String username, String password) throws JMSException { 201 return doCreateQueueConnection(username, password); 202 } 203 204 215 protected QueueConnection doCreateQueueConnection(String username, String password) throws JMSException { 216 Assert.state(this.targetConnectionFactory != null, "'targetConnectionFactory' is required"); 217 if (!(this.targetConnectionFactory instanceof QueueConnectionFactory )) { 218 throw new javax.jms.IllegalStateException ("'targetConnectionFactory' is not a QueueConnectionFactory"); 219 } 220 QueueConnectionFactory queueFactory = (QueueConnectionFactory ) this.targetConnectionFactory; 221 if (StringUtils.hasLength(username)) { 222 return queueFactory.createQueueConnection(username, password); 223 } 224 else { 225 return queueFactory.createQueueConnection(); 226 } 227 } 228 229 230 236 public final TopicConnection createTopicConnection() throws JMSException { 237 String [] threadCredentials = (String []) this.threadBoundCredentials.get(); 238 if (threadCredentials != null) { 239 return doCreateTopicConnection(threadCredentials[0], threadCredentials[1]); 240 } 241 else { 242 return doCreateTopicConnection(this.username, this.password); 243 } 244 } 245 246 249 public TopicConnection createTopicConnection(String username, String password) throws JMSException { 250 return doCreateTopicConnection(username, password); 251 } 252 253 264 protected TopicConnection doCreateTopicConnection(String username, String password) throws JMSException { 265 Assert.state(this.targetConnectionFactory != null, "'targetConnectionFactory' is required"); 266 if (!(this.targetConnectionFactory instanceof TopicConnectionFactory )) { 267 throw new javax.jms.IllegalStateException ("'targetConnectionFactory' is not a TopicConnectionFactory"); 268 } 269 TopicConnectionFactory queueFactory = (TopicConnectionFactory ) this.targetConnectionFactory; 270 if (StringUtils.hasLength(username)) { 271 return queueFactory.createTopicConnection(username, password); 272 } 273 else { 274 return queueFactory.createTopicConnection(); 275 } 276 } 277 278 } 279 | Popular Tags |