Add jkeytool application core.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 19 Jan 2009 20:57:49 +0000 (21:57 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 19 Jan 2009 20:57:49 +0000 (21:57 +0100)
src/net/pterodactylus/jkeytool/core/Core.java [new file with mode: 0644]

diff --git a/src/net/pterodactylus/jkeytool/core/Core.java b/src/net/pterodactylus/jkeytool/core/Core.java
new file mode 100644 (file)
index 0000000..44fc7bb
--- /dev/null
@@ -0,0 +1,133 @@
+/*
+ * jkeytool - Core.java -
+ * Copyright © 2009 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package net.pterodactylus.jkeytool.core;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.CertificateException;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * The jkeytool core.
+ *
+ * @author David Roden &lt;droden@gmail.com&gt;
+ */
+public class Core {
+
+       /** Support for {@link CoreListener}s. */
+       private CoreListenerSupport coreListenerSupport = new CoreListenerSupport(this);
+
+       /** All loaded keystores. */
+       private Set<KeyStore> keyStores = new HashSet<KeyStore>();
+
+       //
+       // EVENT MANAGEMENT
+       //
+
+       /**
+        * Adds a core listener to the list of registered listeners.
+        *
+        * @param coreListener
+        *            The listener to add
+        */
+       public void addCoreListener(CoreListener coreListener) {
+               coreListenerSupport.addListener(coreListener);
+       }
+
+       /**
+        * Removes a core listener from the list of registered listeners.
+        *
+        * @param coreListener
+        *            The listener to remove
+        */
+       public void removeCoreListener(CoreListener coreListener) {
+               coreListenerSupport.removeListener(coreListener);
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       //
+       // ACTIONS
+       //
+
+       /**
+        * Loads a keystore from the given file. This method will fire either a
+        * {@link CoreListener#keyStoreLoaded(File, KeyStore)} or a
+        * {@link CoreListener#keyStoreNotLoaded(File)} event, depending on whether
+        * the loading succeeded or failed.
+        *
+        * @param keyStoreFile
+        *            The file to load the keystore from
+        */
+       public void loadKeyStore(File keyStoreFile) {
+               KeyStore keyStore = loadKeyStore(keyStoreFile, "PKCS12");
+               if (keyStore == null) {
+                       keyStore = loadKeyStore(keyStoreFile, "JKS");
+               }
+               if (keyStore == null) {
+                       coreListenerSupport.fireKeyStoreNotLoaded(keyStoreFile);
+                       return;
+               }
+               coreListenerSupport.fireKeyStoreLoaded(keyStoreFile, keyStore);
+       }
+
+       //
+       // PRIVATE METHODS
+       //
+
+       /**
+        * Loads a keystore from the given file, trying to parse it as a keystore of
+        * the given type.
+        *
+        * @param keyStoreFile
+        *            The file to read the keystore from
+        * @param type
+        *            The type of the keystore
+        * @return The loaded keystore, or <code>null</code> if the keystore could
+        *         not be loaded
+        */
+       private KeyStore loadKeyStore(File keyStoreFile, String type) {
+               FileInputStream keyStoreFileInputStream = null;
+               try {
+                       KeyStore keyStore = KeyStore.getInstance(type);
+                       keyStoreFileInputStream = new FileInputStream(keyStoreFile);
+                       keyStore.load(keyStoreFileInputStream, null);
+                       keyStores.add(keyStore);
+                       return keyStore;
+               } catch (IOException ioe1) {
+                       /* swallow. */
+               } catch (KeyStoreException kse1) {
+                       /* swallow. */
+               } catch (NoSuchAlgorithmException nsae1) {
+                       /* swallow. */
+               } catch (CertificateException ce1) {
+                       /* swallow. */
+               }
+               return null;
+       }
+
+}