KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > extras > pgp > PGPKeyRingImpl


1 /*
2  * $Id: PGPKeyRingImpl.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.extras.pgp;
12
13 import java.io.InputStream JavaDoc;
14 import java.security.Principal JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.mule.config.i18n.Messages;
22 import org.mule.umo.lifecycle.InitialisationException;
23 import org.mule.util.IOUtils;
24
25 import cryptix.pki.ExtendedKeyStore;
26 import cryptix.pki.KeyBundle;
27
28 /**
29  * @author ariva
30  */

31 public class PGPKeyRingImpl implements PGPKeyRing
32 {
33     protected static Log logger = LogFactory.getLog(PGPKeyRingImpl.class);
34
35     private String JavaDoc publicKeyRingFileName;
36
37     private HashMap JavaDoc principalsKeyBundleMap;
38
39     private String JavaDoc secretKeyRingFileName;
40
41     private String JavaDoc secretAliasId;
42
43     private KeyBundle secretKeyBundle;
44
45     private String JavaDoc secretPassphrase;
46
47     public PGPKeyRingImpl()
48     {
49         super();
50     }
51
52     public String JavaDoc getSecretKeyRingFileName()
53     {
54         return secretKeyRingFileName;
55     }
56
57     public void setSecretKeyRingFileName(String JavaDoc value)
58     {
59         this.secretKeyRingFileName = value;
60     }
61
62     public String JavaDoc getSecretAliasId()
63     {
64         return secretAliasId;
65     }
66
67     public void setSecretAliasId(String JavaDoc value)
68     {
69         this.secretAliasId = value;
70     }
71
72     public String JavaDoc getSecretPassphrase()
73     {
74         return secretPassphrase;
75     }
76
77     public void setSecretPassphrase(String JavaDoc value)
78     {
79         this.secretPassphrase = value;
80     }
81
82     private void readPrivateKeyBundle() throws Exception JavaDoc
83     {
84         InputStream JavaDoc in = IOUtils.getResourceAsStream(secretKeyRingFileName, getClass());
85
86         ExtendedKeyStore ring = (ExtendedKeyStore)ExtendedKeyStore.getInstance("OpenPGP/KeyRing");
87         ring.load(in, null);
88
89         in.close();
90
91         secretKeyBundle = ring.getKeyBundle(secretAliasId);
92     }
93
94     public KeyBundle getSecretKeyBundle()
95     {
96         return secretKeyBundle;
97     }
98
99     /**
100      * @return
101      */

102     public String JavaDoc getPublicKeyRingFileName()
103     {
104         return publicKeyRingFileName;
105     }
106
107     /**
108      * @param value
109      */

110     public void setPublicKeyRingFileName(String JavaDoc value)
111     {
112         this.publicKeyRingFileName = value;
113     }
114
115     public KeyBundle getKeyBundle(String JavaDoc principalId)
116     {
117         return (KeyBundle)principalsKeyBundleMap.get(principalId);
118     }
119
120     public void initialise() throws InitialisationException
121     {
122         try
123         {
124             java.security.Security.addProvider(new cryptix.jce.provider.CryptixCrypto());
125             java.security.Security.addProvider(new cryptix.openpgp.provider.CryptixOpenPGP());
126
127             principalsKeyBundleMap = new HashMap JavaDoc();
128
129             readPublicKeyRing();
130             readPrivateKeyBundle();
131         }
132         catch (Exception JavaDoc e)
133         {
134             logger.error("errore in inizializzazione:" + e.getMessage(), e);
135             throw new InitialisationException(new org.mule.config.i18n.Message(Messages.FAILED_TO_CREATE_X,
136                 "PGPKeyRingImpl"), e);
137         }
138     }
139
140     private void readPublicKeyRing() throws Exception JavaDoc
141     {
142         logger.debug(System.getProperties().get("user.dir"));
143         InputStream JavaDoc in = IOUtils.getResourceAsStream(publicKeyRingFileName, getClass());
144
145         ExtendedKeyStore ring = (ExtendedKeyStore)ExtendedKeyStore.getInstance("OpenPGP/KeyRing");
146         ring.load(in, null);
147         in.close();
148
149         for (Enumeration JavaDoc e = ring.aliases(); e.hasMoreElements();)
150         {
151             String JavaDoc aliasId = (String JavaDoc)e.nextElement();
152
153             KeyBundle bundle = ring.getKeyBundle(aliasId);
154
155             if (bundle != null)
156             {
157                 for (Iterator JavaDoc users = bundle.getPrincipals(); users.hasNext();)
158                 {
159                     Principal JavaDoc princ = (Principal JavaDoc)users.next();
160
161                     principalsKeyBundleMap.put(princ.getName(), bundle);
162                 }
163             }
164         }
165     }
166 }
167
Popular Tags