示例#1
0
  private static Object getAddressFromNameService(String host, InetAddress reqAddr)
      throws UnknownHostException {
    Object obj = null;
    boolean success = false;
    UnknownHostException ex = null;

    // Check whether the host is in the lookupTable.
    // 1) If the host isn't in the lookupTable when
    //    checkLookupTable() is called, checkLookupTable()
    //    would add the host in the lookupTable and
    //    return null. So we will do the lookup.
    // 2) If the host is in the lookupTable when
    //    checkLookupTable() is called, the current thread
    //    would be blocked until the host is removed
    //    from the lookupTable. Then this thread
    //    should try to look up the addressCache.
    //     i) if it found the address in the
    //        addressCache, checkLookupTable()  would
    //        return the address.
    //     ii) if it didn't find the address in the
    //         addressCache for any reason,
    //         it should add the host in the
    //         lookupTable and return null so the
    //         following code would do  a lookup itself.
    if ((obj = checkLookupTable(host)) == null) {
      // This is the first thread which looks up the address
      // 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.
           */

          obj = nameService.lookupAllHostAddr(host);
          success = true;
          break;
        } catch (UnknownHostException uhe) {
          if (host.equalsIgnoreCase("localhost")) {
            InetAddress[] local = new InetAddress[] {impl.loopbackAddress()};
            obj = local;
            success = true;
            break;
          } else {
            obj = unknown_array;
            success = false;
            ex = uhe;
          }
        }
      }

      // More to do?
      InetAddress[] addrs = (InetAddress[]) obj;
      if (reqAddr != null && addrs.length > 1 && !addrs[0].equals(reqAddr)) {
        // Find it?
        int i = 1;
        for (; i < addrs.length; i++) {
          if (addrs[i].equals(reqAddr)) {
            break;
          }
        }
        // Rotate
        if (i < addrs.length) {
          InetAddress tmp, tmp2 = reqAddr;
          for (int j = 0; j < i; j++) {
            tmp = addrs[j];
            addrs[j] = tmp2;
            tmp2 = tmp;
          }
          addrs[i] = tmp2;
        }
      }

      // Cache the address.
      cacheAddress(host, obj, success);
      // Delete the host from the lookupTable, and
      // notify all threads waiting for the monitor
      // for lookupTable.
      updateLookupTable(host);
      if (!success && ex != null) throw ex;
    }

    return obj;
  }