@Test public void testGetNegativeTtlThrowsOnForever() { log.info("When DNS is cached forever, we should throw."); try { Security.setProperty("networkaddress.cache.negative.ttl", "-1"); } catch (SecurityException exception) { log.error("We can't set the DNS cache period, so this test is effectively ignored."); return; } try { log.info("AddressUtil is (hopefully) going to spit out an error about DNS lookups. " + "you can ignore it."); AddressUtil.getAddressCacheNegativeTtl(null); fail("The JVM Security settings cache DNS failures forever, this should cause an exception."); } catch (IllegalArgumentException exception) { } }
public static int getAddressCacheNegativeTtl(UnknownHostException originalException) { int negativeTtl = 10; try { negativeTtl = Integer.parseInt(Security.getProperty("networkaddress.cache.negative.ttl")); } catch (NumberFormatException exception) { log.warn("Failed to get JVM negative DNS respones cache TTL due to format problem " + "(e.g. this JVM might not have the property). " + "Falling back to default based on Oracle JVM 1.4+ (10s)", exception); } catch (SecurityException exception) { log.warn("Failed to get JVM negative DNS response cache TTL due to security manager. " + "Falling back to default based on Oracle JVM 1.4+ (10s)", exception); } if (negativeTtl == -1) { log.error( "JVM negative DNS repsonse cache TTL is set to 'forever' and host lookup failed. " + "TTL can be changed with security property " + "'networkaddress.cache.negative.ttl', see java.net.InetAddress.", originalException); throw new IllegalArgumentException(originalException); } else if (negativeTtl < 0) { log.warn("JVM specified negative DNS response cache TTL was negative (and not 'forever'). " + "Falling back to default based on Oracle JVM 1.4+ (10s)"); negativeTtl = 10; } return negativeTtl; }
AddressUtil { public static int getAddressCacheNegativeTtl(UnknownHostException originalException) { int negativeTtl = 10; try { negativeTtl = Integer.parseInt(Security.getProperty("networkaddress.cache.negative.ttl")); } catch (NumberFormatException exception) { log.warn("Failed to get JVM negative DNS respones cache TTL due to format problem " + "(e.g. this JVM might not have the property). " + "Falling back to default based on Oracle JVM 1.4+ (10s)", exception); } catch (SecurityException exception) { log.warn("Failed to get JVM negative DNS response cache TTL due to security manager. " + "Falling back to default based on Oracle JVM 1.4+ (10s)", exception); } if (negativeTtl == -1) { log.error( "JVM negative DNS repsonse cache TTL is set to 'forever' and host lookup failed. " + "TTL can be changed with security property " + "'networkaddress.cache.negative.ttl', see java.net.InetAddress.", originalException); throw new IllegalArgumentException(originalException); } else if (negativeTtl < 0) { log.warn("JVM specified negative DNS response cache TTL was negative (and not 'forever'). " + "Falling back to default based on Oracle JVM 1.4+ (10s)"); negativeTtl = 10; } return negativeTtl; } }
AddressUtil { public static int getAddressCacheNegativeTtl(UnknownHostException originalException) { int negativeTtl = 10; try { negativeTtl = Integer.parseInt(Security.getProperty("networkaddress.cache.negative.ttl")); } catch (NumberFormatException exception) { log.warn("Failed to get JVM negative DNS respones cache TTL due to format problem " + "(e.g. this JVM might not have the property). " + "Falling back to default based on Oracle JVM 1.4+ (10s)", exception); } catch (SecurityException exception) { log.warn("Failed to get JVM negative DNS response cache TTL due to security manager. " + "Falling back to default based on Oracle JVM 1.4+ (10s)", exception); } if (negativeTtl == -1) { log.error( "JVM negative DNS repsonse cache TTL is set to 'forever' and host lookup failed. " + "TTL can be changed with security property " + "'networkaddress.cache.negative.ttl', see java.net.InetAddress.", originalException); throw new IllegalArgumentException(originalException); } else if (negativeTtl < 0) { log.warn("JVM specified negative DNS response cache TTL was negative (and not 'forever'). " + "Falling back to default based on Oracle JVM 1.4+ (10s)"); negativeTtl = 10; } return negativeTtl; }  }
AddressUtil { public static int getAddressCacheNegativeTtl(UnknownHostException originalException) { int negativeTtl = 10; try { negativeTtl = Integer.parseInt(Security.getProperty("networkaddress.cache.negative.ttl")); } catch (NumberFormatException exception) { log.warn("Failed to get JVM negative DNS respones cache TTL due to format problem " + "(e.g. this JVM might not have the property). " + "Falling back to default based on Oracle JVM 1.4+ (10s)", exception); } catch (SecurityException exception) { log.warn("Failed to get JVM negative DNS response cache TTL due to security manager. " + "Falling back to default based on Oracle JVM 1.4+ (10s)", exception); } if (negativeTtl == -1) { log.error( "JVM negative DNS repsonse cache TTL is set to 'forever' and host lookup failed. " + "TTL can be changed with security property " + "'networkaddress.cache.negative.ttl', see java.net.InetAddress.", originalException); throw new IllegalArgumentException(originalException); } else if (negativeTtl < 0) { log.warn("JVM specified negative DNS response cache TTL was negative (and not 'forever'). " + "Falling back to default based on Oracle JVM 1.4+ (10s)"); negativeTtl = 10; } return negativeTtl; }  static HostAndPort parseAddress(String address, boolean ignoreMissingPort); static HostAndPort parseAddress(String address, int defaultPort); static int getAddressCacheNegativeTtl(UnknownHostException originalException); }
AddressUtil { public static int getAddressCacheNegativeTtl(UnknownHostException originalException) { int negativeTtl = 10; try { negativeTtl = Integer.parseInt(Security.getProperty("networkaddress.cache.negative.ttl")); } catch (NumberFormatException exception) { log.warn("Failed to get JVM negative DNS respones cache TTL due to format problem " + "(e.g. this JVM might not have the property). " + "Falling back to default based on Oracle JVM 1.4+ (10s)", exception); } catch (SecurityException exception) { log.warn("Failed to get JVM negative DNS response cache TTL due to security manager. " + "Falling back to default based on Oracle JVM 1.4+ (10s)", exception); } if (negativeTtl == -1) { log.error( "JVM negative DNS repsonse cache TTL is set to 'forever' and host lookup failed. " + "TTL can be changed with security property " + "'networkaddress.cache.negative.ttl', see java.net.InetAddress.", originalException); throw new IllegalArgumentException(originalException); } else if (negativeTtl < 0) { log.warn("JVM specified negative DNS response cache TTL was negative (and not 'forever'). " + "Falling back to default based on Oracle JVM 1.4+ (10s)"); negativeTtl = 10; } return negativeTtl; }  static HostAndPort parseAddress(String address, boolean ignoreMissingPort); static HostAndPort parseAddress(String address, int defaultPort); static int getAddressCacheNegativeTtl(UnknownHostException originalException);  }