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 void setTrustStore(String trustStore) { 197 if (log.isDebugEnabled()) { 198 log.debug(this + ", setting [trustStore] to: " + trustStore); 199 } 200 info.setTrustStore(trustStore); 201 } 202 203 public void setTrustStorePassword(String trustStorePassword) { 204 if (log.isDebugEnabled()) { 205 log.debug(this + ", setting [trustStorePassword] to: " + trustStorePassword); 206 } 207 info.setTrustStorePassword(trustStorePassword); 208 } 209 210 public void setKeyStore(String keyStore) { 211 if (log.isDebugEnabled()) { 212 log.debug(this + ", setting [keyStore] to: " + keyStore); 213 } 214 info.setKeyStore(keyStore); 215 } 216 217 public void setKeyStorePassword(String keyStorePassword) { 218 if (log.isDebugEnabled()) { 219 log.debug(this + ", setting [keyStorePassword] to: " + keyStorePassword); 220 } 221 info.setKeyStorePassword(keyStorePassword); 222 } 223 224 public void setKeyStoreKeyPassword(String keyStoreKeyPassword) { 225 if (log.isDebugEnabled()) { 226 log.debug(this + ", setting [keyStoreKeyPassword] to: " + keyStoreKeyPassword); 227 } 228 info.setKeyStoreKeyPassword(keyStoreKeyPassword); 229 } 230 231 /** 232 * @return user name 233 */ 234 public String getUserName() { 235 return emptyToNull(info.getUserName()); 236 } 237 238 /** 239 * @param userid 240 */ 241 public void setUserName(String userid) { 242 if (log.isDebugEnabled()) { 243 log.debug("setting [userName] to: " + userid); 244 } 245 info.setUserName(userid); 246 } 247 248 /** 249 * @return durable topic prefetch 250 */ 251 public Integer getDurableTopicPrefetch() { 252 return info.getDurableTopicPrefetch(); 253 } 254 255 /** 256 * @param optimizeDurableTopicPrefetch 257 */ 258 public void setOptimizeDurableTopicPrefetch(Integer optimizeDurableTopicPrefetch) { 259 if (log.isDebugEnabled()) { 260 log.debug("setting [optimizeDurableTopicPrefetch] to: " + optimizeDurableTopicPrefetch); 261 } 262 info.setOptimizeDurableTopicPrefetch(optimizeDurableTopicPrefetch); 263 } 264 265 /** 266 * @return durable topic prefetch 267 */ 268 public Integer getOptimizeDurableTopicPrefetch() { 269 return info.getOptimizeDurableTopicPrefetch(); 270 } 271 272 /** 273 * @param durableTopicPrefetch 274 */ 275 public void setDurableTopicPrefetch(Integer durableTopicPrefetch) { 276 if (log.isDebugEnabled()) { 277 log.debug("setting [durableTopicPrefetch] to: " + durableTopicPrefetch); 278 } 279 info.setDurableTopicPrefetch(durableTopicPrefetch); 280 } 281 282 /** 283 * @return initial redelivery delay 284 */ 285 public Long getInitialRedeliveryDelay() { 286 return info.getInitialRedeliveryDelay(); 287 } 288 289 /** 290 * @param value 291 */ 292 public void setInitialRedeliveryDelay(Long value) { 293 if (log.isDebugEnabled()) { 294 log.debug("setting [initialRedeliveryDelay] to: " + value); 295 } 296 info.setInitialRedeliveryDelay(value); 297 } 298 299 /** 300 * @return initial redelivery delay 301 */ 302 public Long getMaximumRedeliveryDelay() { 303 return info.getMaximumRedeliveryDelay(); 304 } 305 306 /** 307 * @param value 308 */ 309 public void setMaximumRedeliveryDelay(Long value) { 310 if (log.isDebugEnabled()) { 311 log.debug("setting [maximumRedeliveryDelay] to: " + value); 312 } 313 info.setMaximumRedeliveryDelay(value); 314 } 315 316 /** 317 * @return input stream prefetch 318 */ 319 @Deprecated 320 public Integer getInputStreamPrefetch() { 321 return 0; 322 } 323 324 /** 325 * @return maximum redeliveries 326 */ 327 public Integer getMaximumRedeliveries() { 328 return info.getMaximumRedeliveries(); 329 } 330 331 /** 332 * @param value 333 */ 334 public void setMaximumRedeliveries(Integer value) { 335 if (log.isDebugEnabled()) { 336 log.debug("setting [maximumRedeliveries] to: " + value); 337 } 338 info.setMaximumRedeliveries(value); 339 } 340 341 /** 342 * @return queue browser prefetch 343 */ 344 public Integer getQueueBrowserPrefetch() { 345 return info.getQueueBrowserPrefetch(); 346 } 347 348 /** 349 * @param queueBrowserPrefetch 350 */ 351 public void setQueueBrowserPrefetch(Integer queueBrowserPrefetch) { 352 if (log.isDebugEnabled()) { 353 log.debug("setting [queueBrowserPrefetch] to: " + queueBrowserPrefetch); 354 } 355 info.setQueueBrowserPrefetch(queueBrowserPrefetch); 356 } 357 358 /** 359 * @return queue prefetch 360 */ 361 public Integer getQueuePrefetch() { 362 return info.getQueuePrefetch(); 363 } 364 365 /** 366 * @param queuePrefetch 367 */ 368 public void setQueuePrefetch(Integer queuePrefetch) { 369 if (log.isDebugEnabled()) { 370 log.debug("setting [queuePrefetch] to: " + queuePrefetch); 371 } 372 info.setQueuePrefetch(queuePrefetch); 373 } 374 375 /** 376 * @return redelivery backoff multiplier 377 */ 378 public Double getRedeliveryBackOffMultiplier() { 379 return info.getRedeliveryBackOffMultiplier(); 380 } 381 382 /** 383 * @param value 384 */ 385 public void setRedeliveryBackOffMultiplier(Double value) { 386 if (log.isDebugEnabled()) { 387 log.debug("setting [redeliveryBackOffMultiplier] to: " + value); 388 } 389 info.setRedeliveryBackOffMultiplier(value); 390 } 391 392 /** 393 * @return redelivery use exponential backoff 394 */ 395 public Boolean getRedeliveryUseExponentialBackOff() { 396 return info.getRedeliveryUseExponentialBackOff(); 397 } 398 399 /** 400 * @param value 401 */ 402 public void setRedeliveryUseExponentialBackOff(Boolean value) { 403 if (log.isDebugEnabled()) { 404 log.debug("setting [redeliveryUseExponentialBackOff] to: " + value); 405 } 406 info.setRedeliveryUseExponentialBackOff(value); 407 } 408 409 /** 410 * @return topic prefetch 411 */ 412 public Integer getTopicPrefetch() { 413 return info.getTopicPrefetch(); 414 } 415 416 /** 417 * @param topicPrefetch 418 */ 419 public void setTopicPrefetch(Integer topicPrefetch) { 420 if (log.isDebugEnabled()) { 421 log.debug("setting [topicPrefetch] to: " + topicPrefetch); 422 } 423 info.setTopicPrefetch(topicPrefetch); 424 } 425 426 /** 427 * @param i 428 */ 429 public void setAllPrefetchValues(Integer i) { 430 info.setAllPrefetchValues(i); 431 } 432 433 /** 434 * @return use inbound session enabled 435 */ 436 public boolean isUseInboundSessionEnabled() { 437 return info.isUseInboundSessionEnabled(); 438 } 439 440 /** 441 * @return use inbound session 442 */ 443 public Boolean getUseInboundSession() { 444 return info.getUseInboundSession(); 445 } 446 447 /** 448 * @param useInboundSession 449 */ 450 public void setUseInboundSession(Boolean useInboundSession) { 451 if (log.isDebugEnabled()) { 452 log.debug("setting [useInboundSession] to: " + useInboundSession); 453 } 454 info.setUseInboundSession(useInboundSession); 455 } 456 457 public boolean isUseSessionArgs() { 458 return info.isUseSessionArgs(); 459 } 460 461 public Boolean getUseSessionArgs() { 462 return info.getUseSessionArgs(); 463 } 464 465 /** 466 * if true, calls to managed connection factory.connection.createSession 467 * will respect the passed in args. When false (default) the args are 468 * ignored b/c the container will do transaction demarcation via xa or local 469 * transaction rar contracts. This option is useful when a managed 470 * connection is used in plain jms mode and a jms transacted session session 471 * is required. 472 * 473 * @param useSessionArgs 474 */ 475 public void setUseSessionArgs(Boolean useSessionArgs) { 476 if (log.isDebugEnabled()) { 477 log.debug(this + ", setting [useSessionArgs] to: " + useSessionArgs); 478 } 479 info.setUseSessionArgs(useSessionArgs); 480 } 481}