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 */ 017 package org.apache.activemq.ra; 018 019 import javax.jms.JMSException; 020 import org.apache.activemq.ActiveMQConnection; 021 import org.apache.activemq.ActiveMQConnectionFactory; 022 import org.slf4j.Logger; 023 import org.slf4j.LoggerFactory; 024 025 /** 026 * Abstract base class providing support for creating physical 027 * connections to an ActiveMQ instance. 028 * 029 * 030 */ 031 public class ActiveMQConnectionSupport { 032 033 private ActiveMQConnectionRequestInfo info = new ActiveMQConnectionRequestInfo(); 034 protected Logger log = LoggerFactory.getLogger(getClass()); 035 036 /** 037 * Creates a factory for obtaining physical connections to an Active MQ 038 * broker. The factory is configured with the given configuration information. 039 * 040 * @param connectionRequestInfo the configuration request information 041 * @return the connection factory 042 * @throws java.lang.IllegalArgumentException if the server URL given in the 043 * configuration information is not a valid URL 044 */ 045 protected ActiveMQConnectionFactory createConnectionFactory(ActiveMQConnectionRequestInfo connectionRequestInfo) { 046 ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(); 047 connectionRequestInfo.configure(factory); 048 return factory; 049 } 050 051 /** 052 * Creates a new physical connection to an Active MQ broker identified by given 053 * connection request information. 054 * 055 * @param connectionRequestInfo the connection request information identifying the broker and any 056 * required connection parameters, e.g. username/password 057 * @return the physical connection 058 * @throws JMSException if the connection could not be established 059 */ 060 public ActiveMQConnection makeConnection(ActiveMQConnectionRequestInfo connectionRequestInfo) throws JMSException{ 061 return makeConnection(connectionRequestInfo, createConnectionFactory(connectionRequestInfo)); 062 } 063 064 /** 065 * Creates a new physical connection to an Active MQ broker using a given 066 * connection factory and credentials supplied in connection request information. 067 * 068 * @param connectionRequestInfo the connection request information containing the credentials to use 069 * for the connection request 070 * @return the physical connection 071 * @throws JMSException if the connection could not be established 072 */ 073 public ActiveMQConnection makeConnection( 074 ActiveMQConnectionRequestInfo connectionRequestInfo, 075 ActiveMQConnectionFactory connectionFactory) throws JMSException 076 { 077 String userName = connectionRequestInfo.getUserName(); 078 String password = connectionRequestInfo.getPassword(); 079 ActiveMQConnection physicalConnection = (ActiveMQConnection) connectionFactory.createConnection(userName, password); 080 081 String clientId = connectionRequestInfo.getClientid(); 082 if ( clientId != null && clientId.length() > 0 ) 083 { 084 physicalConnection.setClientID(clientId); 085 } 086 return physicalConnection; 087 } 088 089 /** 090 * Gets the connection request information. 091 * 092 * @return the connection request information 093 */ 094 public ActiveMQConnectionRequestInfo getInfo() 095 { 096 return info; 097 } 098 099 /** 100 * Sets the connection request information as a whole. 101 * 102 * @param connectionRequestInfo the connection request information 103 */ 104 protected void setInfo(ActiveMQConnectionRequestInfo connectionRequestInfo){ 105 info = connectionRequestInfo; 106 } 107 108 protected boolean notEqual(Object o1, Object o2) { 109 return (o1 == null ^ o2 == null) || (o1 != null && !o1.equals(o2)); 110 } 111 112 protected String emptyToNull(String value) { 113 if (value == null || value.length() == 0) 114 { 115 return null; 116 } 117 else 118 { 119 return value; 120 } 121 } 122 123 protected String defaultValue(String value, String defaultValue) { 124 if (value != null) { 125 return value; 126 } 127 return defaultValue; 128 } 129 130 // /////////////////////////////////////////////////////////////////////// 131 // 132 // Java Bean getters and setters for this ResourceAdapter class. 133 // 134 // /////////////////////////////////////////////////////////////////////// 135 136 /** 137 * @return client id 138 */ 139 public String getClientid() { 140 return emptyToNull(info.getClientid()); 141 } 142 143 /** 144 * @param clientid 145 */ 146 public void setClientid(String clientid) { 147 if ( log.isDebugEnabled() ) { 148 log.debug("setting [clientid] to: " + clientid); 149 } 150 info.setClientid(clientid); 151 } 152 153 /** 154 * @return password 155 */ 156 public String getPassword() { 157 return emptyToNull(info.getPassword()); 158 } 159 160 /** 161 * @param password 162 */ 163 public void setPassword(String password) { 164 if ( log.isDebugEnabled() ) { 165 log.debug("setting [password] property"); 166 } 167 info.setPassword(password); 168 } 169 170 /** 171 * @return server URL 172 */ 173 public String getServerUrl() { 174 return info.getServerUrl(); 175 } 176 177 /** 178 * @param url 179 */ 180 public void setServerUrl(String url) { 181 if ( log.isDebugEnabled() ) { 182 log.debug("setting [serverUrl] to: " + url); 183 } 184 info.setServerUrl(url); 185 } 186 187 /** 188 * @return user name 189 */ 190 public String getUserName() { 191 return emptyToNull(info.getUserName()); 192 } 193 194 /** 195 * @param userid 196 */ 197 public void setUserName(String userid) { 198 if ( log.isDebugEnabled() ) { 199 log.debug("setting [userName] to: " + userid); 200 } 201 info.setUserName(userid); 202 } 203 204 /** 205 * @return durable topic prefetch 206 */ 207 public Integer getDurableTopicPrefetch() { 208 return info.getDurableTopicPrefetch(); 209 } 210 211 /** 212 * @param durableTopicPrefetch 213 */ 214 public void setDurableTopicPrefetch(Integer durableTopicPrefetch) { 215 if ( log.isDebugEnabled() ) { 216 log.debug("setting [durableTopicPrefetch] to: " + durableTopicPrefetch); 217 } 218 info.setDurableTopicPrefetch(durableTopicPrefetch); 219 } 220 221 /** 222 * @return initial redelivery delay 223 */ 224 public Long getInitialRedeliveryDelay() { 225 return info.getInitialRedeliveryDelay(); 226 } 227 228 /** 229 * @param value 230 */ 231 public void setInitialRedeliveryDelay(Long value) { 232 if ( log.isDebugEnabled() ) { 233 log.debug("setting [initialRedeliveryDelay] to: " + value); 234 } 235 info.setInitialRedeliveryDelay(value); 236 } 237 238 239 /** 240 * @return initial redelivery delay 241 */ 242 public Long getMaximumRedeliveryDelay() { 243 return info.getMaximumRedeliveryDelay(); 244 } 245 246 /** 247 * @param value 248 */ 249 public void setMaximumRedeliveryDelay(Long value) { 250 if ( log.isDebugEnabled() ) { 251 log.debug("setting [maximumRedeliveryDelay] to: " + value); 252 } 253 info.setMaximumRedeliveryDelay(value); 254 } 255 256 /** 257 * @return input stream prefetch 258 */ 259 public Integer getInputStreamPrefetch() { 260 return info.getInputStreamPrefetch(); 261 } 262 263 /** 264 * @param inputStreamPrefetch 265 */ 266 public void setInputStreamPrefetch(Integer inputStreamPrefetch) { 267 if ( log.isDebugEnabled() ) { 268 log.debug("setting [inputStreamPrefetch] to: " + inputStreamPrefetch); 269 } 270 info.setInputStreamPrefetch(inputStreamPrefetch); 271 } 272 273 /** 274 * @return maximum redeliveries 275 */ 276 public Integer getMaximumRedeliveries() { 277 return info.getMaximumRedeliveries(); 278 } 279 280 /** 281 * @param value 282 */ 283 public void setMaximumRedeliveries(Integer value) { 284 if ( log.isDebugEnabled() ) { 285 log.debug("setting [maximumRedeliveries] to: " + value); 286 } 287 info.setMaximumRedeliveries(value); 288 } 289 290 /** 291 * @return queue browser prefetch 292 */ 293 public Integer getQueueBrowserPrefetch() { 294 return info.getQueueBrowserPrefetch(); 295 } 296 297 /** 298 * @param queueBrowserPrefetch 299 */ 300 public void setQueueBrowserPrefetch(Integer queueBrowserPrefetch) { 301 if ( log.isDebugEnabled() ) { 302 log.debug("setting [queueBrowserPrefetch] to: " + queueBrowserPrefetch); 303 } 304 info.setQueueBrowserPrefetch(queueBrowserPrefetch); 305 } 306 307 /** 308 * @return queue prefetch 309 */ 310 public Integer getQueuePrefetch() { 311 return info.getQueuePrefetch(); 312 } 313 314 /** 315 * @param queuePrefetch 316 */ 317 public void setQueuePrefetch(Integer queuePrefetch) { 318 if ( log.isDebugEnabled() ) { 319 log.debug("setting [queuePrefetch] to: " + queuePrefetch); 320 } 321 info.setQueuePrefetch(queuePrefetch); 322 } 323 324 /** 325 * @return redelivery backoff multiplier 326 */ 327 public Double getRedeliveryBackOffMultiplier() { 328 return info.getRedeliveryBackOffMultiplier(); 329 } 330 331 /** 332 * @param value 333 */ 334 public void setRedeliveryBackOffMultiplier(Double value) { 335 if ( log.isDebugEnabled() ) { 336 log.debug("setting [redeliveryBackOffMultiplier] to: " + value); 337 } 338 info.setRedeliveryBackOffMultiplier(value); 339 } 340 341 /** 342 * @return redelivery use exponential backoff 343 */ 344 public Boolean getRedeliveryUseExponentialBackOff() { 345 return info.getRedeliveryUseExponentialBackOff(); 346 } 347 348 /** 349 * @param value 350 */ 351 public void setRedeliveryUseExponentialBackOff(Boolean value) { 352 if ( log.isDebugEnabled() ) { 353 log.debug("setting [redeliveryUseExponentialBackOff] to: " + value); 354 } 355 info.setRedeliveryUseExponentialBackOff(value); 356 } 357 358 /** 359 * @return topic prefetch 360 */ 361 public Integer getTopicPrefetch() { 362 return info.getTopicPrefetch(); 363 } 364 365 /** 366 * @param topicPrefetch 367 */ 368 public void setTopicPrefetch(Integer topicPrefetch) { 369 if ( log.isDebugEnabled() ) { 370 log.debug("setting [topicPrefetch] to: " + topicPrefetch); 371 } 372 info.setTopicPrefetch(topicPrefetch); 373 } 374 375 /** 376 * @param i 377 */ 378 public void setAllPrefetchValues(Integer i) { 379 info.setAllPrefetchValues(i); 380 } 381 382 /** 383 * @return use inbound session enabled 384 */ 385 public boolean isUseInboundSessionEnabled() { 386 return info.isUseInboundSessionEnabled(); 387 } 388 389 /** 390 * @return use inbound session 391 */ 392 public Boolean getUseInboundSession() { 393 return info.getUseInboundSession(); 394 } 395 396 /** 397 * @param useInboundSession 398 */ 399 public void setUseInboundSession(Boolean useInboundSession) { 400 if ( log.isDebugEnabled() ) { 401 log.debug("setting [useInboundSession] to: " + useInboundSession); 402 } 403 info.setUseInboundSession(useInboundSession); 404 } 405 406 }