public static KeyStore buildKeyStore() { if (keystore == null) { File keyStoreFile = new File(KEY_STORE_FILENAME); if (keyStoreFile.exists()) { loadKeyStore(keyStoreFile); } else { dynamicallyCreateKeyStore(); saveKeyStore(); } } return keystore; }
private static void saveKeyStore() { try { ByteArrayOutputStream bout = new ByteArrayOutputStream(); keystore.store(bout, KEY_STORE_PASSWORD.toCharArray()); File keyStoreFile = new File(KEY_STORE_FILENAME); logger.trace("Saving key store to file [" + keyStoreFile + "]"); FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(keyStoreFile); fileOutputStream.write(bout.toByteArray()); } finally { if (fileOutputStream != null) { fileOutputStream.close(); } } keyStoreFile.deleteOnExit(); } catch (Exception e) { throw new RuntimeException("Exception while saving KeyStore", e); } }
private static void loadKeyStore(File keyStoreFile) { try { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(KEY_STORE_FILENAME); logger.trace("Loading key store from file [" + keyStoreFile + "]"); keystore = KeyStore.getInstance(KeyStore.getDefaultType()); keystore.load(fileInputStream, KEY_STORE_PASSWORD.toCharArray()); } finally { if (fileInputStream != null) { fileInputStream.close(); } } } catch (Exception e) { throw new RuntimeException( "Exception while loading KeyStore from " + keyStoreFile.getAbsolutePath(), e); } }