diff options
-rw-r--r-- | luni/src/main/java/javax/net/ssl/SSLSocketFactory.java | 50 | ||||
-rw-r--r-- | luni/src/test/java/libcore/javax/net/ssl/SSLSocketFactoryTest.java | 1 |
2 files changed, 31 insertions, 20 deletions
diff --git a/luni/src/main/java/javax/net/ssl/SSLSocketFactory.java b/luni/src/main/java/javax/net/ssl/SSLSocketFactory.java index 5c0f15d..b506fa6 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 @@ public abstract class SSLSocketFactory extends SocketFactory { // The default SSL socket factory private static SocketFactory defaultSocketFactory; - private static String defaultName; - private static int lastCacheVersion = -1; /** @@ -45,28 +43,38 @@ public abstract class SSLSocketFactory extends SocketFactory { */ 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 @@ public abstract class SSLSocketFactory extends SocketFactory { 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 @@ public class SSLSocketFactoryTest extends TestCase { fail("Cannot find a way to clear out the SocketFactory provider"); } + assertNull(Security.getProperty(SSL_PROPERTY)); return origProvider; } |