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    public int hashCode() {
125        int rc = 0;
126        if (useInboundSession != null) {
127            rc ^= useInboundSession.hashCode();
128        }
129        if (useSessionArgs != null) {
130            rc ^= useSessionArgs.hashCode();
131        }
132        if (serverUrl != null) {
133            rc ^= serverUrl.hashCode();
134        }
135        return rc;
136    }
137
138    /**
139     * @see javax.resource.spi.ConnectionRequestInfo#equals(java.lang.Object)
140     */
141    public boolean equals(Object o) {
142        if (o == null) {
143            return false;
144        }
145        if (!getClass().equals(o.getClass())) {
146            return false;
147        }
148        ActiveMQConnectionRequestInfo i = (ActiveMQConnectionRequestInfo)o;
149        if (notEqual(serverUrl, i.serverUrl)) {
150            return false;
151        }
152        if (notEqual(useInboundSession, i.useInboundSession)) {
153            return false;
154        }
155        if (notEqual(useSessionArgs, i.useSessionArgs)) {
156            return false;
157        }
158        return true;
159    }
160
161    /**
162     * @param i
163     * @return
164     */
165    private boolean notEqual(Object o1, Object o2) {
166        return (o1 == null ^ o2 == null) || (o1 != null && !o1.equals(o2));
167    }
168
169    /**
170     * @return Returns the url.
171     */
172    public String getServerUrl() {
173        return serverUrl;
174    }
175
176    /**
177     * @param url The url to set.
178     */
179    public void setServerUrl(String url) {
180        this.serverUrl = url;
181    }
182
183    /**
184     * @return Returns the password.
185     */
186    public String getPassword() {
187        return password;
188    }
189
190    /**
191     * @param password The password to set.
192     */
193    public void setPassword(String password) {
194        this.password = password;
195    }
196
197    /**
198     * @return Returns the userid.
199     */
200    public String getUserName() {
201        return userName;
202    }
203
204    /**
205     * @param userid The userid to set.
206     */
207    public void setUserName(String userid) {
208        this.userName = userid;
209    }
210
211    /**
212     * @return Returns the clientid.
213     */
214    public String getClientid() {
215        return clientid;
216    }
217
218    /**
219     * @param clientid The clientid to set.
220     */
221    public void setClientid(String clientid) {
222        this.clientid = clientid;
223    }
224
225    public String getTrustStore() {
226        return trustStore;
227    }
228
229    public void setTrustStore(String trustStore) {
230        this.trustStore = trustStore;
231    }
232
233    public String getTrustStorePassword() {
234        return trustStorePassword;
235    }
236
237    public void setTrustStorePassword(String trustStorePassword) {
238        this.trustStorePassword = trustStorePassword;
239    }
240
241    public String getKeyStore() {
242        return keyStore;
243    }
244
245    public void setKeyStore(String keyStore) {
246        this.keyStore = keyStore;
247    }
248
249    public String getKeyStorePassword() {
250        return keyStorePassword;
251    }
252
253    public void setKeyStorePassword(String keyStorePassword) {
254        this.keyStorePassword = keyStorePassword;
255    }
256
257    public String getKeyStoreKeyPassword() {
258        return keyStoreKeyPassword;
259    }
260
261    public void setKeyStoreKeyPassword(String keyStoreKeyPassword) {
262        this.keyStoreKeyPassword = keyStoreKeyPassword;
263    }
264
265    @Override
266    public String toString() {
267        return new StringBuffer("ActiveMQConnectionRequestInfo{ userName = '").append(userName).append("' ").append(", serverUrl = '").append(serverUrl)
268            .append("' ").append(", clientid = '").append(clientid).append("' ")
269            .append(", useSessionArgs = '").append(useSessionArgs).append("' ").append(", useInboundSession = '").append(useInboundSession).append("'  }")
270            .toString();
271    }
272
273    public Boolean getUseInboundSession() {
274        return useInboundSession;
275    }
276
277    public void setUseInboundSession(Boolean useInboundSession) {
278        this.useInboundSession = useInboundSession;
279    }
280
281    public boolean isUseInboundSessionEnabled() {
282        return useInboundSession != null && useInboundSession.booleanValue();
283    }
284
285    public Double getRedeliveryBackOffMultiplier() {
286        return Double.valueOf(redeliveryPolicy().getBackOffMultiplier());
287    }
288
289    public Long getInitialRedeliveryDelay() {
290        return Long.valueOf(redeliveryPolicy().getInitialRedeliveryDelay());
291    }
292
293    public Long getMaximumRedeliveryDelay() {
294        return Long.valueOf(redeliveryPolicy().getMaximumRedeliveryDelay());
295    }
296
297    public Integer getMaximumRedeliveries() {
298        return Integer.valueOf(redeliveryPolicy().getMaximumRedeliveries());
299    }
300
301    public Boolean getRedeliveryUseExponentialBackOff() {
302        return Boolean.valueOf(redeliveryPolicy().isUseExponentialBackOff());
303    }
304
305    public void setRedeliveryBackOffMultiplier(Double value) {
306        if (value != null) {
307            redeliveryPolicy().setBackOffMultiplier(value);
308        }
309    }
310
311    public void setInitialRedeliveryDelay(Long value) {
312        if (value != null) {
313            redeliveryPolicy().setInitialRedeliveryDelay(value.longValue());
314        }
315    }
316
317    public void setMaximumRedeliveryDelay(Long value) {
318        if (value != null) {
319            redeliveryPolicy().setMaximumRedeliveryDelay(value.longValue());
320        }
321    }
322
323    public void setMaximumRedeliveries(Integer value) {
324        if (value != null) {
325            redeliveryPolicy().setMaximumRedeliveries(value.intValue());
326        }
327    }
328
329    public void setRedeliveryUseExponentialBackOff(Boolean value) {
330        if (value != null) {
331            redeliveryPolicy().setUseExponentialBackOff(value.booleanValue());
332        }
333    }
334
335    public Integer getDurableTopicPrefetch() {
336        return Integer.valueOf(prefetchPolicy().getDurableTopicPrefetch());
337    }
338
339    public Integer getOptimizeDurableTopicPrefetch() {
340        return Integer.valueOf(prefetchPolicy().getOptimizeDurableTopicPrefetch());
341    }
342
343    public Integer getInputStreamPrefetch() {
344        return Integer.valueOf(prefetchPolicy().getInputStreamPrefetch());
345    }
346
347    public Integer getQueueBrowserPrefetch() {
348        return Integer.valueOf(prefetchPolicy().getQueueBrowserPrefetch());
349    }
350
351    public Integer getQueuePrefetch() {
352        return Integer.valueOf(prefetchPolicy().getQueuePrefetch());
353    }
354
355    public Integer getTopicPrefetch() {
356        return Integer.valueOf(prefetchPolicy().getTopicPrefetch());
357    }
358
359    public void setAllPrefetchValues(Integer i) {
360        if (i != null) {
361            prefetchPolicy().setAll(i.intValue());
362        }
363    }
364
365    public void setDurableTopicPrefetch(Integer durableTopicPrefetch) {
366        if (durableTopicPrefetch != null) {
367            prefetchPolicy().setDurableTopicPrefetch(durableTopicPrefetch.intValue());
368        }
369    }
370
371    public void setOptimizeDurableTopicPrefetch(Integer optimizeDurableTopicPrefetch) {
372        if (optimizeDurableTopicPrefetch != null) {
373            prefetchPolicy().setOptimizeDurableTopicPrefetch(optimizeDurableTopicPrefetch.intValue());
374        }
375    }
376
377    public void setInputStreamPrefetch(Integer inputStreamPrefetch) {
378        if (inputStreamPrefetch != null) {
379            prefetchPolicy().setInputStreamPrefetch(inputStreamPrefetch.intValue());
380        }
381    }
382
383    public void setQueueBrowserPrefetch(Integer queueBrowserPrefetch) {
384        if (queueBrowserPrefetch != null) {
385            prefetchPolicy().setQueueBrowserPrefetch(queueBrowserPrefetch.intValue());
386        }
387    }
388
389    public void setQueuePrefetch(Integer queuePrefetch) {
390        if (queuePrefetch != null) {
391            prefetchPolicy().setQueuePrefetch(queuePrefetch.intValue());
392        }
393    }
394
395    public void setTopicPrefetch(Integer topicPrefetch) {
396        if (topicPrefetch != null) {
397            prefetchPolicy().setTopicPrefetch(topicPrefetch.intValue());
398        }
399    }
400
401    /**
402     * Returns the redelivery policy; not using bean properties to avoid
403     * breaking compatibility with JCA configuration in J2EE
404     */
405    public RedeliveryPolicy redeliveryPolicy() {
406        if (redeliveryPolicy == null) {
407            redeliveryPolicy = new RedeliveryPolicy();
408        }
409        return redeliveryPolicy;
410    }
411
412    /**
413     * Returns the prefetch policy; not using bean properties to avoid breaking
414     * compatibility with JCA configuration in J2EE
415     */
416    public ActiveMQPrefetchPolicy prefetchPolicy() {
417        if (prefetchPolicy == null) {
418            prefetchPolicy = new ActiveMQPrefetchPolicy();
419        }
420        return prefetchPolicy;
421    }
422
423    public boolean isUseSessionArgs() {
424        return useSessionArgs != null ? useSessionArgs.booleanValue() : false;
425    }
426
427    public Boolean getUseSessionArgs() {
428        return useSessionArgs;
429    }
430
431    public void setUseSessionArgs(Boolean useSessionArgs) {
432        this.useSessionArgs = useSessionArgs;
433    }
434
435    protected String defaultValue(String value, String defaultValue) {
436        if (value != null) {
437            return value;
438        }
439        return defaultValue;
440    }
441}