KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > util > keystore > DefaultKeyStore


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/util/keystore/DefaultKeyStore.java,v 1.5 2004/02/13 02:21:38 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.util.keystore;
20
21 import java.io.InputStream JavaDoc;
22 import java.security.KeyStore JavaDoc;
23 import java.security.PrivateKey JavaDoc;
24 import java.security.cert.Certificate JavaDoc;
25 import java.security.cert.X509Certificate JavaDoc;
26 import java.util.Enumeration JavaDoc;
27
28 /**
29  * Use this Keystore to wrap the normal KeyStore implementation.
30  *
31  * @author <a HREF="bloritsch@apache.org">Berin Loritsch</a>
32  * @version CVS $Revision: 1.5 $ $Date: 2004/02/13 02:21:38 $
33  */

34 public class DefaultKeyStore extends JmeterKeyStore
35 {
36     private X509Certificate JavaDoc[] certChain;
37     private PrivateKey JavaDoc key;
38     private String JavaDoc alias;
39     private final KeyStore JavaDoc store;
40
41     public DefaultKeyStore(String JavaDoc type) throws Exception JavaDoc
42     {
43         this.store = KeyStore.getInstance(type);
44     }
45
46     public void load(InputStream JavaDoc is, String JavaDoc pword) throws Exception JavaDoc
47     {
48         store.load(is, pword.toCharArray());
49         PrivateKey JavaDoc key = null;
50         X509Certificate JavaDoc[] certChain = null;
51
52         Enumeration JavaDoc aliases = store.aliases();
53         while (aliases.hasMoreElements())
54         {
55             this.alias = (String JavaDoc) aliases.nextElement();
56             if (store.isKeyEntry(alias))
57             {
58                 key = (PrivateKey JavaDoc) store.getKey(alias, pword.toCharArray());
59                 Certificate JavaDoc[] chain = store.getCertificateChain(alias);
60                 certChain = new X509Certificate JavaDoc[chain.length];
61
62                 for (int i = 0; i < chain.length; i++)
63                 {
64                     certChain[i] = (X509Certificate JavaDoc) chain[i];
65                 }
66
67                 break;
68             }
69         }
70
71         if (null == key)
72         {
73             throw new Exception JavaDoc("No key found");
74         }
75         if (null == certChain)
76         {
77             throw new Exception JavaDoc("No certificate chain found");
78         }
79
80         this.key = key;
81         this.certChain = certChain;
82     }
83
84     public final X509Certificate JavaDoc[] getCertificateChain()
85     {
86         return this.certChain;
87     }
88
89     public final PrivateKey JavaDoc getPrivateKey()
90     {
91         return this.key;
92     }
93
94     public final String JavaDoc getAlias()
95     {
96         return this.alias;
97     }
98 }
99
Popular Tags