KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > security > ssl > sun_jsse > KeyStoreUtil


1 package org.jacorb.security.ssl.sun_jsse;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 2000-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23
24 import java.security.*;
25 import java.security.cert.*;
26 import java.io.*;
27
28 /**
29  * A class with utility methods that help managing a key store.
30  *
31  * @author Gerald Brose
32  * @version $Id: KeyStoreUtil.java,v 1.10 2004/11/24 15:45:24 nicolas Exp $
33  */

34
35 public class KeyStoreUtil
36 {
37     /**
38      * @return - a fully loaded and operational KeyStore
39      * @param file_name - a keystore file name to be loaded
40      * @param storepass - the password for managing the keystore
41      */

42
43     public static KeyStore getKeyStore( String JavaDoc file_name,
44                                         char[] storepass )
45         throws IOException, java.security.GeneralSecurityException JavaDoc
46     {
47         InputStream in = null;
48
49         java.net.URL JavaDoc url =
50             Thread.currentThread().getContextClassLoader().getResource(file_name);
51         if (url != null)
52         {
53             in = url.openStream();
54         }
55         else
56         {
57             //try unchanged name first
58
File f = new File( file_name );
59             if( ! f.exists() )
60             {
61                 //try to prepend home dir
62
String JavaDoc name =
63                     System.getProperty( "user.home" ) +
64                     System.getProperty( "file.separator" ) +
65                     file_name;
66                 
67                 f = new File( name );
68                 
69                 if(f.exists())
70                 {
71                     in = new FileInputStream( f );
72                 }
73             }
74             else
75             {
76                 in = new FileInputStream( f );
77             }
78         }
79
80         if (in == null)
81         {
82                 throw new IOException("Unable to find keystore file " +
83                                       file_name);
84         }
85
86         KeyStore ks = KeyStore.getInstance( "JKS" );
87         ks.load( in, storepass );
88         in.close();
89         return ks;
90     }
91 }
92
Popular Tags