Print this page


Split Close
Expand all
Collapse all
          --- old/src/share/classes/java/net/InetAddress.java
          +++ new/src/share/classes/java/net/InetAddress.java
↓ open down ↓ 669 lines elided ↑ open up ↑
 670  670      private static Cache addressCache = new Cache(Cache.Type.Positive);
 671  671  
 672  672      private static Cache negativeCache = new Cache(Cache.Type.Negative);
 673  673  
 674  674      private static boolean addressCacheInit = false;
 675  675  
 676  676      static InetAddress[]    unknown_array; // put THIS in cache
 677  677  
 678  678      static InetAddressImpl  impl;
 679  679  
 680      -    private static HashMap<String, InetAddress[]> lookupTable
 681      -        = new HashMap<String, InetAddress[]>();
      680 +    private static final HashMap<String, Void> lookupTable = new HashMap<>();
 682  681  
 683  682      /**
 684  683       * Represents a cache entry
 685  684       */
 686  685      static final class CacheEntry {
 687  686  
 688  687          CacheEntry(InetAddress[] addresses, long expiration) {
 689  688              this.addresses = addresses;
 690  689              this.expiration = expiration;
 691  690          }
↓ open down ↓ 38 lines elided ↑ open up ↑
 730  729              if (policy == InetAddressCachePolicy.NEVER) {
 731  730                  return this;
 732  731              }
 733  732  
 734  733              // purge any expired entries
 735  734  
 736  735              if (policy != InetAddressCachePolicy.FOREVER) {
 737  736  
 738  737                  // As we iterate in insertion order we can
 739  738                  // terminate when a non-expired entry is found.
 740      -                LinkedList<String> expired = new LinkedList<String>();
      739 +                LinkedList<String> expired = new LinkedList<>();
 741  740                  long now = System.currentTimeMillis();
 742  741                  for (String key : cache.keySet()) {
 743  742                      CacheEntry entry = cache.get(key);
 744  743  
 745  744                      if (entry.expiration >= 0 && entry.expiration < now) {
 746  745                          expired.add(key);
 747  746                      } else {
 748  747                          break;
 749  748                      }
 750  749                  }
↓ open down ↓ 469 lines elided ↑ open up ↑
1220 1219          //    should try to look up the addressCache.
1221 1220          //     i) if it found the addresses in the
1222 1221          //        addressCache, checkLookupTable()  would
1223 1222          //        return the addresses.
1224 1223          //     ii) if it didn't find the addresses in the
1225 1224          //         addressCache for any reason,
1226 1225          //         it should add the host in the
1227 1226          //         lookupTable and return null so the
1228 1227          //         following code would do  a lookup itself.
1229 1228          if ((addresses = checkLookupTable(host)) == null) {
1230      -            // This is the first thread which looks up the addresses
1231      -            // this host or the cache entry for this host has been
1232      -            // expired so this thread should do the lookup.
1233      -            for (NameService nameService : nameServices) {
1234      -                try {
1235      -                    /*
1236      -                     * Do not put the call to lookup() inside the
1237      -                     * constructor.  if you do you will still be
1238      -                     * allocating space when the lookup fails.
1239      -                     */
     1229 +            try {
     1230 +                // This is the first thread which looks up the addresses
     1231 +                // this host or the cache entry for this host has been
     1232 +                // expired so this thread should do the lookup.
     1233 +                for (NameService nameService : nameServices) {
     1234 +                    try {
     1235 +                        /*
     1236 +                         * Do not put the call to lookup() inside the
     1237 +                         * constructor.  if you do you will still be
     1238 +                         * allocating space when the lookup fails.
     1239 +                         */
1240 1240  
1241      -                    addresses = nameService.lookupAllHostAddr(host);
1242      -                    success = true;
1243      -                    break;
1244      -                } catch (UnknownHostException uhe) {
1245      -                    if (host.equalsIgnoreCase("localhost")) {
1246      -                        InetAddress[] local = new InetAddress[] { impl.loopbackAddress() };
1247      -                        addresses = local;
     1241 +                        addresses = nameService.lookupAllHostAddr(host);
1248 1242                          success = true;
1249 1243                          break;
     1244 +                    } catch (UnknownHostException uhe) {
     1245 +                        if (host.equalsIgnoreCase("localhost")) {
     1246 +                            InetAddress[] local = new InetAddress[] { impl.loopbackAddress() };
     1247 +                            addresses = local;
     1248 +                            success = true;
     1249 +                            break;
     1250 +                        }
     1251 +                        else {
     1252 +                            addresses = unknown_array;
     1253 +                            success = false;
     1254 +                            ex = uhe;
     1255 +                        }
1250 1256                      }
1251      -                    else {
1252      -                        addresses = unknown_array;
1253      -                        success = false;
1254      -                        ex = uhe;
1255      -                    }
1256 1257                  }
1257      -            }
1258 1258  
1259      -            // Cache the addresses.
1260      -            cacheAddresses(host, addresses, success);
1261      -            // Delete the host from the lookupTable, and
1262      -            // notify all threads waiting for the monitor
1263      -            // for lookupTable.
1264      -            updateLookupTable(host);
1265      -            if (!success && ex != null)
1266      -                throw ex;
     1259 +                // Cache the addresses.
     1260 +                cacheAddresses(host, addresses, success);
     1261 +                if (!success && ex != null)
     1262 +                    throw ex;
     1263 +            } finally {
     1264 +                // Delete host from the lookupTable and notify
     1265 +                // all threads waiting on the lookupTable monitor.
     1266 +                updateLookupTable(host);
     1267 +            }
1267 1268          }
1268 1269  
1269 1270          return addresses;
1270 1271      }
1271 1272  
1272 1273  
1273 1274      private static InetAddress[] checkLookupTable(String host) {
1274      -        // make sure addresses is null.
1275      -        InetAddress[] addresses = null;
1276      -
1277 1275          synchronized (lookupTable) {
1278 1276              // If the host isn't in the lookupTable, add it in the
1279 1277              // lookuptable and return null. The caller should do
1280 1278              // the lookup.
1281 1279              if (lookupTable.containsKey(host) == false) {
1282 1280                  lookupTable.put(host, null);
1283      -                return addresses;
     1281 +                return null;
1284 1282              }
1285 1283  
1286 1284              // If the host is in the lookupTable, it means that another
1287 1285              // thread is trying to look up the addresses of this host.
1288 1286              // This thread should wait.
1289 1287              while (lookupTable.containsKey(host)) {
1290 1288                  try {
1291 1289                      lookupTable.wait();
1292 1290                  } catch (InterruptedException e) {
1293 1291                  }
1294 1292              }
1295 1293          }
1296 1294  
1297 1295          // The other thread has finished looking up the addresses of
1298 1296          // the host. This thread should retry to get the addresses
1299 1297          // from the addressCache. If it doesn't get the addresses from
1300 1298          // the cache, it will try to look up the addresses itself.
1301      -        addresses = getCachedAddresses(host);
     1299 +        InetAddress[] addresses = getCachedAddresses(host);
1302 1300          if (addresses == null) {
1303 1301              synchronized (lookupTable) {
1304 1302                  lookupTable.put(host, null);
     1303 +                return null;
1305 1304              }
1306 1305          }
1307 1306  
1308 1307          return addresses;
1309 1308      }
1310 1309  
1311 1310      private static void updateLookupTable(String host) {
1312 1311          synchronized (lookupTable) {
1313 1312              lookupTable.remove(host);
1314 1313              lookupTable.notifyAll();
↓ open down ↓ 188 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX