private X509TrustManager getTrustManager ( String algorithm ) { try { TrustManagerFactory factory = TrustManagerFactory . getInstance ( algorithm ) ; factory . init ( ( KeyStore ) null ) ; X509TrustManager ret = null ; for ( TrustManager tm : factory . getTrustManagers ( ) ) { // Multiple TrustManager may be attached. We just need X509 Trust // Manager here. if ( tm instanceof X509TrustManager ) { ret = ( X509TrustManager ) tm ; break ; } } if ( ret == null ) { return null ; } synchronized ( ROOT_CA_LOCK ) { // cache root CA certificates for later use. if ( ROOT_CA . size ( ) == 0 ) { for ( X509Certificate cert : ret . getAcceptedIssuers ( ) ) { Certificate bcCert = Certificate . getInstance ( cert . getEncoded ( ) ) ; ROOT_CA . put ( bcCert . getSubject ( ) . hashCode ( ) , bcCert ) ; } } } return ret ; } catch ( NoSuchAlgorithmException | KeyStoreException | CertificateEncodingException ex ) { throw new SSLInitializationException ( ex . getMessage ( ) , ex ) ; } }
Get TrustManager for the algorithm . This is mainly used to get the JVM default trust manager and cache all of the root CA .