< prev index next >

src/java.base/share/classes/sun/nio/ch/Net.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.ch;
  27 
  28 import java.io.*;
  29 import java.net.*;
  30 import jdk.net.*;
  31 import java.nio.channels.*;
  32 import java.util.*;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedAction;
  35 import sun.net.ExtendedOptionsImpl;

  36 
  37 
  38 public class Net {
  39 
  40     private Net() { }
  41 
  42     // unspecified protocol family
  43     static final ProtocolFamily UNSPEC = new ProtocolFamily() {
  44         public String name() {
  45             return "UNSPEC";
  46         }
  47     };
  48 
  49     // set to true if exclusive binding is on for Windows
  50     private static final boolean exclusiveBind;
  51 
  52     // set to true if the fast tcp loopback should be enabled on Windows
  53     private static final boolean fastLoopback;
  54 
  55     // -- Miscellaneous utilities --


 365         // only simple values supported by this method
 366         if (type != Integer.class && type != Boolean.class)
 367             throw new AssertionError("Should not reach here");
 368 
 369         // map option name to platform level/name
 370         OptionKey key = SocketOptionRegistry.findOption(name, family);
 371         if (key == null)
 372             throw new AssertionError("Option not found");
 373 
 374         boolean mayNeedConversion = (family == UNSPEC);
 375         int value = getIntOption0(fd, mayNeedConversion, key.level(), key.name());
 376 
 377         if (type == Integer.class) {
 378             return Integer.valueOf(value);
 379         } else {
 380             return (value == 0) ? Boolean.FALSE : Boolean.TRUE;
 381         }
 382     }
 383 
 384     public static boolean isFastTcpLoopbackRequested() {
 385         String loopbackProp = java.security.AccessController.doPrivileged(
 386             new PrivilegedAction<String>() {
 387                 @Override
 388                 public String run() {
 389                     return System.getProperty("jdk.net.useFastTcpLoopback");
 390                 }
 391             });
 392         boolean enable;
 393         if ("".equals(loopbackProp)) {
 394             enable = true;
 395         } else {
 396             enable = Boolean.parseBoolean(loopbackProp);
 397         }
 398         return enable;
 399     }
 400 
 401     // -- Socket operations --
 402 
 403     private static native boolean isIPv6Available0();
 404 
 405     private static native boolean isReusePortAvailable0();
 406 
 407     /*
 408      * Returns 1 for Windows and -1 for Solaris/Linux/Mac OS
 409      */
 410     private static native int isExclusiveBindAvailable();
 411 


 630     static native short pollhupValue();
 631     static native short pollnvalValue();
 632     static native short pollconnValue();
 633 
 634     static {
 635         IOUtil.load();
 636         initIDs();
 637 
 638         POLLIN     = pollinValue();
 639         POLLOUT    = polloutValue();
 640         POLLERR    = pollerrValue();
 641         POLLHUP    = pollhupValue();
 642         POLLNVAL   = pollnvalValue();
 643         POLLCONN   = pollconnValue();
 644     }
 645 
 646     static {
 647         int availLevel = isExclusiveBindAvailable();
 648         if (availLevel >= 0) {
 649             String exclBindProp =
 650                 java.security.AccessController.doPrivileged(
 651                     new PrivilegedAction<String>() {
 652                         @Override
 653                         public String run() {
 654                             return System.getProperty(
 655                                     "sun.net.useExclusiveBind");
 656                         }
 657                     });
 658             if (exclBindProp != null) {
 659                 exclusiveBind = exclBindProp.length() == 0 ?
 660                         true : Boolean.parseBoolean(exclBindProp);
 661             } else if (availLevel == 1) {
 662                 exclusiveBind = true;
 663             } else {
 664                 exclusiveBind = false;
 665             }
 666         } else {
 667             exclusiveBind = false;
 668         }
 669 
 670         fastLoopback = isFastTcpLoopbackRequested();
 671     }
 672 }


  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.ch;
  27 
  28 import java.io.*;
  29 import java.net.*;
  30 import jdk.net.*;
  31 import java.nio.channels.*;
  32 import java.util.*;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedAction;
  35 import sun.net.ExtendedOptionsImpl;
  36 import sun.security.action.GetPropertyAction;
  37 
  38 
  39 public class Net {
  40 
  41     private Net() { }
  42 
  43     // unspecified protocol family
  44     static final ProtocolFamily UNSPEC = new ProtocolFamily() {
  45         public String name() {
  46             return "UNSPEC";
  47         }
  48     };
  49 
  50     // set to true if exclusive binding is on for Windows
  51     private static final boolean exclusiveBind;
  52 
  53     // set to true if the fast tcp loopback should be enabled on Windows
  54     private static final boolean fastLoopback;
  55 
  56     // -- Miscellaneous utilities --


 366         // only simple values supported by this method
 367         if (type != Integer.class && type != Boolean.class)
 368             throw new AssertionError("Should not reach here");
 369 
 370         // map option name to platform level/name
 371         OptionKey key = SocketOptionRegistry.findOption(name, family);
 372         if (key == null)
 373             throw new AssertionError("Option not found");
 374 
 375         boolean mayNeedConversion = (family == UNSPEC);
 376         int value = getIntOption0(fd, mayNeedConversion, key.level(), key.name());
 377 
 378         if (type == Integer.class) {
 379             return Integer.valueOf(value);
 380         } else {
 381             return (value == 0) ? Boolean.FALSE : Boolean.TRUE;
 382         }
 383     }
 384 
 385     public static boolean isFastTcpLoopbackRequested() {
 386         String loopbackProp =
 387                 GetPropertyAction.getProperty("jdk.net.useFastTcpLoopback");





 388         boolean enable;
 389         if ("".equals(loopbackProp)) {
 390             enable = true;
 391         } else {
 392             enable = Boolean.parseBoolean(loopbackProp);
 393         }
 394         return enable;
 395     }
 396 
 397     // -- Socket operations --
 398 
 399     private static native boolean isIPv6Available0();
 400 
 401     private static native boolean isReusePortAvailable0();
 402 
 403     /*
 404      * Returns 1 for Windows and -1 for Solaris/Linux/Mac OS
 405      */
 406     private static native int isExclusiveBindAvailable();
 407 


 626     static native short pollhupValue();
 627     static native short pollnvalValue();
 628     static native short pollconnValue();
 629 
 630     static {
 631         IOUtil.load();
 632         initIDs();
 633 
 634         POLLIN     = pollinValue();
 635         POLLOUT    = polloutValue();
 636         POLLERR    = pollerrValue();
 637         POLLHUP    = pollhupValue();
 638         POLLNVAL   = pollnvalValue();
 639         POLLCONN   = pollconnValue();
 640     }
 641 
 642     static {
 643         int availLevel = isExclusiveBindAvailable();
 644         if (availLevel >= 0) {
 645             String exclBindProp =
 646                     GetPropertyAction.getProperty("sun.net.useExclusiveBind");







 647             if (exclBindProp != null) {
 648                 exclusiveBind = exclBindProp.isEmpty() ?
 649                         true : Boolean.parseBoolean(exclBindProp);
 650             } else if (availLevel == 1) {
 651                 exclusiveBind = true;
 652             } else {
 653                 exclusiveBind = false;
 654             }
 655         } else {
 656             exclusiveBind = false;
 657         }
 658 
 659         fastLoopback = isFastTcpLoopbackRequested();
 660     }
 661 }
< prev index next >