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.apache.commons.logging.Log; 023 import org.apache.commons.logging.LogFactory; 024 025 /** 026 * Abstract base class providing support for creating physical 027 * connections to an ActiveMQ instance. 028 * 029 * @version $Revision$ 030 */ 031 public class ActiveMQConnectionSupport { 032 033 private ActiveMQConnectionRequestInfo info = new ActiveMQConnectionRequestInfo(); 034 protected Log log = LogFactory.getLog(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 * @return input stream prefetch 240 */ 241 public Integer getInputStreamPrefetch() { 242 return info.getInputStreamPrefetch(); 243 } 244 245 /** 246 * @param inputStreamPrefetch 247 */ 248 public void setInputStreamPrefetch(Integer inputStreamPrefetch) { 249 if ( log.isDebugEnabled() ) { 250 log.debug("setting [inputStreamPrefetch] to: " + inputStreamPrefetch); 251 } 252 info.setInputStreamPrefetch(inputStreamPrefetch); 253 } 254 255 /** 256 * @return maximum redeliveries 257 */ 258 public Integer getMaximumRedeliveries() { 259 return info.getMaximumRedeliveries(); 260 } 261 262 /** 263 * @param value 264 */ 265 public void setMaximumRedeliveries(Integer value) { 266 if ( log.isDebugEnabled() ) { 267 log.debug("setting [maximumRedeliveries] to: " + value); 268 } 269 info.setMaximumRedeliveries(value); 270 } 271 272 /** 273 * @return queue browser prefetch 274 */ 275 public Integer getQueueBrowserPrefetch() { 276 return info.getQueueBrowserPrefetch(); 277 } 278 279 /** 280 * @param queueBrowserPrefetch 281 */ 282 public void setQueueBrowserPrefetch(Integer queueBrowserPrefetch) { 283 if ( log.isDebugEnabled() ) { 284 log.debug("setting [queueBrowserPrefetch] to: " + queueBrowserPrefetch); 285 } 286 info.setQueueBrowserPrefetch(queueBrowserPrefetch); 287 } 288 289 /** 290 * @return queue prefetch 291 */ 292 public Integer getQueuePrefetch() { 293 return info.getQueuePrefetch(); 294 } 295 296 /** 297 * @param queuePrefetch 298 */ 299 public void setQueuePrefetch(Integer queuePrefetch) { 300 if ( log.isDebugEnabled() ) { 301 log.debug("setting [queuePrefetch] to: " + queuePrefetch); 302 } 303 info.setQueuePrefetch(queuePrefetch); 304 } 305 306 /** 307 * @return redelivery backoff multiplier 308 */ 309 public Double getRedeliveryBackOffMultiplier() { 310 return info.getRedeliveryBackOffMultiplier(); 311 } 312 313 /** 314 * @param value 315 */ 316 public void setRedeliveryBackOffMultiplier(Short value) { 317 if ( log.isDebugEnabled() ) { 318 log.debug("setting [redeliveryBackOffMultiplier] to: " + value); 319 } 320 info.setRedeliveryBackOffMultiplier(value); 321 } 322 323 /** 324 * @return redelivery use exponential backoff 325 */ 326 public Boolean getRedeliveryUseExponentialBackOff() { 327 return info.getRedeliveryUseExponentialBackOff(); 328 } 329 330 /** 331 * @param value 332 */ 333 public void setRedeliveryUseExponentialBackOff(Boolean value) { 334 if ( log.isDebugEnabled() ) { 335 log.debug("setting [redeliveryUseExponentialBackOff] to: " + value); 336 } 337 info.setRedeliveryUseExponentialBackOff(value); 338 } 339 340 /** 341 * @return topic prefetch 342 */ 343 public Integer getTopicPrefetch() { 344 return info.getTopicPrefetch(); 345 } 346 347 /** 348 * @param topicPrefetch 349 */ 350 public void setTopicPrefetch(Integer topicPrefetch) { 351 if ( log.isDebugEnabled() ) { 352 log.debug("setting [topicPrefetch] to: " + topicPrefetch); 353 } 354 info.setTopicPrefetch(topicPrefetch); 355 } 356 357 /** 358 * @param i 359 */ 360 public void setAllPrefetchValues(Integer i) { 361 info.setAllPrefetchValues(i); 362 } 363 364 /** 365 * @return use inbound session enabled 366 */ 367 public boolean isUseInboundSessionEnabled() { 368 return info.isUseInboundSessionEnabled(); 369 } 370 371 /** 372 * @return use inbound session 373 */ 374 public Boolean getUseInboundSession() { 375 return info.getUseInboundSession(); 376 } 377 378 /** 379 * @param useInboundSession 380 */ 381 public void setUseInboundSession(Boolean useInboundSession) { 382 if ( log.isDebugEnabled() ) { 383 log.debug("setting [useInboundSession] to: " + useInboundSession); 384 } 385 info.setUseInboundSession(useInboundSession); 386 } 387 388 }