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