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 optimizeDurableTopicPrefetch
213         */
214        public void setOptimizeDurableTopicPrefetch(Integer optimizeDurableTopicPrefetch) {
215            if ( log.isDebugEnabled() ) {
216                log.debug("setting [optimizeDurableTopicPrefetch] to: " + optimizeDurableTopicPrefetch);
217            }
218            info.setOptimizeDurableTopicPrefetch(optimizeDurableTopicPrefetch);
219        }
220    
221        /**
222         * @return durable topic prefetch
223         */
224        public Integer getOptimizeDurableTopicPrefetch() {
225            return info.getOptimizeDurableTopicPrefetch();
226        }
227    
228        /**
229         * @param durableTopicPrefetch
230         */
231        public void setDurableTopicPrefetch(Integer durableTopicPrefetch) {
232            if ( log.isDebugEnabled() ) {
233                log.debug("setting [durableTopicPrefetch] to: " + durableTopicPrefetch);
234            }
235            info.setDurableTopicPrefetch(durableTopicPrefetch);
236        }
237    
238        /**
239         * @return initial redelivery delay
240         */
241        public Long getInitialRedeliveryDelay() {
242            return info.getInitialRedeliveryDelay();
243        }
244    
245        /**
246         * @param value
247         */
248        public void setInitialRedeliveryDelay(Long value) {
249            if ( log.isDebugEnabled() ) {
250                log.debug("setting [initialRedeliveryDelay] to: " + value);
251            }
252            info.setInitialRedeliveryDelay(value);
253        }
254    
255    
256        /**
257         * @return initial redelivery delay
258         */
259        public Long getMaximumRedeliveryDelay() {
260            return info.getMaximumRedeliveryDelay();
261        }
262    
263        /**
264         * @param value
265         */
266        public void setMaximumRedeliveryDelay(Long value) {
267            if ( log.isDebugEnabled() ) {
268                log.debug("setting [maximumRedeliveryDelay] to: " + value);
269            }
270            info.setMaximumRedeliveryDelay(value);
271        }
272    
273        /**
274         * @return input stream prefetch
275         */
276        public Integer getInputStreamPrefetch() {
277            return info.getInputStreamPrefetch();
278        }
279    
280        /**
281         * @param inputStreamPrefetch
282         */
283        public void setInputStreamPrefetch(Integer inputStreamPrefetch) {
284            if ( log.isDebugEnabled() ) {
285                log.debug("setting [inputStreamPrefetch] to: " + inputStreamPrefetch);
286            }
287            info.setInputStreamPrefetch(inputStreamPrefetch);
288        }
289    
290        /**
291         * @return maximum redeliveries
292         */
293        public Integer getMaximumRedeliveries() {
294            return info.getMaximumRedeliveries();
295        }
296    
297        /**
298         * @param value
299         */
300        public void setMaximumRedeliveries(Integer value) {
301            if ( log.isDebugEnabled() ) {
302                log.debug("setting [maximumRedeliveries] to: " + value);
303            }
304            info.setMaximumRedeliveries(value);
305        }
306    
307        /**
308         * @return queue browser prefetch
309         */
310        public Integer getQueueBrowserPrefetch() {
311            return info.getQueueBrowserPrefetch();
312        }
313    
314        /**
315         * @param queueBrowserPrefetch
316         */
317        public void setQueueBrowserPrefetch(Integer queueBrowserPrefetch) {
318            if ( log.isDebugEnabled() ) {
319                log.debug("setting [queueBrowserPrefetch] to: " + queueBrowserPrefetch);
320            }
321            info.setQueueBrowserPrefetch(queueBrowserPrefetch);
322        }
323    
324        /**
325         * @return queue prefetch
326         */
327        public Integer getQueuePrefetch() {
328            return info.getQueuePrefetch();
329        }
330    
331        /**
332         * @param queuePrefetch
333         */
334        public void setQueuePrefetch(Integer queuePrefetch) {
335            if ( log.isDebugEnabled() ) {
336                log.debug("setting [queuePrefetch] to: " + queuePrefetch);
337            }
338            info.setQueuePrefetch(queuePrefetch);
339        }
340    
341        /**
342         * @return redelivery backoff multiplier
343         */
344        public Double getRedeliveryBackOffMultiplier() {
345            return info.getRedeliveryBackOffMultiplier();
346        }
347    
348        /**
349         * @param value
350         */
351        public void setRedeliveryBackOffMultiplier(Double value) {
352            if ( log.isDebugEnabled() ) {
353                log.debug("setting [redeliveryBackOffMultiplier] to: " + value);
354            }
355            info.setRedeliveryBackOffMultiplier(value);
356        }
357    
358        /**
359         * @return redelivery use exponential backoff
360         */
361        public Boolean getRedeliveryUseExponentialBackOff() {
362            return info.getRedeliveryUseExponentialBackOff();
363        }
364    
365        /**
366         * @param value
367         */
368        public void setRedeliveryUseExponentialBackOff(Boolean value) {
369            if ( log.isDebugEnabled() ) {
370                log.debug("setting [redeliveryUseExponentialBackOff] to: " + value);
371            }
372            info.setRedeliveryUseExponentialBackOff(value);
373        }
374    
375        /**
376         * @return topic prefetch
377         */
378        public Integer getTopicPrefetch() {
379            return info.getTopicPrefetch();
380        }
381    
382        /**
383         * @param topicPrefetch
384         */
385        public void setTopicPrefetch(Integer topicPrefetch) {
386            if ( log.isDebugEnabled() ) {
387                log.debug("setting [topicPrefetch] to: " + topicPrefetch);
388            }
389            info.setTopicPrefetch(topicPrefetch);
390        }
391    
392        /**
393         * @param i
394         */
395        public void setAllPrefetchValues(Integer i) {
396            info.setAllPrefetchValues(i);
397        }
398    
399        /**
400         * @return use inbound session enabled
401         */
402        public boolean isUseInboundSessionEnabled() {
403            return info.isUseInboundSessionEnabled();
404        }
405    
406        /**
407         * @return use inbound session
408         */
409        public Boolean getUseInboundSession() {
410            return info.getUseInboundSession();
411        }
412    
413        /**
414         * @param useInboundSession
415         */
416        public void setUseInboundSession(Boolean useInboundSession) {
417            if ( log.isDebugEnabled() ) {
418                log.debug("setting [useInboundSession] to: " + useInboundSession);
419            }
420            info.setUseInboundSession(useInboundSession);
421        }
422    
423    }