@Override
  public String getHostnameByIP(InetAddress address) {
    String host = address.getCanonicalHostName();
    if (host != null && host.length() != 0
        && host.charAt(host.length()-1) == '.') {
      host = host.substring(0, host.length()-1);
    }
    // Protect against the Java behaviour of returning the IP address as a string from a cache
    // instead of performing a reverse lookup.
    if (host != null && host.equals(address.getHostAddress())) {
      LOG.debug("IP address returned for FQDN detected: {}", address.getHostAddress());
      try {
        return DNS.reverseDns(address, null);
      } catch (NamingException lookupFailure) {
        LOG.warn("Failed to perform reverse lookup: {}", address);
      }
    }
    return host;
  }
@Test
  public void testGetHostNameByIP() throws UnknownHostException {
    InetAddress localhost = InetAddress.getLocalHost();
    assumeFalse("IP lookup support required",
        Objects.equals(localhost.getCanonicalHostName(), localhost.getHostAddress()));

    // Precondition: host name and canonical host name for unresolved returns an IP address.
    InetAddress unresolved = InetAddress.getByAddress(localhost.getHostAddress(),
        localhost.getAddress());
    assertEquals(localhost.getHostAddress(), unresolved.getHostName());

    // Test: Get the canonical name despite InetAddress caching
    String canonicalHostName = DNR.getHostnameByIP(unresolved);

    // Verify: The canonical host name doesn't match the host address but does match the localhost.
    assertNotEquals(localhost.getHostAddress(), canonicalHostName);
    assertEquals(localhost.getCanonicalHostName(), canonicalHostName);
  }