From 5e5c5075b96aed1080984814de0e7c6ed1ffa637 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 19 Jan 2009 21:57:49 +0100 Subject: [PATCH] Add jkeytool application core. --- src/net/pterodactylus/jkeytool/core/Core.java | 133 ++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 src/net/pterodactylus/jkeytool/core/Core.java diff --git a/src/net/pterodactylus/jkeytool/core/Core.java b/src/net/pterodactylus/jkeytool/core/Core.java new file mode 100644 index 0000000..44fc7bb --- /dev/null +++ b/src/net/pterodactylus/jkeytool/core/Core.java @@ -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 <droden@gmail.com> + */ +public class Core { + + /** Support for {@link CoreListener}s. */ + private CoreListenerSupport coreListenerSupport = new CoreListenerSupport(this); + + /** All loaded keystores. */ + private Set keyStores = new HashSet(); + + // + // 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 null 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; + } + +} -- 2.7.4