001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.ra; 018 019import javax.jms.JMSException; 020import org.apache.activemq.ActiveMQConnection; 021import org.apache.activemq.ActiveMQConnectionFactory; 022import org.apache.activemq.ActiveMQSslConnectionFactory; 023import org.slf4j.Logger; 024import org.slf4j.LoggerFactory; 025 026/** 027 * Abstract base class providing support for creating physical 028 * connections to an ActiveMQ instance. 029 * 030 * 031 */ 032public class ActiveMQConnectionSupport { 033 034 private ActiveMQConnectionRequestInfo info = new ActiveMQConnectionRequestInfo(); 035 protected Logger log = LoggerFactory.getLogger(getClass()); 036 037 /** 038 * Creates a factory for obtaining physical connections to an Active MQ 039 * broker. The factory is configured with the given configuration information. 040 * 041 * @param connectionRequestInfo the configuration request information 042 * @param activationSpec 043 * @return the connection factory 044 * @throws java.lang.IllegalArgumentException if the server URL given in the 045 * configuration information is not a valid URL 046 */ 047 protected ActiveMQConnectionFactory createConnectionFactory(ActiveMQConnectionRequestInfo connectionRequestInfo, MessageActivationSpec activationSpec) { 048 //ActiveMQSslConnectionFactory defaults to TCP anyway 049 ActiveMQConnectionFactory factory = new ActiveMQSslConnectionFactory(); 050 connectionRequestInfo.configure(factory, activationSpec); 051 return factory; 052 } 053 054 /** 055 * Creates a new physical connection to an Active MQ broker identified by given 056 * connection request information. 057 * 058 * @param connectionRequestInfo the connection request information identifying the broker and any 059 * required connection parameters, e.g. username/password 060 * @return the physical connection 061 * @throws JMSException if the connection could not be established 062 */ 063 public ActiveMQConnection makeConnection(ActiveMQConnectionRequestInfo connectionRequestInfo) throws JMSException { 064 return makeConnection(connectionRequestInfo, createConnectionFactory(connectionRequestInfo, null)); 065 } 066 067 /** 068 * Creates a new physical connection to an Active MQ broker using a given 069 * connection factory and credentials supplied in connection request information. 070 * 071 * @param connectionRequestInfo the connection request information containing the credentials to use 072 * for the connection request 073 * @return the physical connection 074 * @throws JMSException if the connection could not be established 075 */ 076 public ActiveMQConnection makeConnection( 077 ActiveMQConnectionRequestInfo connectionRequestInfo, 078 ActiveMQConnectionFactory connectionFactory) throws JMSException 079 { 080 String userName = connectionRequestInfo.getUserName(); 081 String password = connectionRequestInfo.getPassword(); 082 ActiveMQConnection physicalConnection = (ActiveMQConnection) connectionFactory.createConnection(userName, password); 083 084 String clientId = connectionRequestInfo.getClientid(); 085 if ( clientId != null && clientId.length() > 0 ) 086 { 087 physicalConnection.setClientID(clientId); 088 } 089 return physicalConnection; 090 } 091 092 /** 093 * Gets the connection request information. 094 * 095 * @return the connection request information 096 */ 097 public ActiveMQConnectionRequestInfo getInfo() 098 { 099 return info; 100 } 101 102 /** 103 * Sets the connection request information as a whole. 104 * 105 * @param connectionRequestInfo the connection request information 106 */ 107 protected void setInfo(ActiveMQConnectionRequestInfo connectionRequestInfo){ 108 info = connectionRequestInfo; 109 if ( log.isDebugEnabled() ) { 110 log.debug(this + ", setting [info] to: " + info); 111 } 112 } 113 114 protected boolean notEqual(Object o1, Object o2) { 115 return (o1 == null ^ o2 == null) || (o1 != null && !o1.equals(o2)); 116 } 117 118 protected String emptyToNull(String value) { 119 if (value == null || value.length() == 0) 120 { 121 return null; 122 } 123 else 124 { 125 return value; 126 } 127 } 128 129 protected String defaultValue(String value, String defaultValue) { 130 if (value != null) { 131 return value; 132 } 133 return defaultValue; 134 } 135 136 // /////////////////////////////////////////////////////////////////////// 137 // 138 // Java Bean getters and setters for this ResourceAdapter class. 139 // 140 // /////////////////////////////////////////////////////////////////////// 141 142 /** 143 * @return client id 144 */ 145 public String getClientid() { 146 return emptyToNull(info.getClientid()); 147 } 148 149 /** 150 * @param clientid 151 */ 152 public void setClientid(String clientid) { 153 if ( log.isDebugEnabled() ) { 154 log.debug(this + ", setting [clientid] to: " + clientid); 155 } 156 info.setClientid(clientid); 157 } 158 159 /** 160 * @return password 161 */ 162 public String getPassword() { 163 return emptyToNull(info.getPassword()); 164 } 165 166 /** 167 * @param password 168 */ 169 public void setPassword(String password) { 170 if ( log.isDebugEnabled() ) { 171 log.debug(this + ", setting [password] property"); 172 } 173 info.setPassword(password); 174 } 175 176 /** 177 * @return server URL 178 */ 179 public String getServerUrl() { 180 return info.getServerUrl(); 181 } 182 183 /** 184 * @param url 185 */ 186 public void setServerUrl(String url) { 187 if ( log.isDebugEnabled() ) { 188 log.debug(this + ", setting [serverUrl] to: " + url); 189 } 190 info.setServerUrl(url); 191 } 192 193 public void setTrustStore(String trustStore) { 194 if ( log.isDebugEnabled() ) { 195 log.debug(this + ", setting [trustStore] to: " + trustStore); 196 } 197 info.setTrustStore(trustStore); 198 } 199 200 public void setTrustStorePassword(String trustStorePassword) { 201 if ( log.isDebugEnabled() ) { 202 log.debug(this + ", setting [trustStorePassword] to: " + trustStorePassword); 203 } 204 info.setTrustStorePassword(trustStorePassword); 205 } 206 207 public void setKeyStore(String keyStore) { 208 if ( log.isDebugEnabled() ) { 209 log.debug(this + ", setting [keyStore] to: " + keyStore); 210 } 211 info.setKeyStore(keyStore); 212 } 213 214 public void setKeyStorePassword(String keyStorePassword) { 215 if ( log.isDebugEnabled() ) { 216 log.debug(this + ", setting [keyStorePassword] to: " + keyStorePassword); 217 } 218 info.setKeyStorePassword(keyStorePassword); 219 } 220 221 public void setKeyStoreKeyPassword(String keyStoreKeyPassword) { 222 if ( log.isDebugEnabled() ) { 223 log.debug(this + ", setting [keyStoreKeyPassword] to: " + keyStoreKeyPassword); 224 } 225 info.setKeyStoreKeyPassword(keyStoreKeyPassword); 226 } 227 228 /** 229 * @return user name 230 */ 231 public String getUserName() { 232 return emptyToNull(info.getUserName()); 233 } 234 235 /** 236 * @param userid 237 */ 238 public void setUserName(String userid) { 239 if ( log.isDebugEnabled() ) { 240 log.debug("setting [userName] to: " + userid); 241 } 242 info.setUserName(userid); 243 } 244 245 /** 246 * @return durable topic prefetch 247 */ 248 public Integer getDurableTopicPrefetch() { 249 return info.getDurableTopicPrefetch(); 250 } 251 252 /** 253 * @param optimizeDurableTopicPrefetch 254 */ 255 public void setOptimizeDurableTopicPrefetch(Integer optimizeDurableTopicPrefetch) { 256 if ( log.isDebugEnabled() ) { 257 log.debug("setting [optimizeDurableTopicPrefetch] to: " + optimizeDurableTopicPrefetch); 258 } 259 info.setOptimizeDurableTopicPrefetch(optimizeDurableTopicPrefetch); 260 } 261 262 /** 263 * @return durable topic prefetch 264 */ 265 public Integer getOptimizeDurableTopicPrefetch() { 266 return info.getOptimizeDurableTopicPrefetch(); 267 } 268 269 /** 270 * @param durableTopicPrefetch 271 */ 272 public void setDurableTopicPrefetch(Integer durableTopicPrefetch) { 273 if ( log.isDebugEnabled() ) { 274 log.debug("setting [durableTopicPrefetch] to: " + durableTopicPrefetch); 275 } 276 info.setDurableTopicPrefetch(durableTopicPrefetch); 277 } 278 279 /** 280 * @return initial redelivery delay 281 */ 282 public Long getInitialRedeliveryDelay() { 283 return info.getInitialRedeliveryDelay(); 284 } 285 286 /** 287 * @param value 288 */ 289 public void setInitialRedeliveryDelay(Long value) { 290 if ( log.isDebugEnabled() ) { 291 log.debug("setting [initialRedeliveryDelay] to: " + value); 292 } 293 info.setInitialRedeliveryDelay(value); 294 } 295 296 297 /** 298 * @return initial redelivery delay 299 */ 300 public Long getMaximumRedeliveryDelay() { 301 return info.getMaximumRedeliveryDelay(); 302 } 303 304 /** 305 * @param value 306 */ 307 public void setMaximumRedeliveryDelay(Long value) { 308 if ( log.isDebugEnabled() ) { 309 log.debug("setting [maximumRedeliveryDelay] to: " + value); 310 } 311 info.setMaximumRedeliveryDelay(value); 312 } 313 314 /** 315 * @return input stream prefetch 316 */ 317 public Integer getInputStreamPrefetch() { 318 return info.getInputStreamPrefetch(); 319 } 320 321 /** 322 * @param inputStreamPrefetch 323 */ 324 public void setInputStreamPrefetch(Integer inputStreamPrefetch) { 325 if ( log.isDebugEnabled() ) { 326 log.debug("setting [inputStreamPrefetch] to: " + inputStreamPrefetch); 327 } 328 info.setInputStreamPrefetch(inputStreamPrefetch); 329 } 330 331 /** 332 * @return maximum redeliveries 333 */ 334 public Integer getMaximumRedeliveries() { 335 return info.getMaximumRedeliveries(); 336 } 337 338 /** 339 * @param value 340 */ 341 public void setMaximumRedeliveries(Integer value) { 342 if ( log.isDebugEnabled() ) { 343 log.debug("setting [maximumRedeliveries] to: " + value); 344 } 345 info.setMaximumRedeliveries(value); 346 } 347 348 /** 349 * @return queue browser prefetch 350 */ 351 public Integer getQueueBrowserPrefetch() { 352 return info.getQueueBrowserPrefetch(); 353 } 354 355 /** 356 * @param queueBrowserPrefetch 357 */ 358 public void setQueueBrowserPrefetch(Integer queueBrowserPrefetch) { 359 if ( log.isDebugEnabled() ) { 360 log.debug("setting [queueBrowserPrefetch] to: " + queueBrowserPrefetch); 361 } 362 info.setQueueBrowserPrefetch(queueBrowserPrefetch); 363 } 364 365 /** 366 * @return queue prefetch 367 */ 368 public Integer getQueuePrefetch() { 369 return info.getQueuePrefetch(); 370 } 371 372 /** 373 * @param queuePrefetch 374 */ 375 public void setQueuePrefetch(Integer queuePrefetch) { 376 if ( log.isDebugEnabled() ) { 377 log.debug("setting [queuePrefetch] to: " + queuePrefetch); 378 } 379 info.setQueuePrefetch(queuePrefetch); 380 } 381 382 /** 383 * @return redelivery backoff multiplier 384 */ 385 public Double getRedeliveryBackOffMultiplier() { 386 return info.getRedeliveryBackOffMultiplier(); 387 } 388 389 /** 390 * @param value 391 */ 392 public void setRedeliveryBackOffMultiplier(Double value) { 393 if ( log.isDebugEnabled() ) { 394 log.debug("setting [redeliveryBackOffMultiplier] to: " + value); 395 } 396 info.setRedeliveryBackOffMultiplier(value); 397 } 398 399 /** 400 * @return redelivery use exponential backoff 401 */ 402 public Boolean getRedeliveryUseExponentialBackOff() { 403 return info.getRedeliveryUseExponentialBackOff(); 404 } 405 406 /** 407 * @param value 408 */ 409 public void setRedeliveryUseExponentialBackOff(Boolean value) { 410 if ( log.isDebugEnabled() ) { 411 log.debug("setting [redeliveryUseExponentialBackOff] to: " + value); 412 } 413 info.setRedeliveryUseExponentialBackOff(value); 414 } 415 416 /** 417 * @return topic prefetch 418 */ 419 public Integer getTopicPrefetch() { 420 return info.getTopicPrefetch(); 421 } 422 423 /** 424 * @param topicPrefetch 425 */ 426 public void setTopicPrefetch(Integer topicPrefetch) { 427 if ( log.isDebugEnabled() ) { 428 log.debug("setting [topicPrefetch] to: " + topicPrefetch); 429 } 430 info.setTopicPrefetch(topicPrefetch); 431 } 432 433 /** 434 * @param i 435 */ 436 public void setAllPrefetchValues(Integer i) { 437 info.setAllPrefetchValues(i); 438 } 439 440 /** 441 * @return use inbound session enabled 442 */ 443 public boolean isUseInboundSessionEnabled() { 444 return info.isUseInboundSessionEnabled(); 445 } 446 447 /** 448 * @return use inbound session 449 */ 450 public Boolean getUseInboundSession() { 451 return info.getUseInboundSession(); 452 } 453 454 /** 455 * @param useInboundSession 456 */ 457 public void setUseInboundSession(Boolean useInboundSession) { 458 if ( log.isDebugEnabled() ) { 459 log.debug("setting [useInboundSession] to: " + useInboundSession); 460 } 461 info.setUseInboundSession(useInboundSession); 462 } 463 464 public boolean isUseSessionArgs() { 465 return info.isUseSessionArgs(); 466 } 467 468 public Boolean getUseSessionArgs() { 469 return info.getUseSessionArgs(); 470 } 471 472 /** 473 * if true, calls to managed connection factory.connection.createSession will 474 * respect the passed in args. When false (default) the args are ignored b/c 475 * the container will do transaction demarcation via xa or local transaction rar 476 * contracts. 477 * This option is useful when a managed connection is used in plain jms mode 478 * and a jms transacted session session is required. 479 * @param useSessionArgs 480 */ 481 public void setUseSessionArgs(Boolean useSessionArgs) { 482 if ( log.isDebugEnabled() ) { 483 log.debug(this + ", setting [useSessionArgs] to: " + useSessionArgs); 484 } 485 info.setUseSessionArgs(useSessionArgs); 486 } 487 488}