changeset 55687:065142ace8e9

8227720: Improve ExtendedSocketOptions initialization Reviewed-by: chegar, vtewari
author redestad
date Tue, 16 Jul 2019 17:50:54 +0200
parents b8152c273f76
children 0e1bc587472c
files src/java.base/share/classes/sun/net/ext/ExtendedSocketOptions.java src/jdk.net/linux/classes/jdk/net/LinuxSocketOptions.java src/jdk.net/macosx/classes/jdk/net/MacOSXSocketOptions.java src/jdk.net/solaris/classes/jdk/net/SolarisSocketOptions.java
diffstat 4 files changed, 58 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/sun/net/ext/ExtendedSocketOptions.java	Tue Jul 16 18:05:42 2019 +0300
+++ b/src/java.base/share/classes/sun/net/ext/ExtendedSocketOptions.java	Tue Jul 16 17:50:54 2019 +0200
@@ -29,8 +29,8 @@
 import java.net.SocketException;
 import java.net.SocketOption;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.Set;
-import java.util.stream.Collectors;
 
 /**
  * Defines the infrastructure to support extended socket options, beyond those
@@ -45,6 +45,9 @@
     public static final short SOCK_DGRAM = 2;
 
     private final Set<SocketOption<?>> options;
+    private final Set<SocketOption<?>> datagramOptions;
+    private final Set<SocketOption<?>> clientStreamOptions;
+    private final Set<SocketOption<?>> serverStreamOptions;
 
     /** Tells whether or not the option is supported. */
     public final boolean isOptionSupported(SocketOption<?> option) {
@@ -78,11 +81,11 @@
         return getInstance().options0(SOCK_DGRAM, false);
     }
 
-    private boolean isDatagramOption(SocketOption<?> option) {
+    private static boolean isDatagramOption(SocketOption<?> option) {
         return !option.name().startsWith("TCP_");
     }
 
-    private boolean isStreamOption(SocketOption<?> option, boolean server) {
+    private static boolean isStreamOption(SocketOption<?> option, boolean server) {
         if (server && "SO_FLOW_SLA".equals(option.name())) {
             return false;
         } else {
@@ -91,23 +94,19 @@
     }
 
     private Set<SocketOption<?>> options0(short type, boolean server) {
-        Set<SocketOption<?>> extOptions;
         switch (type) {
             case SOCK_DGRAM:
-                extOptions = options.stream()
-                        .filter(option -> isDatagramOption(option))
-                        .collect(Collectors.toUnmodifiableSet());
-                break;
+                return datagramOptions;
             case SOCK_STREAM:
-                extOptions = options.stream()
-                        .filter(option -> isStreamOption(option, server))
-                        .collect(Collectors.toUnmodifiableSet());
-                break;
+                if (server) {
+                    return serverStreamOptions;
+                } else {
+                    return clientStreamOptions;
+                }
             default:
                 //this will never happen
                 throw new IllegalArgumentException("Invalid socket option type");
         }
-        return extOptions;
     }
 
     /** Sets the value of a socket option, for the given socket. */
@@ -120,6 +119,23 @@
 
     protected ExtendedSocketOptions(Set<SocketOption<?>> options) {
         this.options = options;
+        var datagramOptions = new HashSet<SocketOption<?>>();
+        var serverStreamOptions = new HashSet<SocketOption<?>>();
+        var clientStreamOptions = new HashSet<SocketOption<?>>();
+        for (var option : options) {
+            if (isDatagramOption(option)) {
+                datagramOptions.add(option);
+            }
+            if (isStreamOption(option, true)) {
+                serverStreamOptions.add(option);
+            }
+            if (isStreamOption(option, false)) {
+                clientStreamOptions.add(option);
+            }
+        }
+        this.datagramOptions = Set.copyOf(datagramOptions);
+        this.serverStreamOptions = Set.copyOf(serverStreamOptions);
+        this.clientStreamOptions = Set.copyOf(clientStreamOptions);
     }
 
     private static volatile ExtendedSocketOptions instance;
--- a/src/jdk.net/linux/classes/jdk/net/LinuxSocketOptions.java	Tue Jul 16 18:05:42 2019 +0300
+++ b/src/jdk.net/linux/classes/jdk/net/LinuxSocketOptions.java	Tue Jul 16 17:50:54 2019 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2019, 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
@@ -95,9 +95,14 @@
     private static native boolean keepAliveOptionsSupported0();
     private static native boolean quickAckSupported0();
     static {
-        AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
+        if (System.getSecurityManager() == null) {
             System.loadLibrary("extnet");
-            return null;
-        });
+        } else {
+            AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
+                System.loadLibrary("extnet");
+                return null;
+            });
+        }
     }
-}
\ No newline at end of file
+}
+
--- a/src/jdk.net/macosx/classes/jdk/net/MacOSXSocketOptions.java	Tue Jul 16 18:05:42 2019 +0300
+++ b/src/jdk.net/macosx/classes/jdk/net/MacOSXSocketOptions.java	Tue Jul 16 17:50:54 2019 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, 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
@@ -77,9 +77,13 @@
     private static native int getTcpKeepAliveIntvl0(int fd) throws SocketException;
     private static native boolean keepAliveOptionsSupported0();
     static {
-        AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
+        if (System.getSecurityManager() == null) {
             System.loadLibrary("extnet");
-            return null;
-        });
+        } else {
+            AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
+                System.loadLibrary("extnet");
+                return null;
+            });
+        }
     }
 }
--- a/src/jdk.net/solaris/classes/jdk/net/SolarisSocketOptions.java	Tue Jul 16 18:05:42 2019 +0300
+++ b/src/jdk.net/solaris/classes/jdk/net/SolarisSocketOptions.java	Tue Jul 16 17:50:54 2019 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2019, 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
@@ -45,12 +45,16 @@
     private static native void init();
 
     static {
-        AccessController.doPrivileged(new PrivilegedAction<Void>() {
-            public Void run() {
-                System.loadLibrary("extnet");
-                return null;
-            }
-        });
+        if (System.getSecurityManager() == null) {
+            System.loadLibrary("extnet");
+        } else {
+            AccessController.doPrivileged(new PrivilegedAction<Void>() {
+                public Void run() {
+                    System.loadLibrary("extnet");
+                    return null;
+                }
+            });
+        }
         init();
     }
 }