KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xmlrpc > secure > SecurityTool


1 /*
2  * Copyright 1999,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.xmlrpc.secure;
19
20 import java.security.KeyStore JavaDoc;
21 import java.security.Provider JavaDoc;
22 import java.security.Security JavaDoc;
23
24 /**
25  */

26 public class SecurityTool
27     implements SecurityConstants
28 {
29     /**
30      * Class name of the security provider to be
31      * used by the secure web server.
32      */

33     protected static String JavaDoc securityProviderClass;
34     
35     /**
36      * The security protocol to be used by the
37      * secure web server. Currently the options are
38      * SSL and TLS.
39      */

40     private static String JavaDoc securityProtocol;
41
42     /**
43      * Password used to access the key store.
44      */

45     private static String JavaDoc keyStorePassword;
46
47     /**
48      * Format to be used for the key store. With
49      * the Sun JSSE the standard "JKS" format is
50      * available along with the "PKCS12" format.
51      */

52     private static String JavaDoc keyStoreType;
53     
54     /**
55      * Path to the key store that will be used by
56      * the secure web server.
57      */

58     private static String JavaDoc keyStore;
59
60     /**
61      * Password used to access the key store.
62      */

63     private static String JavaDoc trustStorePassword;
64
65     /**
66      * Format to be used for the key store. With
67      * the Sun JSSE the standard "JKS" format is
68      * available along with the "PKCS12" format.
69      */

70     private static String JavaDoc trustStoreType;
71     
72     /**
73      * Path to the key store that will be used by
74      * the secure web server.
75      */

76     private static String JavaDoc trustStore;
77
78     /**
79      * The type of key manager to be used by the
80      * secure web server. With the Sun JSSE only
81      * type available is the X509 type which
82      * is implemented in the SunX509 classes.
83      */

84     private static String JavaDoc keyManagerType;
85
86     /**
87      * The protocol handler package to use for
88      * the secure web server. This allows the URL
89      * class to handle https streams.
90      */

91     private static String JavaDoc protocolHandlerPackages;
92
93     public static void setup()
94         throws Exception JavaDoc
95     {
96         /*
97          * We want to dynamically register the SunJSSE provider
98          * because we don't want people to have to modify their
99          * JVM setups manually.
100          */

101         Security.addProvider((Provider JavaDoc)Class.forName(
102             SecurityTool.getSecurityProviderClass()).newInstance());
103         
104         /*
105          * Set the packages that will provide the URL stream
106          * handlers that can cope with TLS/SSL.
107          */

108         System.setProperty(PROTOCOL_HANDLER_PACKAGES,
109             SecurityTool.getProtocolHandlerPackages());
110
111         // Setup KeyStore
112

113         System.setProperty(KEY_STORE_TYPE,
114             SecurityTool.getKeyStoreType());
115         
116         System.setProperty(KEY_STORE,
117             SecurityTool.getKeyStore());
118         
119         System.setProperty(KEY_STORE_PASSWORD,
120             SecurityTool.getKeyStorePassword());
121
122         // Setup TrustStore
123

124         System.setProperty(TRUST_STORE_TYPE,
125             SecurityTool.getTrustStoreType());
126         
127         System.setProperty(TRUST_STORE,
128             SecurityTool.getTrustStore());
129         
130         System.setProperty(TRUST_STORE_PASSWORD,
131             SecurityTool.getTrustStorePassword());
132     }
133
134     /**
135      * Set the protocol handler packages.
136      *
137      * @param String protocol handler package.
138      */

139     public static void setProtocolHandlerPackages(String JavaDoc x)
140     {
141         protocolHandlerPackages = x;
142     }
143
144     /**
145      * Get the protocol handler packages.
146      *
147      * @param String protocol handler package.
148      */

149     public static String JavaDoc getProtocolHandlerPackages()
150     {
151         if (System.getProperty(PROTOCOL_HANDLER_PACKAGES) != null)
152         {
153             return System.getProperty(PROTOCOL_HANDLER_PACKAGES);
154         }
155         if (protocolHandlerPackages == null)
156         {
157             return DEFAULT_PROTOCOL_HANDLER_PACKAGES;
158         }
159         else
160         {
161             return protocolHandlerPackages;
162         }
163     }
164
165     /**
166      * Set the security provider class.
167      *
168      * @param String name of security provider class.
169      */

170     public static void setSecurityProviderClass(String JavaDoc x)
171     {
172         securityProviderClass = x;
173     }
174
175     /**
176      * Get the security provider class.
177      *
178      * @return String name of security provider class.
179      */

180     public static String JavaDoc getSecurityProviderClass()
181     {
182         if (System.getProperty(SECURITY_PROVIDER_CLASS) != null)
183         {
184             return System.getProperty(SECURITY_PROVIDER_CLASS);
185         }
186         if (securityProviderClass == null)
187         {
188             return DEFAULT_SECURITY_PROVIDER_CLASS;
189         }
190         else
191         {
192             return securityProviderClass;
193         }
194     }
195
196     /**
197      * Set the key store password.
198      *
199      * @param String key store password.
200      */

201     public static void setKeyStorePassword(String JavaDoc x)
202     {
203         keyStorePassword = x;
204     }
205
206     /**
207      * Set the security protocol.
208      *
209      * @param String security protocol.
210      */

211     public static void setSecurityProtocol(String JavaDoc x)
212     {
213         securityProtocol = x;
214     }
215
216     /**
217      * Get the security protocol.
218      *
219      * @return String security protocol.
220      */

221     public static String JavaDoc getSecurityProtocol()
222     {
223         if (System.getProperty(SECURITY_PROTOCOL) != null)
224         {
225             return System.getProperty(SECURITY_PROTOCOL);
226         }
227         if (securityProtocol== null)
228         {
229             return DEFAULT_SECURITY_PROTOCOL;
230         }
231         else
232         {
233             return securityProtocol;
234         }
235     }
236
237     /**
238      * Set the key store location.
239      *
240      * @param String key store location.
241      */

242     public static void setKeyStore(String JavaDoc x)
243     {
244         keyStore = x;
245     }
246
247     /**
248      * Get the key store location.
249      *
250      * @return String key store location.
251      */

252     public static String JavaDoc getKeyStore()
253     {
254         if (System.getProperty(KEY_STORE) != null)
255         {
256             return System.getProperty(KEY_STORE);
257         }
258         if (keyStore == null)
259         {
260             return DEFAULT_KEY_STORE;
261         }
262         else
263         {
264             return keyStore;
265         }
266     }
267
268     /**
269      * Set the key store format.
270      *
271      * @param String key store format.
272      */

273     public static void setKeyStoreType(String JavaDoc x)
274     {
275         keyStoreType = x;
276     }
277
278     /**
279      * Get the key store format.
280      *
281      * @return String key store format.
282      */

283     public static String JavaDoc getKeyStoreType()
284     {
285         if (System.getProperty(KEY_STORE_TYPE) != null)
286         {
287             return System.getProperty(KEY_STORE_TYPE);
288         }
289         if (keyStoreType == null)
290         {
291             /*
292              * If the keystore type hasn't been specified
293              * then let the system determine the default
294              * type.
295              */

296             return KeyStore.getDefaultType();
297         }
298         else
299         {
300             return keyStoreType;
301         }
302     }
303
304     /**
305      * Get the key store password.
306      *
307      * @return String key store password.
308      */

309     public static String JavaDoc getKeyStorePassword()
310     {
311         if (System.getProperty(KEY_STORE_PASSWORD) != null)
312         {
313             return System.getProperty(KEY_STORE_PASSWORD);
314         }
315         if (keyStorePassword == null)
316         {
317             return DEFAULT_KEY_STORE_PASSWORD;
318         }
319         else
320         {
321             return keyStorePassword;
322         }
323     }
324
325     /**
326      * Set the key store location.
327      *
328      * @param String key store location.
329      */

330     public static void setTrustStore(String JavaDoc x)
331     {
332         trustStore = x;
333     }
334
335     /**
336      * Get the key store location.
337      *
338      * @return String key store location.
339      */

340     public static String JavaDoc getTrustStore()
341     {
342         if (System.getProperty(TRUST_STORE) != null)
343         {
344             return System.getProperty(TRUST_STORE);
345         }
346         if (trustStore == null)
347         {
348             return DEFAULT_TRUST_STORE;
349         }
350         else
351         {
352             return trustStore;
353         }
354     }
355
356     /**
357      * Set the key store format.
358      *
359      * @param String key store format.
360      */

361     public static void setTrustStoreType(String JavaDoc x)
362     {
363         trustStoreType = x;
364     }
365
366     /**
367      * Get the key store format.
368      *
369      * @return String key store format.
370      */

371     public static String JavaDoc getTrustStoreType()
372     {
373         if (System.getProperty(TRUST_STORE_TYPE) != null)
374         {
375             return System.getProperty(TRUST_STORE_TYPE);
376         }
377         if (trustStoreType == null)
378         {
379             /*
380              * If the keystore type hasn't been specified
381              * then let the system determine the default
382              * type.
383              */

384             return KeyStore.getDefaultType();
385         }
386         else
387         {
388             return trustStoreType;
389         }
390     }
391
392     /**
393      * Set the trust store password.
394      *
395      * @param String trust store password.
396      */

397     public static void setTrustStorePassword(String JavaDoc x)
398     {
399         trustStorePassword = x;
400     }
401
402     /**
403      * Get the trust store password.
404      *
405      * @return String trust store password.
406      */

407     public static String JavaDoc getTrustStorePassword()
408     {
409         if (System.getProperty(TRUST_STORE_PASSWORD) != null)
410         {
411             return System.getProperty(TRUST_STORE_PASSWORD);
412         }
413         if (trustStorePassword == null)
414         {
415             return DEFAULT_TRUST_STORE_PASSWORD;
416         }
417         else
418         {
419             return trustStorePassword;
420         }
421     }
422
423     /**
424      * Set the key manager type.
425      *
426      * @param String key manager type.
427      */

428     public static void setKeyManagerType(String JavaDoc x)
429     {
430         keyManagerType = x;
431     }
432
433     /**
434      * Get the key manager type.
435      *
436      * @return String key manager type.
437      */

438     public static String JavaDoc getKeyManagerType()
439     {
440         if (System.getProperty(KEY_MANAGER_TYPE) != null)
441         {
442             return System.getProperty(KEY_MANAGER_TYPE);
443         }
444         if (keyManagerType == null)
445         {
446             return DEFAULT_KEY_MANAGER_TYPE;
447         }
448         else
449         {
450             return keyManagerType;
451         }
452     }
453 }
454
Popular Tags