< prev index next >

src/java.base/share/classes/java/net/InetAddress.java

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


1106         nameService = createNameService();
1107         }
1108 
1109     /**
1110      * Create an instance of the NameService interface based on
1111      * the setting of the {@codejdk.net.hosts.file} system property.
1112      *
1113      * <p>The default NameService is the PlatformNameService, which typically
1114      * delegates name and address resolution calls to the underlying
1115      * OS network libraries.
1116      *
1117      * <p> A HostsFileNameService is created if the {@code jdk.net.hosts.file}
1118      * system property is set. If the specified file doesn't exist, the name or
1119      * address lookup will result in an UnknownHostException. Thus, non existent
1120      * hosts file is handled as if the file is empty.
1121      *
1122      * @return a NameService
1123      */
1124     private static NameService createNameService() {
1125 
1126         String hostsFileName = AccessController
1127                 .doPrivileged(new GetPropertyAction("jdk.net.hosts.file"));
1128         NameService theNameService;
1129         if (hostsFileName != null) {
1130             theNameService = new HostsFileNameService(hostsFileName);
1131         } else {
1132             theNameService = new PlatformNameService();
1133         }
1134         return theNameService;
1135     }
1136 
1137     /**
1138      * Creates an InetAddress based on the provided host name and IP address.
1139      * No name service is checked for the validity of the address.
1140      *
1141      * <p> The host name can either be a machine name, such as
1142      * "{@code java.sun.com}", or a textual representation of its IP
1143      * address.
1144      * <p> No validity checking is done on the host name either.
1145      *
1146      * <p> If addr specifies an IPv4 address an instance of Inet4Address
1147      * will be returned; otherwise, an instance of Inet6Address


1626      * Returns the InetAddress representing anyLocalAddress
1627      * (typically 0.0.0.0 or ::0)
1628      */
1629     static InetAddress anyLocalAddress() {
1630         return impl.anyLocalAddress();
1631     }
1632 
1633     /*
1634      * Load and instantiate an underlying impl class
1635      */
1636     static InetAddressImpl loadImpl(String implName) {
1637         Object impl = null;
1638 
1639         /*
1640          * Property "impl.prefix" will be prepended to the classname
1641          * of the implementation object we instantiate, to which we
1642          * delegate the real work (like native methods).  This
1643          * property can vary across implementations of the java.
1644          * classes.  The default is an empty String "".
1645          */
1646         String prefix = AccessController.doPrivileged(
1647                       new GetPropertyAction("impl.prefix", ""));
1648         try {
1649             impl = Class.forName("java.net." + prefix + implName).newInstance();
1650         } catch (ClassNotFoundException e) {
1651             System.err.println("Class not found: java.net." + prefix +
1652                                implName + ":\ncheck impl.prefix property " +
1653                                "in your properties file.");
1654         } catch (InstantiationException e) {
1655             System.err.println("Could not instantiate: java.net." + prefix +
1656                                implName + ":\ncheck impl.prefix property " +
1657                                "in your properties file.");
1658         } catch (IllegalAccessException e) {
1659             System.err.println("Cannot access class: java.net." + prefix +
1660                                implName + ":\ncheck impl.prefix property " +
1661                                "in your properties file.");
1662         }
1663 
1664         if (impl == null) {
1665             try {
1666                 impl = Class.forName(implName).newInstance();
1667             } catch (Exception e) {




1106         nameService = createNameService();
1107         }
1108 
1109     /**
1110      * Create an instance of the NameService interface based on
1111      * the setting of the {@codejdk.net.hosts.file} system property.
1112      *
1113      * <p>The default NameService is the PlatformNameService, which typically
1114      * delegates name and address resolution calls to the underlying
1115      * OS network libraries.
1116      *
1117      * <p> A HostsFileNameService is created if the {@code jdk.net.hosts.file}
1118      * system property is set. If the specified file doesn't exist, the name or
1119      * address lookup will result in an UnknownHostException. Thus, non existent
1120      * hosts file is handled as if the file is empty.
1121      *
1122      * @return a NameService
1123      */
1124     private static NameService createNameService() {
1125 
1126         String hostsFileName =
1127                 GetPropertyAction.getProperty("jdk.net.hosts.file");
1128         NameService theNameService;
1129         if (hostsFileName != null) {
1130             theNameService = new HostsFileNameService(hostsFileName);
1131         } else {
1132             theNameService = new PlatformNameService();
1133         }
1134         return theNameService;
1135     }
1136 
1137     /**
1138      * Creates an InetAddress based on the provided host name and IP address.
1139      * No name service is checked for the validity of the address.
1140      *
1141      * <p> The host name can either be a machine name, such as
1142      * "{@code java.sun.com}", or a textual representation of its IP
1143      * address.
1144      * <p> No validity checking is done on the host name either.
1145      *
1146      * <p> If addr specifies an IPv4 address an instance of Inet4Address
1147      * will be returned; otherwise, an instance of Inet6Address


1626      * Returns the InetAddress representing anyLocalAddress
1627      * (typically 0.0.0.0 or ::0)
1628      */
1629     static InetAddress anyLocalAddress() {
1630         return impl.anyLocalAddress();
1631     }
1632 
1633     /*
1634      * Load and instantiate an underlying impl class
1635      */
1636     static InetAddressImpl loadImpl(String implName) {
1637         Object impl = null;
1638 
1639         /*
1640          * Property "impl.prefix" will be prepended to the classname
1641          * of the implementation object we instantiate, to which we
1642          * delegate the real work (like native methods).  This
1643          * property can vary across implementations of the java.
1644          * classes.  The default is an empty String "".
1645          */
1646         String prefix = GetPropertyAction.getProperty("impl.prefix", "");

1647         try {
1648             impl = Class.forName("java.net." + prefix + implName).newInstance();
1649         } catch (ClassNotFoundException e) {
1650             System.err.println("Class not found: java.net." + prefix +
1651                                implName + ":\ncheck impl.prefix property " +
1652                                "in your properties file.");
1653         } catch (InstantiationException e) {
1654             System.err.println("Could not instantiate: java.net." + prefix +
1655                                implName + ":\ncheck impl.prefix property " +
1656                                "in your properties file.");
1657         } catch (IllegalAccessException e) {
1658             System.err.println("Cannot access class: java.net." + prefix +
1659                                implName + ":\ncheck impl.prefix property " +
1660                                "in your properties file.");
1661         }
1662 
1663         if (impl == null) {
1664             try {
1665                 impl = Class.forName(implName).newInstance();
1666             } catch (Exception e) {


< prev index next >