am fa7f9bad: SSLSocketFactory: don\'t reload the default if class name is same

* commit 'fa7f9bad0ae25224a366828c538bd1f0c179b56b':
  SSLSocketFactory: don't reload the default if class name is same
diff --git a/luni/src/main/java/javax/net/ssl/SSLSocketFactory.java b/luni/src/main/java/javax/net/ssl/SSLSocketFactory.java
index 5c0f15d..b506fa62 100644
--- a/luni/src/main/java/javax/net/ssl/SSLSocketFactory.java
+++ b/luni/src/main/java/javax/net/ssl/SSLSocketFactory.java
@@ -33,8 +33,6 @@
     // The default SSL socket factory
     private static SocketFactory defaultSocketFactory;
 
-    private static String defaultName;
-
     private static int lastCacheVersion = -1;
 
     /**
@@ -45,28 +43,38 @@
      */
     public static synchronized SocketFactory getDefault() {
         int newCacheVersion = Services.getCacheVersion();
-        if (lastCacheVersion != newCacheVersion) {
-            defaultSocketFactory = null;
-            defaultName = null;
-            lastCacheVersion = newCacheVersion;
-        }
-        if (defaultSocketFactory != null) {
+        if (defaultSocketFactory != null && lastCacheVersion == newCacheVersion) {
             return defaultSocketFactory;
         }
-        if (defaultName == null) {
-            defaultName = Security.getProperty("ssl.SocketFactory.provider");
-            if (defaultName != null) {
-                ClassLoader cl = Thread.currentThread().getContextClassLoader();
-                if (cl == null) {
-                    cl = ClassLoader.getSystemClassLoader();
-                }
-                try {
-                    final Class<?> sfc = Class.forName(defaultName, true, cl);
-                    defaultSocketFactory = (SocketFactory) sfc.newInstance();
-                } catch (Exception e) {
-                    System.logE("Problem creating " + defaultName, e);
+        lastCacheVersion = newCacheVersion;
+
+        String newName = Security.getProperty("ssl.SocketFactory.provider");
+        if (newName != null) {
+            /* The cache could have been invalidated, but the provider name didn't change. This
+             * will be the most common state, so check for it early without resetting the default
+             * SocketFactory.
+             */
+            if (defaultSocketFactory != null) {
+                if (newName.equals(defaultSocketFactory.getClass().getName())) {
+                    return defaultSocketFactory;
+                } else {
+                    defaultSocketFactory = null;
                 }
             }
+
+            ClassLoader cl = Thread.currentThread().getContextClassLoader();
+            if (cl == null) {
+                cl = ClassLoader.getSystemClassLoader();
+            }
+            try {
+                final Class<?> sfc = Class.forName(newName, true, cl);
+                defaultSocketFactory = (SocketFactory) sfc.newInstance();
+            } catch (Exception e) {
+                System.logW("Could not create " + newName + " with ClassLoader "
+                        + cl.toString() + ": " + e.getMessage());
+            }
+        } else {
+            defaultSocketFactory = null;
         }
 
         if (defaultSocketFactory == null) {
@@ -80,10 +88,12 @@
                 defaultSocketFactory = context.getSocketFactory();
             }
         }
+
         if (defaultSocketFactory == null) {
             // Use internal implementation
             defaultSocketFactory = new DefaultSSLSocketFactory("No SSLSocketFactory installed");
         }
+
         return defaultSocketFactory;
     }
 
diff --git a/luni/src/test/java/libcore/javax/net/ssl/SSLSocketFactoryTest.java b/luni/src/test/java/libcore/javax/net/ssl/SSLSocketFactoryTest.java
index d59bbb2..acf69c0 100644
--- a/luni/src/test/java/libcore/javax/net/ssl/SSLSocketFactoryTest.java
+++ b/luni/src/test/java/libcore/javax/net/ssl/SSLSocketFactoryTest.java
@@ -205,6 +205,7 @@
             fail("Cannot find a way to clear out the SocketFactory provider");
         }
 
+        assertNull(Security.getProperty(SSL_PROPERTY));
         return origProvider;
     }