# HG changeset patch # User redestad # Date 1563292254 -7200 # Node ID 065142ace8e951b55de64ab3df500662318c27fd # Parent b8152c273f76e66f78f03f161c0a0aa84403b012 8227720: Improve ExtendedSocketOptions initialization Reviewed-by: chegar, vtewari diff -r b8152c273f76 -r 065142ace8e9 src/java.base/share/classes/sun/net/ext/ExtendedSocketOptions.java --- 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> options; + private final Set> datagramOptions; + private final Set> clientStreamOptions; + private final Set> 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> options0(short type, boolean server) { - Set> 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> options) { this.options = options; + var datagramOptions = new HashSet>(); + var serverStreamOptions = new HashSet>(); + var clientStreamOptions = new HashSet>(); + 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; diff -r b8152c273f76 -r 065142ace8e9 src/jdk.net/linux/classes/jdk/net/LinuxSocketOptions.java --- 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) () -> { + if (System.getSecurityManager() == null) { System.loadLibrary("extnet"); - return null; - }); + } else { + AccessController.doPrivileged((PrivilegedAction) () -> { + System.loadLibrary("extnet"); + return null; + }); + } } -} \ No newline at end of file +} + diff -r b8152c273f76 -r 065142ace8e9 src/jdk.net/macosx/classes/jdk/net/MacOSXSocketOptions.java --- 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) () -> { + if (System.getSecurityManager() == null) { System.loadLibrary("extnet"); - return null; - }); + } else { + AccessController.doPrivileged((PrivilegedAction) () -> { + System.loadLibrary("extnet"); + return null; + }); + } } } diff -r b8152c273f76 -r 065142ace8e9 src/jdk.net/solaris/classes/jdk/net/SolarisSocketOptions.java --- 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() { - public Void run() { - System.loadLibrary("extnet"); - return null; - } - }); + if (System.getSecurityManager() == null) { + System.loadLibrary("extnet"); + } else { + AccessController.doPrivileged(new PrivilegedAction() { + public Void run() { + System.loadLibrary("extnet"); + return null; + } + }); + } init(); } }