< prev index next >

src/java.base/share/classes/javax/net/ssl/SSLContext.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -24,13 +24,12 @@
  */
 
 package javax.net.ssl;
 
 import java.security.*;
-import java.lang.invoke.MethodHandles;
-import java.lang.invoke.VarHandle;
 import java.util.Objects;
+
 import sun.security.jca.GetInstance;
 
 /**
  * Instances of this class represent a secure socket protocol
  * implementation which acts as a factory for secure socket

@@ -57,24 +56,10 @@
 
     private final SSLContextSpi contextSpi;
 
     private final String protocol;
 
-    private static volatile SSLContext defaultContext;
-
-    private static final VarHandle VH_DEFAULT_CONTEXT;
-
-    static {
-        try {
-            VH_DEFAULT_CONTEXT = MethodHandles.lookup()
-                .findStaticVarHandle(
-                    SSLContext.class, "defaultContext", SSLContext.class);
-        } catch (Exception e) {
-            throw new ExceptionInInitializerError(e);
-        }
-    }
-
     /**
      * Creates an SSLContext object.
      *
      * @param contextSpi the delegate
      * @param provider the provider

@@ -85,10 +70,12 @@
         this.contextSpi = contextSpi;
         this.provider = provider;
         this.protocol = protocol;
     }
 
+    private static SSLContext defaultContext;
+
     /**
      * Returns the default SSL context.
      *
      * <p>If a default context was set using the {@link #setDefault
      * SSLContext.setDefault()} method, it is returned. Otherwise, the first

@@ -102,20 +89,16 @@
      * @return the default SSL context
      * @throws NoSuchAlgorithmException if the
      *   {@link SSLContext#getInstance SSLContext.getInstance()} call fails
      * @since 1.6
      */
-    public static SSLContext getDefault() throws NoSuchAlgorithmException {
-        SSLContext temporaryContext = defaultContext;
-        if (temporaryContext == null) {
-            temporaryContext = SSLContext.getInstance("Default");
-            if (!VH_DEFAULT_CONTEXT.compareAndSet(null, temporaryContext)) {
-                temporaryContext = defaultContext;
-            }
+    public static synchronized SSLContext getDefault()
+            throws NoSuchAlgorithmException {
+        if (defaultContext == null) {
+            defaultContext = SSLContext.getInstance("Default");
         }
-
-        return temporaryContext;
+        return defaultContext;
     }
 
     /**
      * Sets the default SSL context. It will be returned by subsequent calls
      * to {@link #getDefault}. The default context must be immediately usable

@@ -126,19 +109,18 @@
      * @throws  SecurityException if a security manager exists and its
      *          {@code checkPermission} method does not allow
      *          {@code SSLPermission("setDefaultSSLContext")}
      * @since 1.6
      */
-    public static void setDefault(SSLContext context) {
+    public static synchronized void setDefault(SSLContext context) {
         if (context == null) {
             throw new NullPointerException();
         }
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) {
             sm.checkPermission(new SSLPermission("setDefaultSSLContext"));
         }
-
         defaultContext = context;
     }
 
     /**
      * Returns a {@code SSLContext} object that implements the
< prev index next >