View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: SSLProperties.java,v 1.4 2005/05/03 13:45:59 tanderson Exp $
44   */
45  package org.exolab.jms.net.util;
46  
47  import org.exolab.jms.net.connector.ResourceException;
48  
49  
50  /***
51   * Helper class to hold the SSL properties (used by TPCS & HTTPS connectors).
52   *
53   * @author <a href="mailto:daniel.otero@mac.com">Daniel Otero</a>
54   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
55   * @version $Revision: 1.4 $
56   */
57  public final class SSLProperties {
58  
59      /***
60       * The keystore to use. If <code>null</code>, indicates to use the default
61       * keystore.
62       */
63      private String _keyStore;
64  
65      /***
66       * The keystore password. If <code>null</code>, indicates to use the default
67       * password.
68       */
69      private String _keyStorePassword;
70  
71      /***
72       * The keystore type. If <code>null</code>, indicates to use the default
73       * type.
74       */
75      private String _keyStoreType;
76  
77      /***
78       * The truststore to use. If <code>null</code>, indicates to use the default
79       * truststore.
80       */
81      private String _trustStore;
82  
83      /***
84       * The truststore password. If <code>null</code>, indicates to use the
85       * default password.
86       */
87      private String _trustStorePassword;
88  
89      /***
90       * The truststore type. If <code>null</code>, indicates to use the default
91       * type.
92       */
93      private String _trustStoreType;
94  
95      /***
96       * Connection property to indicate the keystore to use.
97       */
98      private static final String KEY_STORE = "keyStore";
99  
100     /***
101      * Connection property to indicate the keystore password.
102      */
103     private static final String KEY_STORE_PASSWORD = "keyStorePassword";
104 
105     /***
106      * Connection property to indicate the keystore type.
107      */
108     private static final String KEY_STORE_TYPE = "keyStoreType";
109 
110     /***
111      * Connection property to indicate the truststore to use.
112      */
113     private static final String TRUST_STORE = "trustStore";
114 
115     /***
116      * Connection property to indicate the truststore password.
117      */
118     private static final String TRUST_STORE_PASSWORD = "trustStorePassword";
119 
120     /***
121      * Connection property to indicate the truststore type.
122      */
123     private static final String TRUST_STORE_TYPE = "trustStoreType";
124 
125     /***
126      * Construct a new <code>SSLProperties</code>.
127      */
128     public SSLProperties() {
129     }
130 
131     /***
132      * Construct a new <code>SSLProperties</code>.
133      *
134      * @param properties the properties to populate this from
135      * @throws ResourceException if any of the properties are invalid
136      */
137     public SSLProperties(Properties properties) throws ResourceException {
138         setKeyStore(properties.get(KEY_STORE));
139         setKeyStorePassword(properties.get(KEY_STORE_PASSWORD));
140         setKeyStoreType(properties.get(KEY_STORE_TYPE));
141         setTrustStore(properties.get(TRUST_STORE));
142         setTrustStorePassword(properties.get(TRUST_STORE_PASSWORD));
143         setTrustStoreType(properties.get(TRUST_STORE_TYPE));
144     }
145 
146     /***
147      * Returns the keystore.
148      *
149      * @return the keystore, or <code>null</code> if unset
150      */
151     public String getKeyStore() {
152         return _keyStore;
153     }
154 
155     /***
156      * Sets the keystore..
157      *
158      * @param store the keystore
159      */
160     public void setKeyStore(String store) {
161         _keyStore = store;
162     }
163 
164     /***
165      * Returns the keystore password.
166      *
167      * @return the keystore password, or <code>null</code> if unset
168      */
169     public String getKeyStorePassword() {
170         return _keyStorePassword;
171     }
172 
173     /***
174      * Sets the keystore password.
175      *
176      * @param password the keystore password
177      */
178     public void setKeyStorePassword(String password) {
179         _keyStorePassword = password;
180     }
181 
182     /***
183      * Returns the keystore type.
184      *
185      * @return the keystore type, or <code>null</code> if unset
186      */
187     public String getKeyStoreType() {
188         return _keyStoreType;
189     }
190 
191     /***
192      * Sets the keystore type.
193      *
194      * @param type the the keystore type
195      */
196     public void setKeyStoreType(String type) {
197         _keyStoreType = type;
198     }
199 
200     /***
201      * Returns the truststore.
202      *
203      * @return the truststore, or <code>null</code> if unset
204      */
205     public String getTrustStore() {
206         return _trustStore;
207     }
208 
209     /***
210      * Sets the truststore.
211      *
212      * @param store the truststore
213      */
214     public void setTrustStore(String store) {
215         _trustStore = store;
216     }
217 
218     /***
219      * Returns the truststore password.
220      *
221      * @return the truststore passowrd, or <code>null</code> if unset
222      */
223     public String getTrustStorePassword() {
224         return _trustStorePassword;
225     }
226 
227     /***
228      * Sets the truststore password.
229      *
230      * @param password the truststore password
231      */
232     public void setTrustStorePassword(String password) {
233         _trustStorePassword = password;
234     }
235 
236     /***
237      * Returns the truststore type.
238      *
239      * @return the truststore type, or <code>null</code> if unset
240      */
241     public String getTrustStoreType() {
242         return _trustStoreType;
243     }
244 
245     /***
246      * Sets the truststore type.
247      *
248      * @param type the the trusstore type
249      */
250     public void setTrustStoreType(String type) {
251         _trustStoreType = type;
252     }
253 
254     /***
255      * Test to see if this instance has been populated.
256      *
257      * @return <code>true</code> if this hasn't been populated
258      */
259     public boolean isEmpty() {
260         return _keyStore == null && _keyStorePassword == null
261                 && _keyStoreType == null && _trustStore == null
262                 && _trustStorePassword == null && _trustStoreType == null;
263     }
264 
265     /***
266      * Checks whether this instance is equal to another.
267      *
268      * @param other the object to compare
269      * @return <code>true</code> if the two instances are equal; otherwise
270      *         <code>false</code>
271      */
272     public boolean equals(Object other) {
273         boolean equal = (this == other);
274         if (!equal) {
275             if (other instanceof SSLProperties) {
276                 SSLProperties props = (SSLProperties) other;
277                 if (equals(_keyStore, props._keyStore)
278                         && equals(_keyStorePassword, props._keyStorePassword)
279                         && equals(_keyStoreType, props._keyStoreType)
280                         && equals(_trustStore, props._trustStore)
281                         && equals(_trustStorePassword,
282                                   props._trustStorePassword)
283                         && equals(_trustStoreType, props._trustStoreType)) {
284                     equal = true;
285                 }
286             } else {
287                 equal = false;
288             }
289         }
290         return equal;
291     }
292 
293     /***
294      * Helper to export this to a {@link Properties} instance.
295      *
296      * @param properties the properties to export to.
297      */
298     public void export(Properties properties) {
299         properties.setNonNull(KEY_STORE, getKeyStore());
300         properties.setNonNull(KEY_STORE_PASSWORD, getKeyStorePassword());
301         properties.setNonNull(KEY_STORE_TYPE, getKeyStoreType());
302         properties.setNonNull(TRUST_STORE, getTrustStore());
303         properties.setNonNull(TRUST_STORE_PASSWORD, getTrustStorePassword());
304         properties.setNonNull(TRUST_STORE_TYPE, getTrustStoreType());
305     }
306 
307     /***
308      * Helper to compare two objects for equality.
309      *
310      * @param o1 the first object to compare
311      * @param o2 the second object to compare
312      * @return <code>true</code> if the objects are equal, otherwise
313      *         <code>false</code>
314      */
315     private boolean equals(Object o1, Object o2) {
316         boolean equal = (o1 == null && o2 == null);
317         if (!equal) {
318             if (o1 != null && o1.equals(o2)) {
319                 equal = true;
320             }
321         }
322         return equal;
323     }
324 
325 }