KeyStoreUtil { public static void updateWithSelfSignedServerCertificate(KeyStore pKeyStore) throws NoSuchProviderException, NoSuchAlgorithmException, IOException, InvalidKeyException, CertificateException, SignatureException, KeyStoreException { final Object x500Name; final Object[] certAttributes = { "Jolokia Agent " + Version.getAgentVersion(), "JVM", "jolokia.org", "Pegnitz", "Franconia", "DE" }; if (ClassUtil.checkForClass(X500_NAME_SUN)) { x500Name = ClassUtil.newInstance(X500_NAME_SUN, certAttributes); } else if (ClassUtil.checkForClass(X500_NAME_IBM)) { x500Name = ClassUtil.newInstance(X500_NAME_IBM, certAttributes); } else { throw new IllegalStateException("Neither Sun- nor IBM-style JVM found."); } Object keypair = createKeyPair(); PrivateKey privKey = getPrivateKey(keypair); X509Certificate[] chain = new X509Certificate[1]; chain[0] = getSelfCertificate(keypair, x500Name, new Date(), (long) 3650 * 24 * 60 * 60); pKeyStore.setKeyEntry("jolokia-agent", privKey, new char[0], chain); } private  KeyStoreUtil(); static void updateWithCaPem(KeyStore pTrustStore, File pCaCert); static void updateWithServerPems(KeyStore pKeyStore, File pServerCert, File pServerKey, String pKeyAlgo, char[] pPassword); static void updateWithSelfSignedServerCertificate(KeyStore pKeyStore);  }
@Test public void testSelfSignedCertificate() throws Exception { KeyStore keystore = createKeyStore(); KeyStoreUtil.updateWithSelfSignedServerCertificate(keystore); X509Certificate cert = (X509Certificate) keystore.getCertificate("jolokia-agent"); assertNotNull(cert); assertEquals(cert.getSubjectDN().getName(), "CN=Jolokia Agent " + Version.getAgentVersion() + ", OU=JVM, O=jolokia.org, L=Pegnitz, ST=Franconia, C=DE"); assertEquals(cert.getSubjectDN(), cert.getIssuerDN()); }