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("' ")
268                .append(", serverUrl = '").append(serverUrl).append("' ")
269                .append(", clientid = '").append(clientid).append("' ")
270                .append(", userName = '").append(userName).append("' ")
271                .append(", useSessionArgs = '").append(useSessionArgs).append("' ")
272                .append(", useInboundSession = '").append(useInboundSession).append("'  }")
273                .toString();
274    }
275
276    public Boolean getUseInboundSession() {
277        return useInboundSession;
278    }
279
280    public void setUseInboundSession(Boolean useInboundSession) {
281        this.useInboundSession = useInboundSession;
282    }
283
284    public boolean isUseInboundSessionEnabled() {
285        return useInboundSession != null && useInboundSession.booleanValue();
286    }
287
288    public Double getRedeliveryBackOffMultiplier() {
289        return Double.valueOf(redeliveryPolicy().getBackOffMultiplier());
290    }
291
292    public Long getInitialRedeliveryDelay() {
293        return Long.valueOf(redeliveryPolicy().getInitialRedeliveryDelay());
294    }
295
296    public Long getMaximumRedeliveryDelay() {
297        return Long.valueOf(redeliveryPolicy().getMaximumRedeliveryDelay());
298    }
299
300    public Integer getMaximumRedeliveries() {
301        return Integer.valueOf(redeliveryPolicy().getMaximumRedeliveries());
302    }
303
304    public Boolean getRedeliveryUseExponentialBackOff() {
305        return Boolean.valueOf(redeliveryPolicy().isUseExponentialBackOff());
306    }
307
308    public void setRedeliveryBackOffMultiplier(Double value) {
309        if (value != null) {
310            redeliveryPolicy().setBackOffMultiplier(value);
311        }
312    }
313
314    public void setInitialRedeliveryDelay(Long value) {
315        if (value != null) {
316            redeliveryPolicy().setInitialRedeliveryDelay(value.longValue());
317        }
318    }
319
320    public void setMaximumRedeliveryDelay(Long value) {
321        if (value != null) {
322            redeliveryPolicy().setMaximumRedeliveryDelay(value.longValue());
323        }
324    }
325
326    public void setMaximumRedeliveries(Integer value) {
327        if (value != null) {
328            redeliveryPolicy().setMaximumRedeliveries(value.intValue());
329        }
330    }
331
332    public void setRedeliveryUseExponentialBackOff(Boolean value) {
333        if (value != null) {
334            redeliveryPolicy().setUseExponentialBackOff(value.booleanValue());
335        }
336    }
337
338    public Integer getDurableTopicPrefetch() {
339        return Integer.valueOf(prefetchPolicy().getDurableTopicPrefetch());
340    }
341
342    public Integer getOptimizeDurableTopicPrefetch() {
343        return Integer.valueOf(prefetchPolicy().getOptimizeDurableTopicPrefetch());
344    }
345
346    public Integer getInputStreamPrefetch() {
347        return Integer.valueOf(prefetchPolicy().getInputStreamPrefetch());
348    }
349
350    public Integer getQueueBrowserPrefetch() {
351        return Integer.valueOf(prefetchPolicy().getQueueBrowserPrefetch());
352    }
353
354    public Integer getQueuePrefetch() {
355        return Integer.valueOf(prefetchPolicy().getQueuePrefetch());
356    }
357
358    public Integer getTopicPrefetch() {
359        return Integer.valueOf(prefetchPolicy().getTopicPrefetch());
360    }
361
362    public void setAllPrefetchValues(Integer i) {
363        if (i != null) {
364            prefetchPolicy().setAll(i.intValue());
365        }
366    }
367
368    public void setDurableTopicPrefetch(Integer durableTopicPrefetch) {
369        if (durableTopicPrefetch != null) {
370            prefetchPolicy().setDurableTopicPrefetch(durableTopicPrefetch.intValue());
371        }
372    }
373
374    public void setOptimizeDurableTopicPrefetch(Integer optimizeDurableTopicPrefetch) {
375        if (optimizeDurableTopicPrefetch != null) {
376            prefetchPolicy().setOptimizeDurableTopicPrefetch(optimizeDurableTopicPrefetch.intValue());
377        }
378    }
379
380    public void setInputStreamPrefetch(Integer inputStreamPrefetch) {
381        if (inputStreamPrefetch != null) {
382            prefetchPolicy().setInputStreamPrefetch(inputStreamPrefetch.intValue());
383        }
384    }
385
386    public void setQueueBrowserPrefetch(Integer queueBrowserPrefetch) {
387        if (queueBrowserPrefetch != null) {
388            prefetchPolicy().setQueueBrowserPrefetch(queueBrowserPrefetch.intValue());
389        }
390    }
391
392    public void setQueuePrefetch(Integer queuePrefetch) {
393        if (queuePrefetch != null) {
394            prefetchPolicy().setQueuePrefetch(queuePrefetch.intValue());
395        }
396    }
397
398    public void setTopicPrefetch(Integer topicPrefetch) {
399        if (topicPrefetch != null) {
400            prefetchPolicy().setTopicPrefetch(topicPrefetch.intValue());
401        }
402    }
403
404    /**
405     * Returns the redelivery policy; not using bean properties to avoid
406     * breaking compatibility with JCA configuration in J2EE
407     */
408    public RedeliveryPolicy redeliveryPolicy() {
409        if (redeliveryPolicy == null) {
410            redeliveryPolicy = new RedeliveryPolicy();
411        }
412        return redeliveryPolicy;
413    }
414
415    /**
416     * Returns the prefetch policy; not using bean properties to avoid breaking
417     * compatibility with JCA configuration in J2EE
418     */
419    public ActiveMQPrefetchPolicy prefetchPolicy() {
420        if (prefetchPolicy == null) {
421            prefetchPolicy = new ActiveMQPrefetchPolicy();
422        }
423        return prefetchPolicy;
424    }
425
426    public boolean isUseSessionArgs() {
427        return useSessionArgs != null ? useSessionArgs.booleanValue() : false;
428    }
429
430    public Boolean getUseSessionArgs() {
431        return useSessionArgs;
432    }
433
434    public void setUseSessionArgs(Boolean useSessionArgs) {
435        this.useSessionArgs = useSessionArgs;
436    }
437
438    protected String defaultValue(String value, String defaultValue) {
439        if (value != null) {
440            return value;
441        }
442        return defaultValue;
443    }
444}