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 java.io.Serializable;
020    
021    import javax.resource.spi.ConnectionRequestInfo;
022    
023    import org.apache.activemq.ActiveMQConnectionFactory;
024    import org.apache.activemq.ActiveMQPrefetchPolicy;
025    import org.apache.activemq.RedeliveryPolicy;
026    
027    /**
028     *  Must override equals and hashCode (JCA spec 16.4)
029     */
030    public class ActiveMQConnectionRequestInfo implements ConnectionRequestInfo, Serializable, Cloneable {
031    
032        private static final long serialVersionUID = -5754338187296859149L;
033    
034        private String userName;
035        private String password;
036        private String serverUrl;
037        private String clientid;
038        private Boolean useInboundSession;
039        private RedeliveryPolicy redeliveryPolicy;
040        private ActiveMQPrefetchPolicy prefetchPolicy;
041    
042        public ActiveMQConnectionRequestInfo copy() {
043            try {
044                ActiveMQConnectionRequestInfo answer = (ActiveMQConnectionRequestInfo)clone();
045                if (redeliveryPolicy != null) {
046                    answer.redeliveryPolicy = redeliveryPolicy.copy();
047                }
048                return answer;
049            } catch (CloneNotSupportedException e) {
050                throw new RuntimeException("Could not clone: " + e, e);
051            }
052        }
053    
054        /**
055         * Returns true if this object will configure an ActiveMQConnectionFactory
056         * in any way
057         */
058        public boolean isConnectionFactoryConfigured() {
059            return serverUrl != null || clientid != null || redeliveryPolicy != null || prefetchPolicy != null;
060        }
061    
062        /**
063         * Configures the given connection factory
064         */
065        public void configure(ActiveMQConnectionFactory factory) {
066            if (serverUrl != null) {
067                factory.setBrokerURL(serverUrl);
068            }
069            if (clientid != null) {
070                factory.setClientID(clientid);
071            }
072            if (redeliveryPolicy != null) {
073                factory.setRedeliveryPolicy(redeliveryPolicy);
074            }
075            if (prefetchPolicy != null) {
076                factory.setPrefetchPolicy(prefetchPolicy);
077            }
078        }
079    
080        /**
081         * @see javax.resource.spi.ConnectionRequestInfo#hashCode()
082         */
083        public int hashCode() {
084            int rc = 0;
085            if (useInboundSession != null) {
086                rc ^= useInboundSession.hashCode();
087            }
088            if (serverUrl != null) {
089                rc ^= serverUrl.hashCode();
090            }
091            return rc;
092        }
093    
094        /**
095         * @see javax.resource.spi.ConnectionRequestInfo#equals(java.lang.Object)
096         */
097        public boolean equals(Object o) {
098            if (o == null) {
099                return false;
100            }
101            if (!getClass().equals(o.getClass())) {
102                return false;
103            }
104            ActiveMQConnectionRequestInfo i = (ActiveMQConnectionRequestInfo)o;
105            if (notEqual(serverUrl, i.serverUrl)) {
106                return false;
107            }
108            if (notEqual(useInboundSession, i.useInboundSession)) {
109                return false;
110            }
111            return true;
112        }
113    
114        /**
115         * @param i
116         * @return
117         */
118        private boolean notEqual(Object o1, Object o2) {
119            return (o1 == null ^ o2 == null) || (o1 != null && !o1.equals(o2));
120        }
121    
122        /**
123         * @return Returns the url.
124         */
125        public String getServerUrl() {
126            return serverUrl;
127        }
128    
129        /**
130         * @param url The url to set.
131         */
132        public void setServerUrl(String url) {
133            this.serverUrl = url;
134        }
135    
136        /**
137         * @return Returns the password.
138         */
139        public String getPassword() {
140            return password;
141        }
142    
143        /**
144         * @param password The password to set.
145         */
146        public void setPassword(String password) {
147            this.password = password;
148        }
149    
150        /**
151         * @return Returns the userid.
152         */
153        public String getUserName() {
154            return userName;
155        }
156    
157        /**
158         * @param userid The userid to set.
159         */
160        public void setUserName(String userid) {
161            this.userName = userid;
162        }
163    
164        /**
165         * @return Returns the clientid.
166         */
167        public String getClientid() {
168            return clientid;
169        }
170    
171        /**
172         * @param clientid The clientid to set.
173         */
174        public void setClientid(String clientid) {
175            this.clientid = clientid;
176        }
177    
178        @Override
179        public String toString() {
180            return new StringBuffer("ActiveMQConnectionRequestInfo{ userName = '").append(userName).append("' ")
181                    .append(", serverUrl = '").append(serverUrl).append("' ")
182                    .append(", clientid = '").append(clientid).append("' ")
183                    .append(", userName = '").append(userName).append("' ")
184                    .append(", useInboundSession = '").append(useInboundSession).append("'  }")
185                    .toString();
186        }
187    
188        public Boolean getUseInboundSession() {
189            return useInboundSession;
190        }
191    
192        public void setUseInboundSession(Boolean useInboundSession) {
193            this.useInboundSession = useInboundSession;
194        }
195    
196        public boolean isUseInboundSessionEnabled() {
197            return useInboundSession != null && useInboundSession.booleanValue();
198        }
199    
200        public Double getRedeliveryBackOffMultiplier() {
201            return Double.valueOf(redeliveryPolicy().getBackOffMultiplier());
202        }
203    
204        public Long getInitialRedeliveryDelay() {
205            return Long.valueOf(redeliveryPolicy().getInitialRedeliveryDelay());
206        }
207    
208        public Long getMaximumRedeliveryDelay() {
209            return Long.valueOf(redeliveryPolicy().getMaximumRedeliveryDelay());
210        }
211    
212        public Integer getMaximumRedeliveries() {
213            return Integer.valueOf(redeliveryPolicy().getMaximumRedeliveries());
214        }
215    
216        public Boolean getRedeliveryUseExponentialBackOff() {
217            return Boolean.valueOf(redeliveryPolicy().isUseExponentialBackOff());
218        }
219    
220        public void setRedeliveryBackOffMultiplier(Double value) {
221            if (value != null) {
222                redeliveryPolicy().setBackOffMultiplier(value);
223            }
224        }
225    
226        public void setInitialRedeliveryDelay(Long value) {
227            if (value != null) {
228                redeliveryPolicy().setInitialRedeliveryDelay(value.longValue());
229            }
230        }
231    
232        public void setMaximumRedeliveryDelay(Long value) {
233            if (value != null) {
234                redeliveryPolicy().setMaximumRedeliveryDelay(value.longValue());
235            }
236        }
237    
238        public void setMaximumRedeliveries(Integer value) {
239            if (value != null) {
240                redeliveryPolicy().setMaximumRedeliveries(value.intValue());
241            }
242        }
243    
244        public void setRedeliveryUseExponentialBackOff(Boolean value) {
245            if (value != null) {
246                redeliveryPolicy().setUseExponentialBackOff(value.booleanValue());
247            }
248        }
249    
250        public Integer getDurableTopicPrefetch() {
251            return Integer.valueOf(prefetchPolicy().getDurableTopicPrefetch());
252        }
253    
254        public Integer getOptimizeDurableTopicPrefetch() {
255            return Integer.valueOf(prefetchPolicy().getOptimizeDurableTopicPrefetch());
256        }
257    
258        public Integer getInputStreamPrefetch() {
259            return Integer.valueOf(prefetchPolicy().getInputStreamPrefetch());
260        }
261    
262        public Integer getQueueBrowserPrefetch() {
263            return Integer.valueOf(prefetchPolicy().getQueueBrowserPrefetch());
264        }
265    
266        public Integer getQueuePrefetch() {
267            return Integer.valueOf(prefetchPolicy().getQueuePrefetch());
268        }
269    
270        public Integer getTopicPrefetch() {
271            return Integer.valueOf(prefetchPolicy().getTopicPrefetch());
272        }
273    
274        public void setAllPrefetchValues(Integer i) {
275            if (i != null) {
276                prefetchPolicy().setAll(i.intValue());
277            }
278        }
279    
280        public void setDurableTopicPrefetch(Integer durableTopicPrefetch) {
281            if (durableTopicPrefetch != null) {
282                prefetchPolicy().setDurableTopicPrefetch(durableTopicPrefetch.intValue());
283            }
284        }
285    
286        public void setOptimizeDurableTopicPrefetch(Integer optimizeDurableTopicPrefetch) {
287            if (optimizeDurableTopicPrefetch != null) {
288                prefetchPolicy().setOptimizeDurableTopicPrefetch(optimizeDurableTopicPrefetch.intValue());
289            }
290        }
291    
292        public void setInputStreamPrefetch(Integer inputStreamPrefetch) {
293            if (inputStreamPrefetch != null) {
294                prefetchPolicy().setInputStreamPrefetch(inputStreamPrefetch.intValue());
295            }
296        }
297    
298        public void setQueueBrowserPrefetch(Integer queueBrowserPrefetch) {
299            if (queueBrowserPrefetch != null) {
300                prefetchPolicy().setQueueBrowserPrefetch(queueBrowserPrefetch.intValue());
301            }
302        }
303    
304        public void setQueuePrefetch(Integer queuePrefetch) {
305            if (queuePrefetch != null) {
306                prefetchPolicy().setQueuePrefetch(queuePrefetch.intValue());
307            }
308        }
309    
310        public void setTopicPrefetch(Integer topicPrefetch) {
311            if (topicPrefetch != null) {
312                prefetchPolicy().setTopicPrefetch(topicPrefetch.intValue());
313            }
314        }
315    
316        /**
317         * Returns the redelivery policy; not using bean properties to avoid
318         * breaking compatibility with JCA configuration in J2EE
319         */
320        public RedeliveryPolicy redeliveryPolicy() {
321            if (redeliveryPolicy == null) {
322                redeliveryPolicy = new RedeliveryPolicy();
323            }
324            return redeliveryPolicy;
325        }
326    
327        /**
328         * Returns the prefetch policy; not using bean properties to avoid breaking
329         * compatibility with JCA configuration in J2EE
330         */
331        public ActiveMQPrefetchPolicy prefetchPolicy() {
332            if (prefetchPolicy == null) {
333                prefetchPolicy = new ActiveMQPrefetchPolicy();
334            }
335            return prefetchPolicy;
336        }
337    }