jdk/src/share/classes/java/net/InetAddress.java
changeset 9775 1b128726e887
parent 9550 c3a275ce56d3
child 10882 7ddadf2b8b4c
--- a/jdk/src/share/classes/java/net/InetAddress.java	Mon May 30 16:37:42 2011 +0100
+++ b/jdk/src/share/classes/java/net/InetAddress.java	Mon May 30 23:36:10 2011 +0100
@@ -1013,6 +1013,12 @@
         return InetAddress.getAllByName(host)[0];
     }
 
+    // called from deployment cache manager
+    private static InetAddress getByName(String host, InetAddress reqAddr)
+        throws UnknownHostException {
+        return InetAddress.getAllByName(host, reqAddr)[0];
+    }
+
     /**
      * Given the name of a host, returns an array of its IP addresses,
      * based on the configured name service on the system.
@@ -1054,6 +1060,11 @@
      */
     public static InetAddress[] getAllByName(String host)
         throws UnknownHostException {
+        return getAllByName(host, null);
+    }
+
+    private static InetAddress[] getAllByName(String host, InetAddress reqAddr)
+        throws UnknownHostException {
 
         if (host == null || host.length() == 0) {
             InetAddress[] ret = new InetAddress[1];
@@ -1113,7 +1124,7 @@
                 // We were expecting an IPv6 Litteral, but got something else
                 throw new UnknownHostException("["+host+"]");
             }
-        return getAllByName0(host);
+        return getAllByName0(host, reqAddr, true);
     }
 
     /**
@@ -1174,6 +1185,12 @@
      */
     static InetAddress[] getAllByName0 (String host, boolean check)
         throws UnknownHostException  {
+        return getAllByName0 (host, null, check);
+    }
+
+    private static InetAddress[] getAllByName0 (String host, InetAddress reqAddr, boolean check)
+        throws UnknownHostException  {
+
         /* If it gets here it is presumed to be a hostname */
         /* Cache.get can return: null, unknownAddress, or InetAddress[] */
 
@@ -1191,7 +1208,7 @@
 
         /* If no entry in cache, then do the host lookup */
         if (addresses == null) {
-            addresses = getAddressesFromNameService(host);
+            addresses = getAddressesFromNameService(host, reqAddr);
         }
 
         if (addresses == unknown_array)
@@ -1200,7 +1217,7 @@
         return addresses.clone();
     }
 
-    private static InetAddress[] getAddressesFromNameService(String host)
+    private static InetAddress[] getAddressesFromNameService(String host, InetAddress reqAddr)
         throws UnknownHostException
     {
         InetAddress[] addresses = null;
@@ -1256,10 +1273,32 @@
                     }
                 }
 
-                // Cache the addresses.
+                // More to do?
+                if (reqAddr != null && addresses.length > 1 && !addresses[0].equals(reqAddr)) {
+                    // Find it?
+                    int i = 1;
+                    for (; i < addresses.length; i++) {
+                        if (addresses[i].equals(reqAddr)) {
+                            break;
+                        }
+                    }
+                    // Rotate
+                    if (i < addresses.length) {
+                        InetAddress tmp, tmp2 = reqAddr;
+                        for (int j = 0; j < i; j++) {
+                            tmp = addresses[j];
+                            addresses[j] = tmp2;
+                            tmp2 = tmp;
+                        }
+                        addresses[i] = tmp2;
+                    }
+                }
+                // Cache the address.
                 cacheAddresses(host, addresses, success);
+
                 if (!success && ex != null)
                     throw ex;
+
             } finally {
                 // Delete host from the lookupTable and notify
                 // all threads waiting on the lookupTable monitor.
@@ -1393,7 +1432,7 @@
                     InetAddress[] localAddrs;
                     try {
                         localAddrs =
-                            InetAddress.getAddressesFromNameService(local);
+                            InetAddress.getAddressesFromNameService(local, null);
                     } catch (UnknownHostException uhe) {
                         // Rethrow with a more informative error message.
                         UnknownHostException uhe2 =