src/share/classes/java/net/InetAddress.java
Print this page
*** 250,261 ****
InetAddressHolder holder() {
return holder;
}
! /* Used to store the name service provider */
! private static List<NameService> nameServices = null;
/* Used to store the best available hostname */
private transient String canonicalHostName = null;
/** use serialVersionUID from JDK 1.0.2 for interoperability */
--- 250,344 ----
InetAddressHolder holder() {
return holder;
}
! /* Used to store the name service providers */
! private static final class NameServices {
! private static final List<NameService> nameServices = new ArrayList<>();
!
! static {
! // get name service if provided and requested
! String propPrefix = "sun.net.spi.nameservice.provider.";
! int n = 1;
! String provider = AccessController.doPrivileged(
! new GetPropertyAction(propPrefix + n));
! while (provider != null) {
! NameService ns = createNSProvider(provider);
! if (ns != null)
! nameServices.add(ns);
!
! n++;
! provider = AccessController.doPrivileged(
! new GetPropertyAction(propPrefix + n));
! }
!
! // if not designate any name services provider,
! // create a default one
! if (nameServices.size() == 0) {
! NameService ns = createNSProvider("default");
! nameServices.add(ns);
! }
! }
!
! private static NameService createNSProvider(String provider) {
! if (provider == null)
! return null;
!
! NameService nameService = null;
! if (provider.equals("default")) {
! // initialize the default name service
! nameService = new NameService() {
! public InetAddress[] lookupAllHostAddr(String host)
! throws UnknownHostException {
! return impl.lookupAllHostAddr(host);
! }
! public String getHostByAddr(byte[] addr)
! throws UnknownHostException {
! return impl.getHostByAddr(addr);
! }
! };
! } else {
! final String providerName = provider;
! try {
! nameService = java.security.AccessController.doPrivileged(
! new java.security.PrivilegedExceptionAction<NameService>() {
! public NameService run() {
! Iterator<NameServiceDescriptor> itr =
! ServiceLoader.load(NameServiceDescriptor.class)
! .iterator();
! while (itr.hasNext()) {
! NameServiceDescriptor nsd = itr.next();
! if (providerName.
! equalsIgnoreCase(nsd.getType()+","
! +nsd.getProviderName())) {
! try {
! return nsd.createNameService();
! } catch (Exception e) {
! e.printStackTrace();
! System.err.println(
! "Cannot create name service:"
! +providerName+": " + e);
! }
! }
! }
!
! return null;
! }
! }
! );
! } catch (java.security.PrivilegedActionException e) {
! }
! }
!
! return nameService;
! }
!
! static List<NameService> get() {
! return nameServices;
! }
! }
/* Used to store the best available hostname */
private transient String canonicalHostName = null;
/** use serialVersionUID from JDK 1.0.2 for interoperability */
*** 588,598 ****
*
* @see SecurityManager#checkConnect
*/
private static String getHostFromNameService(InetAddress addr, boolean check) {
String host = null;
! for (NameService nameService : nameServices) {
try {
// first lookup the hostname
host = nameService.getHostByAddr(addr.getAddress());
/* check to see if calling code is allowed to know
--- 671,681 ----
*
* @see SecurityManager#checkConnect
*/
private static String getHostFromNameService(InetAddress addr, boolean check) {
String host = null;
! for (NameService nameService : NameServices.get()) {
try {
// first lookup the hostname
host = nameService.getHostByAddr(addr.getAddress());
/* check to see if calling code is allowed to know
*** 892,981 ****
// not found
return null;
}
- private static NameService createNSProvider(String provider) {
- if (provider == null)
- return null;
-
- NameService nameService = null;
- if (provider.equals("default")) {
- // initialize the default name service
- nameService = new NameService() {
- public InetAddress[] lookupAllHostAddr(String host)
- throws UnknownHostException {
- return impl.lookupAllHostAddr(host);
- }
- public String getHostByAddr(byte[] addr)
- throws UnknownHostException {
- return impl.getHostByAddr(addr);
- }
- };
- } else {
- final String providerName = provider;
- try {
- nameService = java.security.AccessController.doPrivileged(
- new java.security.PrivilegedExceptionAction<NameService>() {
- public NameService run() {
- Iterator<NameServiceDescriptor> itr =
- ServiceLoader.load(NameServiceDescriptor.class)
- .iterator();
- while (itr.hasNext()) {
- NameServiceDescriptor nsd = itr.next();
- if (providerName.
- equalsIgnoreCase(nsd.getType()+","
- +nsd.getProviderName())) {
- try {
- return nsd.createNameService();
- } catch (Exception e) {
- e.printStackTrace();
- System.err.println(
- "Cannot create name service:"
- +providerName+": " + e);
- }
- }
- }
-
- return null;
- }
- }
- );
- } catch (java.security.PrivilegedActionException e) {
- }
- }
-
- return nameService;
- }
-
static {
// create the impl
impl = InetAddressImplFactory.create();
-
- // get name service if provided and requested
- String provider = null;;
- String propPrefix = "sun.net.spi.nameservice.provider.";
- int n = 1;
- nameServices = new ArrayList<NameService>();
- provider = AccessController.doPrivileged(
- new GetPropertyAction(propPrefix + n));
- while (provider != null) {
- NameService ns = createNSProvider(provider);
- if (ns != null)
- nameServices.add(ns);
-
- n++;
- provider = AccessController.doPrivileged(
- new GetPropertyAction(propPrefix + n));
- }
-
- // if not designate any name services provider,
- // create a default one
- if (nameServices.size() == 0) {
- NameService ns = createNSProvider("default");
- nameServices.add(ns);
- }
}
/**
* Creates an InetAddress based on the provided host name and IP address.
* No name service is checked for the validity of the address.
--- 975,987 ----
*** 1289,1299 ****
if ((addresses = checkLookupTable(host)) == null) {
try {
// This is the first thread which looks up the addresses
// this host or the cache entry for this host has been
// expired so this thread should do the lookup.
! for (NameService nameService : nameServices) {
try {
/*
* Do not put the call to lookup() inside the
* constructor. if you do you will still be
* allocating space when the lookup fails.
--- 1295,1305 ----
if ((addresses = checkLookupTable(host)) == null) {
try {
// This is the first thread which looks up the addresses
// this host or the cache entry for this host has been
// expired so this thread should do the lookup.
! for (NameService nameService : NameServices.get()) {
try {
/*
* Do not put the call to lookup() inside the
* constructor. if you do you will still be
* allocating space when the lookup fails.