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 java.io.Serializable;
020
021import javax.resource.spi.ConnectionRequestInfo;
022
023import org.apache.activemq.ActiveMQConnectionFactory;
024import org.apache.activemq.ActiveMQPrefetchPolicy;
025import org.apache.activemq.RedeliveryPolicy;
026
027/**
028 *  Must override equals and hashCode (JCA spec 16.4)
029 */
030public 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 getInputStreamPrefetch() {
255        return Integer.valueOf(prefetchPolicy().getInputStreamPrefetch());
256    }
257
258    public Integer getQueueBrowserPrefetch() {
259        return Integer.valueOf(prefetchPolicy().getQueueBrowserPrefetch());
260    }
261
262    public Integer getQueuePrefetch() {
263        return Integer.valueOf(prefetchPolicy().getQueuePrefetch());
264    }
265
266    public Integer getTopicPrefetch() {
267        return Integer.valueOf(prefetchPolicy().getTopicPrefetch());
268    }
269
270    public void setAllPrefetchValues(Integer i) {
271        if (i != null) {
272            prefetchPolicy().setAll(i.intValue());
273        }
274    }
275
276    public void setDurableTopicPrefetch(Integer durableTopicPrefetch) {
277        if (durableTopicPrefetch != null) {
278            prefetchPolicy().setDurableTopicPrefetch(durableTopicPrefetch.intValue());
279        }
280    }
281
282    public void setInputStreamPrefetch(Integer inputStreamPrefetch) {
283        if (inputStreamPrefetch != null) {
284            prefetchPolicy().setInputStreamPrefetch(inputStreamPrefetch.intValue());
285        }
286    }
287
288    public void setQueueBrowserPrefetch(Integer queueBrowserPrefetch) {
289        if (queueBrowserPrefetch != null) {
290            prefetchPolicy().setQueueBrowserPrefetch(queueBrowserPrefetch.intValue());
291        }
292    }
293
294    public void setQueuePrefetch(Integer queuePrefetch) {
295        if (queuePrefetch != null) {
296            prefetchPolicy().setQueuePrefetch(queuePrefetch.intValue());
297        }
298    }
299
300    public void setTopicPrefetch(Integer topicPrefetch) {
301        if (topicPrefetch != null) {
302            prefetchPolicy().setTopicPrefetch(topicPrefetch.intValue());
303        }
304    }
305
306    /**
307     * Returns the redelivery policy; not using bean properties to avoid
308     * breaking compatibility with JCA configuration in J2EE
309     */
310    public RedeliveryPolicy redeliveryPolicy() {
311        if (redeliveryPolicy == null) {
312            redeliveryPolicy = new RedeliveryPolicy();
313        }
314        return redeliveryPolicy;
315    }
316
317    /**
318     * Returns the prefetch policy; not using bean properties to avoid breaking
319     * compatibility with JCA configuration in J2EE
320     */
321    public ActiveMQPrefetchPolicy prefetchPolicy() {
322        if (prefetchPolicy == null) {
323            prefetchPolicy = new ActiveMQPrefetchPolicy();
324        }
325        return prefetchPolicy;
326    }
327}