package org.bouncycastle.jsse.provider;

import java.security.KeyManagementException;
import java.security.SecureRandom;

import javax.net.ssl.KeyManager;
import javax.net.ssl.TrustManager;

import org.bouncycastle.tls.crypto.impl.jcajce.JcaTlsCryptoProvider;

class DefaultSSLContextSpi extends ProvSSLContextSpi
{
    private static class LazyInstance
    {
        private static final Exception initException;
        private static final DefaultSSLContextSpi instance;

        static
        {
            Exception ex = LazyManagers.initException;
            DefaultSSLContextSpi i = null;

            if (null == ex)
            {
                try
                {
                    i = new DefaultSSLContextSpi(false, new JcaTlsCryptoProvider()); 
                }
                catch (Exception e)
                {
                    ex = e;
                    i = null;
                }
            }

            initException = ex;
            instance = i;
        }
    }

    private static class LazyManagers
    {
        private static final Exception initException;
        private static final KeyManager[] keyManagers;
        private static final TrustManager[] trustManagers;

        static
        {
            Exception ex = null;
            KeyManager[] kms;
            TrustManager[] tms;

            try
            {
                kms = ProvSSLContextSpi.getDefaultKeyManagers();
                tms = ProvSSLContextSpi.getDefaultTrustManagers();
            }
            catch (Exception e)
            {
                ex = e;
                kms = null;
                tms = null;
            }

            initException = ex;
            keyManagers = kms;
            trustManagers = tms;
        }
    }

    static ProvSSLContextSpi getDefaultInstance() throws Exception
    {
        if (null != LazyInstance.initException)
        {
            throw LazyInstance.initException;
        }

        return LazyInstance.instance;
    }

    DefaultSSLContextSpi(boolean isInFipsMode, JcaTlsCryptoProvider cryptoProvider) throws KeyManagementException
    {
        super(isInFipsMode, cryptoProvider, null);

        if (null != LazyManagers.initException)
        {
            throw new KeyManagementException("Default key/trust managers unavailable", LazyManagers.initException);
        }

        super.engineInit(LazyManagers.keyManagers, LazyManagers.trustManagers, null);
    }

    @Override
    protected void engineInit(KeyManager[] kms, TrustManager[] tms, SecureRandom sr)
        throws KeyManagementException
    {
        throw new KeyManagementException("Default SSLContext is initialized automatically");
    }
}

@@ -113,16 +113,81 @@
 eRandom;
+%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;
 %0A%0Aimport
@@ -374,16 +374,320 @@
 xtSpi%0A%7B%0A
+    private static final Logger LOG = Logger.getLogger(DefaultSSLContextSpi.class.getName());%0A%0A    private static Exception avoidCapturingException(Exception e)%0A    %7B%0A        // NOTE: Avoid capturing arbitrary exception into static field%0A        return new KeyManagementException(e.getMessage());%0A    %7D%0A%0A
     priv
@@ -1211,38 +1211,94 @@
                 
-ex =
+LOG.log(Level.WARNING, %22Failed to load default SSLContext%22,
  e
+)
 ;%0A              
@@ -1303,24 +1303,47 @@
         
-i = null
+ex = avoidCapturingException(e)
 ;%0A      
@@ -1739,24 +1739,31 @@
 anager%5B%5D kms
+ = null
 ;%0A          
@@ -1782,16 +1782,23 @@
 er%5B%5D tms
+ = null
 ;%0A%0A     
@@ -1834,25 +1834,25 @@
             
-k
+t
 ms = ProvSSL
@@ -1872,19 +1872,21 @@
 tDefault
-Key
+Trust
 Managers
@@ -1886,32 +1886,41 @@
 anagers();%0A     
+       %7D%0A
            tms =
@@ -1906,33 +1906,291 @@
    %7D%0A           
-t
+ catch (Exception e)%0A            %7B%0A                LOG.log(Level.WARNING, %22Failed to load default trust managers%22, e);%0A                ex = e;%0A            %7D%0A%0A            if (null == ex)%0A            %7B%0A                try%0A                %7B%0A                    k
 ms = ProvSSLCont
@@ -2198,37 +2198,35 @@
 xtSpi.getDefault
-Trust
+Key
 Managers();%0A    
@@ -2225,34 +2225,40 @@
 ();%0A            
-%7D%0A
+    %7D%0A  
             catc
@@ -2245,32 +2245,34 @@
 %7D%0A              
+  
 catch (Exception
@@ -2291,32 +2291,251 @@
     
-%7B%0A                ex = e
+    %7B%0A                    LOG.log(Level.WARNING, %22Failed to load default key managers%22, e);%0A                    ex = e;%0A                %7D%0A            %7D%0A%0A            if (null != ex)%0A            %7B%0A                ex = avoidCapturingException(ex)
 ;%0A  

Add logging to default SSLContext init