Cleanup of system property handling niosocketimpl-branch
authoralanb
Sun, 10 Feb 2019 17:37:38 +0000
branchniosocketimpl-branch
changeset 57173 6692e71a4e9f
parent 57172 63ab5af5d009
child 57174 899641440751
Cleanup of system property handling
src/java.base/share/classes/java/net/ServerSocket.java
src/java.base/share/classes/java/net/Socket.java
src/java.base/share/classes/java/net/SocketImpl.java
--- a/src/java.base/share/classes/java/net/ServerSocket.java	Sun Feb 10 09:54:31 2019 +0000
+++ b/src/java.base/share/classes/java/net/ServerSocket.java	Sun Feb 10 17:37:38 2019 +0000
@@ -295,9 +295,7 @@
             impl = factory.createSocketImpl();
             checkOldImpl();
         } else {
-            // No need to do a checkOldImpl() here, we know it's an up to date
-            // SocketImpl!
-            impl = SocketImpl.createDefaultSocketImpl(true);
+            impl = SocketImpl.createSocketImpl(true);
         }
         if (impl != null)
             impl.setServerSocket(this);
--- a/src/java.base/share/classes/java/net/Socket.java	Sun Feb 10 09:54:31 2019 +0000
+++ b/src/java.base/share/classes/java/net/Socket.java	Sun Feb 10 17:37:38 2019 +0000
@@ -137,17 +137,14 @@
                                   epoint.getPort());
             }
 
-            SocketImpl si = SocketImpl.createDefaultSocketImpl(false);
-            if (type == Proxy.Type.SOCKS) {
-                impl = new SocksSocketImpl(p, si);
-            } else {
-                impl = new HttpConnectSocketImpl(p, si);
-            }
+            SocketImpl si = SocketImpl.createSocketImpl(false);
+            impl = (type == Proxy.Type.SOCKS) ? new SocksSocketImpl(p, si)
+                                              : new HttpConnectSocketImpl(p, si);
             impl.setSocket(this);
         } else {
             if (p == Proxy.NO_PROXY) {
                 if (factory == null) {
-                    impl = SocketImpl.createDefaultSocketImpl(false);
+                    impl = SocketImpl.createSocketImpl(false);
                     impl.setSocket(this);
                 } else
                     setImpl();
@@ -500,7 +497,7 @@
         if (factory != null) {
             return factory.createSocketImpl();
         } else {
-            return SocketImpl.createDefaultSocketImpl(false);
+            return SocketImpl.createSocketImpl(false);
         }
     }
 
@@ -518,9 +515,7 @@
             impl = factory.createSocketImpl();
             checkOldImpl();
         } else {
-            // No need to do a checkOldImpl() here, we know it's an up to date
-            // SocketImpl!
-            SocketImpl si = SocketImpl.createDefaultSocketImpl(false);
+            SocketImpl si = SocketImpl.createSocketImpl(false);
             impl = new SocksSocketImpl(si);
         }
         if (impl != null)
--- a/src/java.base/share/classes/java/net/SocketImpl.java	Sun Feb 10 09:54:31 2019 +0000
+++ b/src/java.base/share/classes/java/net/SocketImpl.java	Sun Feb 10 17:37:38 2019 +0000
@@ -25,14 +25,14 @@
 
 package java.net;
 
-import java.io.*;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
-import java.security.PrivilegedExceptionAction;
+import java.io.FileDescriptor;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.Set;
 
-import sun.net.NetProperties;
 import sun.nio.ch.NioSocketImpl;
+import sun.security.action.GetPropertyAction;
 
 /**
  * The abstract class {@code SocketImpl} is a common superclass
@@ -46,6 +46,24 @@
  * @since   1.0
  */
 public abstract class SocketImpl implements SocketOptions {
+    private static final boolean USE_PLAINSOCKETIMPL = usePlainSocketImpl();
+
+    private static boolean usePlainSocketImpl() {
+        String s = GetPropertyAction.privilegedGetProperty("jdk.net.socketimpl.default");
+        return "classic".equalsIgnoreCase(s);
+    }
+
+    /**
+     * Creates a instance of platform's SocketImpl
+     */
+    static SocketImpl createSocketImpl(boolean server) {
+        if (USE_PLAINSOCKETIMPL) {
+            return new PlainSocketImpl();
+        } else {
+            return new NioSocketImpl(server);
+        }
+    }
+
     /**
      * The actual Socket object.
      */
@@ -72,32 +90,6 @@
      */
     protected int localport;
 
-    private static final boolean useNioSocketImpl = getUseNioSocketImpl();
-
-    // A simple way to override the socketimpl by creating a file in $user.dir
-    private static boolean getUseNioSocketImpl() {
-        // temporary for testing only
-        try {
-            return AccessController.doPrivileged(new PrivilegedExceptionAction<Boolean>() {
-                @Override
-                public Boolean run() throws Exception {
-                    String s = NetProperties.get("jdk.net.socketimpl.default");
-                    return (s == null || !s.equalsIgnoreCase("classic"));
-                }
-            });
-        } catch (PrivilegedActionException e) {
-            return false;
-        }
-    }
-
-    static SocketImpl createDefaultSocketImpl(boolean server) {
-        if (useNioSocketImpl) {
-            return new NioSocketImpl(server);
-        } else {
-            return new PlainSocketImpl();
-        }
-    }
-
     /**
      * Creates either a stream or a datagram socket.
      *