public SSLContext createContext(ContextAware context) throws NoSuchProviderException,
      NoSuchAlgorithmException, KeyManagementException,
      UnrecoverableKeyException, KeyStoreException, CertificateException {

    SSLContext sslContext = getProvider() != null ?
        SSLContext.getInstance(getProtocol(), getProvider())
        : SSLContext.getInstance(getProtocol());

    context.addInfo("SSL protocol '" + sslContext.getProtocol()
        + "' provider '" + sslContext.getProvider() + "'");

    KeyManager[] keyManagers = createKeyManagers(context);
    TrustManager[] trustManagers = createTrustManagers(context);
    SecureRandom secureRandom = createSecureRandom(context);
    sslContext.init(keyManagers, trustManagers, secureRandom);
    return sslContext;
  }
@Test
  public void testCreateContext() throws Exception {
    factoryBean.setKeyManagerFactory(keyManagerFactory);
    factoryBean.setKeyStore(keyStore);
    factoryBean.setTrustManagerFactory(trustManagerFactory);
    factoryBean.setTrustStore(trustStore);
    factoryBean.setSecureRandom(secureRandom);

    assertNotNull(factoryBean.createContext(context));

    assertTrue(keyManagerFactory.isFactoryCreated());
    assertTrue(trustManagerFactory.isFactoryCreated());
    assertTrue(keyStore.isKeyStoreCreated());
    assertTrue(trustStore.isKeyStoreCreated());
    assertTrue(secureRandom.isSecureRandomCreated());

    // it's important that each configured component output an appropriate
    // informational message to the context; i.e. this logging is not just
    // for programmers, it's there for systems administrators to use in
    // verifying that SSL is configured properly

    assertTrue(context.hasInfoMatching(SSL_CONFIGURATION_MESSAGE_PATTERN));
    assertTrue(context.hasInfoMatching(KEY_MANAGER_FACTORY_MESSAGE_PATTERN));
    assertTrue(context.hasInfoMatching(TRUST_MANAGER_FACTORY_MESSAGE_PATTERN));
    assertTrue(context.hasInfoMatching(KEY_STORE_MESSAGE_PATTERN));
    assertTrue(context.hasInfoMatching(TRUST_STORE_MESSAGE_PATTERN));
    assertTrue(context.hasInfoMatching(SECURE_RANDOM_MESSAGE_PATTERN));
  }