Initialize new key store before firing event.
[jkeytool.git] / src / net / pterodactylus / jkeytool / core / Core.java
1 /*
2  * jkeytool - Core.java -
3  * Copyright © 2009 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.jkeytool.core;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.security.KeyStore;
26 import java.security.KeyStoreException;
27 import java.security.NoSuchAlgorithmException;
28 import java.security.cert.CertificateException;
29 import java.util.HashSet;
30 import java.util.Set;
31
32 /**
33  * The jkeytool core.
34  *
35  * @author David Roden <droden@gmail.com>
36  */
37 public class Core {
38
39         /** Support for {@link CoreListener}s. */
40         private CoreListenerSupport coreListenerSupport = new CoreListenerSupport(this);
41
42         /** All loaded keystores. */
43         private Set<KeyStore> keyStores = new HashSet<KeyStore>();
44
45         //
46         // EVENT MANAGEMENT
47         //
48
49         /**
50          * Adds a core listener to the list of registered listeners.
51          *
52          * @param coreListener
53          *            The listener to add
54          */
55         public void addCoreListener(CoreListener coreListener) {
56                 coreListenerSupport.addListener(coreListener);
57         }
58
59         /**
60          * Removes a core listener from the list of registered listeners.
61          *
62          * @param coreListener
63          *            The listener to remove
64          */
65         public void removeCoreListener(CoreListener coreListener) {
66                 coreListenerSupport.removeListener(coreListener);
67         }
68
69         //
70         // ACCESSORS
71         //
72
73         //
74         // ACTIONS
75         //
76
77         /**
78          * Creates a new key store of the given type.
79          *
80          * @param keyStoreType
81          *            The type of the key store
82          */
83         public void createKeyStore(String keyStoreType) {
84                 try {
85                         KeyStore keyStore = KeyStore.getInstance(keyStoreType);
86                         keyStore.load(null, null);
87                         coreListenerSupport.fireKeyStoreCreated(keyStore);
88                 } catch (KeyStoreException kse1) {
89                         coreListenerSupport.fireKeyStoreNotCreated(keyStoreType, kse1);
90                 } catch (NoSuchAlgorithmException nsae1) {
91                         coreListenerSupport.fireKeyStoreNotCreated(keyStoreType, nsae1);
92                 } catch (CertificateException ce1) {
93                         coreListenerSupport.fireKeyStoreNotCreated(keyStoreType, ce1);
94                 } catch (IOException ioe1) {
95                         coreListenerSupport.fireKeyStoreNotCreated(keyStoreType, ioe1);
96                 }
97         }
98
99         /**
100          * Loads a keystore from the given file. This method will fire either a
101          * {@link CoreListener#keyStoreLoaded(File, KeyStore)} or a
102          * {@link CoreListener#keyStoreNotLoaded(File)} event, depending on whether
103          * the loading succeeded or failed.
104          *
105          * @param keyStoreFile
106          *            The file to load the keystore from
107          */
108         public void loadKeyStore(File keyStoreFile) {
109                 KeyStore keyStore = loadKeyStore(keyStoreFile, "PKCS12");
110                 if (keyStore == null) {
111                         keyStore = loadKeyStore(keyStoreFile, "JKS");
112                 }
113                 if (keyStore == null) {
114                         coreListenerSupport.fireKeyStoreNotLoaded(keyStoreFile);
115                         return;
116                 }
117                 coreListenerSupport.fireKeyStoreLoaded(keyStoreFile, keyStore);
118         }
119
120         //
121         // PRIVATE METHODS
122         //
123
124         /**
125          * Loads a keystore from the given file, trying to parse it as a keystore of
126          * the given type.
127          *
128          * @param keyStoreFile
129          *            The file to read the keystore from
130          * @param type
131          *            The type of the keystore
132          * @return The loaded keystore, or <code>null</code> if the keystore could
133          *         not be loaded
134          */
135         private KeyStore loadKeyStore(File keyStoreFile, String type) {
136                 FileInputStream keyStoreFileInputStream = null;
137                 try {
138                         KeyStore keyStore = KeyStore.getInstance(type);
139                         keyStoreFileInputStream = new FileInputStream(keyStoreFile);
140                         keyStore.load(keyStoreFileInputStream, null);
141                         keyStores.add(keyStore);
142                         return keyStore;
143                 } catch (IOException ioe1) {
144                         /* swallow. */
145                 } catch (KeyStoreException kse1) {
146                         /* swallow. */
147                 } catch (NoSuchAlgorithmException nsae1) {
148                         /* swallow. */
149                 } catch (CertificateException ce1) {
150                         /* swallow. */
151                 }
152                 return null;
153         }
154
155 }