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.ActiveMQSslConnectionFactory;
026import org.apache.activemq.RedeliveryPolicy;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * Must override equals and hashCode (JCA spec 16.4)
032 */
033public class ActiveMQConnectionRequestInfo implements ConnectionRequestInfo, Serializable, Cloneable {
034
035    private static final long serialVersionUID = -5754338187296859149L;
036    protected Logger log = LoggerFactory.getLogger(getClass());
037
038    private String userName;
039    private String password;
040    private String serverUrl;
041    private String clientid;
042    private Boolean useInboundSession;
043    private RedeliveryPolicy redeliveryPolicy;
044    private ActiveMQPrefetchPolicy prefetchPolicy;
045    private Boolean useSessionArgs;
046    private String trustStore;
047    private String trustStorePassword;
048    private String keyStore;
049    private String keyStorePassword;
050    private String keyStoreKeyPassword;
051
052    public ActiveMQConnectionRequestInfo copy() {
053        try {
054            ActiveMQConnectionRequestInfo answer = (ActiveMQConnectionRequestInfo) clone();
055            if (redeliveryPolicy != null) {
056                answer.redeliveryPolicy = redeliveryPolicy.copy();
057            }
058            return answer;
059        } catch (CloneNotSupportedException e) {
060            throw new RuntimeException("Could not clone: " + e, e);
061        }
062    }
063
064    /**
065     * Returns true if this object will configure an ActiveMQConnectionFactory
066     * in any way
067     */
068    public boolean isConnectionFactoryConfigured() {
069        return serverUrl != null || clientid != null || redeliveryPolicy != null || prefetchPolicy != null;
070    }
071
072    /**
073     * Configures the given connection factory
074     */
075    public void configure(ActiveMQConnectionFactory factory, MessageActivationSpec activationSpec) {
076        if (serverUrl != null) {
077            factory.setBrokerURL(serverUrl);
078        }
079        if (clientid != null) {
080            factory.setClientID(clientid);
081        }
082        if (redeliveryPolicy != null) {
083            factory.setRedeliveryPolicy(redeliveryPolicy);
084        }
085        if (prefetchPolicy != null) {
086            factory.setPrefetchPolicy(prefetchPolicy);
087        }
088        if (factory instanceof ActiveMQSslConnectionFactory) {
089            String trustStore = defaultValue(activationSpec == null ? null : activationSpec.getTrustStore(), getTrustStore());
090            String trustStorePassword = defaultValue(activationSpec == null ? null : activationSpec.getTrustStorePassword(), getTrustStorePassword());
091            String keyStore = defaultValue(activationSpec == null ? null : activationSpec.getKeyStore(), getKeyStore());
092            String keyStorePassword = defaultValue(activationSpec == null ? null : activationSpec.getKeyStorePassword(), getKeyStorePassword());
093            String keyStoreKeyPassword = defaultValue(activationSpec == null ? null : activationSpec.getKeyStoreKeyPassword(), getKeyStoreKeyPassword());
094            ActiveMQSslConnectionFactory sslFactory = (ActiveMQSslConnectionFactory) factory;
095            if (trustStore != null) {
096                try {
097                    sslFactory.setTrustStore(trustStore);
098                } catch (Exception e) {
099                    log.warn("Unable to set TrustStore", e);
100                }
101            }
102            if (trustStorePassword != null) {
103                sslFactory.setTrustStorePassword(trustStorePassword);
104            }
105            if (keyStore != null) {
106                try {
107                    sslFactory.setKeyStore(keyStore);
108                } catch (Exception e) {
109                    log.warn("Unable to set KeyStore", e);
110                }
111            }
112            if (keyStorePassword != null) {
113                sslFactory.setKeyStorePassword(keyStorePassword);
114            }
115            if (keyStoreKeyPassword != null) {
116                sslFactory.setKeyStoreKeyPassword(keyStoreKeyPassword);
117            }
118        }
119    }
120
121    /**
122     * @see javax.resource.spi.ConnectionRequestInfo#hashCode()
123     */
124    @Override
125    public int hashCode() {
126        int rc = 0;
127        if (useInboundSession != null) {
128            rc ^= useInboundSession.hashCode();
129        }
130        if (useSessionArgs != null) {
131            rc ^= useSessionArgs.hashCode();
132        }
133        if (serverUrl != null) {
134            rc ^= serverUrl.hashCode();
135        }
136        return rc;
137    }
138
139    /**
140     * @see javax.resource.spi.ConnectionRequestInfo#equals(java.lang.Object)
141     */
142    @Override
143    public boolean equals(Object o) {
144        if (o == null) {
145            return false;
146        }
147        if (!getClass().equals(o.getClass())) {
148            return false;
149        }
150        ActiveMQConnectionRequestInfo i = (ActiveMQConnectionRequestInfo) o;
151        if (notEqual(serverUrl, i.serverUrl)) {
152            return false;
153        }
154        if (notEqual(useInboundSession, i.useInboundSession)) {
155            return false;
156        }
157        if (notEqual(useSessionArgs, i.useSessionArgs)) {
158            return false;
159        }
160        return true;
161    }
162
163    /**
164     * @param i
165     * @return
166     */
167    private boolean notEqual(Object o1, Object o2) {
168        return (o1 == null ^ o2 == null) || (o1 != null && !o1.equals(o2));
169    }
170
171    /**
172     * @return Returns the url.
173     */
174    public String getServerUrl() {
175        return serverUrl;
176    }
177
178    /**
179     * @param url
180     *        The url to set.
181     */
182    public void setServerUrl(String url) {
183        this.serverUrl = url;
184    }
185
186    /**
187     * @return Returns the password.
188     */
189    public String getPassword() {
190        return password;
191    }
192
193    /**
194     * @param password
195     *        The password to set.
196     */
197    public void setPassword(String password) {
198        this.password = password;
199    }
200
201    /**
202     * @return Returns the userid.
203     */
204    public String getUserName() {
205        return userName;
206    }
207
208    /**
209     * @param userid
210     *        The userid to set.
211     */
212    public void setUserName(String userid) {
213        this.userName = userid;
214    }
215
216    /**
217     * @return Returns the clientid.
218     */
219    public String getClientid() {
220        return clientid;
221    }
222
223    /**
224     * @param clientid
225     *        The clientid to set.
226     */
227    public void setClientid(String clientid) {
228        this.clientid = clientid;
229    }
230
231    public String getTrustStore() {
232        return trustStore;
233    }
234
235    public void setTrustStore(String trustStore) {
236        this.trustStore = trustStore;
237    }
238
239    public String getTrustStorePassword() {
240        return trustStorePassword;
241    }
242
243    public void setTrustStorePassword(String trustStorePassword) {
244        this.trustStorePassword = trustStorePassword;
245    }
246
247    public String getKeyStore() {
248        return keyStore;
249    }
250
251    public void setKeyStore(String keyStore) {
252        this.keyStore = keyStore;
253    }
254
255    public String getKeyStorePassword() {
256        return keyStorePassword;
257    }
258
259    public void setKeyStorePassword(String keyStorePassword) {
260        this.keyStorePassword = keyStorePassword;
261    }
262
263    public String getKeyStoreKeyPassword() {
264        return keyStoreKeyPassword;
265    }
266
267    public void setKeyStoreKeyPassword(String keyStoreKeyPassword) {
268        this.keyStoreKeyPassword = keyStoreKeyPassword;
269    }
270
271    @Override
272    public String toString() {
273        return new StringBuffer("ActiveMQConnectionRequestInfo{ userName = '").append(userName).append("' ").append(", serverUrl = '").append(serverUrl)
274            .append("' ").append(", clientid = '").append(clientid).append("' ").append(", userName = '").append(userName).append("' ")
275            .append(", useSessionArgs = '").append(useSessionArgs).append("' ").append(", useInboundSession = '").append(useInboundSession).append("'  }")
276            .toString();
277    }
278
279    public Boolean getUseInboundSession() {
280        return useInboundSession;
281    }
282
283    public void setUseInboundSession(Boolean useInboundSession) {
284        this.useInboundSession = useInboundSession;
285    }
286
287    public boolean isUseInboundSessionEnabled() {
288        return useInboundSession != null && useInboundSession.booleanValue();
289    }
290
291    public Double getRedeliveryBackOffMultiplier() {
292        return Double.valueOf(redeliveryPolicy().getBackOffMultiplier());
293    }
294
295    public Long getInitialRedeliveryDelay() {
296        return Long.valueOf(redeliveryPolicy().getInitialRedeliveryDelay());
297    }
298
299    public Long getMaximumRedeliveryDelay() {
300        return Long.valueOf(redeliveryPolicy().getMaximumRedeliveryDelay());
301    }
302
303    public Integer getMaximumRedeliveries() {
304        return Integer.valueOf(redeliveryPolicy().getMaximumRedeliveries());
305    }
306
307    public Boolean getRedeliveryUseExponentialBackOff() {
308        return Boolean.valueOf(redeliveryPolicy().isUseExponentialBackOff());
309    }
310
311    public void setRedeliveryBackOffMultiplier(Double value) {
312        if (value != null) {
313            redeliveryPolicy().setBackOffMultiplier(value);
314        }
315    }
316
317    public void setInitialRedeliveryDelay(Long value) {
318        if (value != null) {
319            redeliveryPolicy().setInitialRedeliveryDelay(value.longValue());
320        }
321    }
322
323    public void setMaximumRedeliveryDelay(Long value) {
324        if (value != null) {
325            redeliveryPolicy().setMaximumRedeliveryDelay(value.longValue());
326        }
327    }
328
329    public void setMaximumRedeliveries(Integer value) {
330        if (value != null) {
331            redeliveryPolicy().setMaximumRedeliveries(value.intValue());
332        }
333    }
334
335    public void setRedeliveryUseExponentialBackOff(Boolean value) {
336        if (value != null) {
337            redeliveryPolicy().setUseExponentialBackOff(value.booleanValue());
338        }
339    }
340
341    public Integer getDurableTopicPrefetch() {
342        return Integer.valueOf(prefetchPolicy().getDurableTopicPrefetch());
343    }
344
345    public Integer getOptimizeDurableTopicPrefetch() {
346        return Integer.valueOf(prefetchPolicy().getOptimizeDurableTopicPrefetch());
347    }
348
349    @Deprecated
350    public Integer getInputStreamPrefetch() {
351        return 0;
352    }
353
354    public Integer getQueueBrowserPrefetch() {
355        return Integer.valueOf(prefetchPolicy().getQueueBrowserPrefetch());
356    }
357
358    public Integer getQueuePrefetch() {
359        return Integer.valueOf(prefetchPolicy().getQueuePrefetch());
360    }
361
362    public Integer getTopicPrefetch() {
363        return Integer.valueOf(prefetchPolicy().getTopicPrefetch());
364    }
365
366    public void setAllPrefetchValues(Integer i) {
367        if (i != null) {
368            prefetchPolicy().setAll(i.intValue());
369        }
370    }
371
372    public void setDurableTopicPrefetch(Integer durableTopicPrefetch) {
373        if (durableTopicPrefetch != null) {
374            prefetchPolicy().setDurableTopicPrefetch(durableTopicPrefetch.intValue());
375        }
376    }
377
378    public void setOptimizeDurableTopicPrefetch(Integer optimizeDurableTopicPrefetch) {
379        if (optimizeDurableTopicPrefetch != null) {
380            prefetchPolicy().setOptimizeDurableTopicPrefetch(optimizeDurableTopicPrefetch.intValue());
381        }
382    }
383
384    public void setQueueBrowserPrefetch(Integer queueBrowserPrefetch) {
385        if (queueBrowserPrefetch != null) {
386            prefetchPolicy().setQueueBrowserPrefetch(queueBrowserPrefetch.intValue());
387        }
388    }
389
390    public void setQueuePrefetch(Integer queuePrefetch) {
391        if (queuePrefetch != null) {
392            prefetchPolicy().setQueuePrefetch(queuePrefetch.intValue());
393        }
394    }
395
396    public void setTopicPrefetch(Integer topicPrefetch) {
397        if (topicPrefetch != null) {
398            prefetchPolicy().setTopicPrefetch(topicPrefetch.intValue());
399        }
400    }
401
402    /**
403     * Returns the redelivery policy; not using bean properties to avoid
404     * breaking compatibility with JCA configuration in J2EE
405     */
406    public RedeliveryPolicy redeliveryPolicy() {
407        if (redeliveryPolicy == null) {
408            redeliveryPolicy = new RedeliveryPolicy();
409        }
410        return redeliveryPolicy;
411    }
412
413    /**
414     * Returns the prefetch policy; not using bean properties to avoid breaking
415     * compatibility with JCA configuration in J2EE
416     */
417    public ActiveMQPrefetchPolicy prefetchPolicy() {
418        if (prefetchPolicy == null) {
419            prefetchPolicy = new ActiveMQPrefetchPolicy();
420        }
421        return prefetchPolicy;
422    }
423
424    public boolean isUseSessionArgs() {
425        return useSessionArgs != null ? useSessionArgs.booleanValue() : false;
426    }
427
428    public Boolean getUseSessionArgs() {
429        return useSessionArgs;
430    }
431
432    public void setUseSessionArgs(Boolean useSessionArgs) {
433        this.useSessionArgs = useSessionArgs;
434    }
435
436    protected String defaultValue(String value, String defaultValue) {
437        if (value != null) {
438            return value;
439        }
440        return defaultValue;
441    }
442}