Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/net/InetAddressCachePolicy.java
38829 views
/*1* Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net;2627import java.security.PrivilegedAction;28import java.security.Security;2930public final class InetAddressCachePolicy {3132// Controls the cache policy for successful lookups only33private static final String cachePolicyProp = "networkaddress.cache.ttl";34private static final String cachePolicyPropFallback =35"sun.net.inetaddr.ttl";3637// Controls the cache policy for negative lookups only38private static final String negativeCachePolicyProp =39"networkaddress.cache.negative.ttl";40private static final String negativeCachePolicyPropFallback =41"sun.net.inetaddr.negative.ttl";4243public static final int FOREVER = -1;44public static final int NEVER = 0;4546/* default value for positive lookups */47public static final int DEFAULT_POSITIVE = 30;4849/* The Java-level namelookup cache policy for successful lookups:50*51* -1: caching forever52* any positive value: the number of seconds to cache an address for53*54* default value is forever (FOREVER), as we let the platform do the55* caching. For security reasons, this caching is made forever when56* a security manager is set.57*/58private static int cachePolicy = FOREVER;5960/* The Java-level namelookup cache policy for negative lookups:61*62* -1: caching forever63* any positive value: the number of seconds to cache an address for64*65* default value is 0. It can be set to some other value for66* performance reasons.67*/68private static int negativeCachePolicy = NEVER;6970/*71* Whether or not the cache policy for successful lookups was set72* using a property (cmd line).73*/74private static boolean propertySet;7576/*77* Whether or not the cache policy for negative lookups was set78* using a property (cmd line).79*/80private static boolean propertyNegativeSet;8182/*83* Initialize84*/85static {8687Integer tmp = java.security.AccessController.doPrivileged(88new PrivilegedAction<Integer>() {89public Integer run() {90try {91String tmpString = Security.getProperty(cachePolicyProp);92if (tmpString != null) {93return Integer.valueOf(tmpString);94}95} catch (NumberFormatException ignored) {96// Ignore97}9899try {100String tmpString = System.getProperty(cachePolicyPropFallback);101if (tmpString != null) {102return Integer.decode(tmpString);103}104} catch (NumberFormatException ignored) {105// Ignore106}107return null;108}109});110111if (tmp != null) {112cachePolicy = tmp.intValue();113if (cachePolicy < 0) {114cachePolicy = FOREVER;115}116propertySet = true;117} else {118/* No properties defined for positive caching. If there is no119* security manager then use the default positive cache value.120*/121if (System.getSecurityManager() == null) {122cachePolicy = DEFAULT_POSITIVE;123}124}125tmp = java.security.AccessController.doPrivileged (126new PrivilegedAction<Integer>() {127public Integer run() {128try {129String tmpString = Security.getProperty(negativeCachePolicyProp);130if (tmpString != null) {131return Integer.valueOf(tmpString);132}133} catch (NumberFormatException ignored) {134// Ignore135}136137try {138String tmpString = System.getProperty(negativeCachePolicyPropFallback);139if (tmpString != null) {140return Integer.decode(tmpString);141}142} catch (NumberFormatException ignored) {143// Ignore144}145return null;146}147});148149if (tmp != null) {150negativeCachePolicy = tmp.intValue();151if (negativeCachePolicy < 0) {152negativeCachePolicy = FOREVER;153}154propertyNegativeSet = true;155}156}157158public static synchronized int get() {159return cachePolicy;160}161162public static synchronized int getNegative() {163return negativeCachePolicy;164}165166/**167* Sets the cache policy for successful lookups if the user has not168* already specified a cache policy for it using a169* command-property.170* @param newPolicy the value in seconds for how long the lookup171* should be cached172*/173public static synchronized void setIfNotSet(int newPolicy) {174/*175* When setting the new value we may want to signal that the176* cache should be flushed, though this doesn't seem strictly177* necessary.178*/179if (!propertySet) {180checkValue(newPolicy, cachePolicy);181cachePolicy = newPolicy;182}183}184185/**186* Sets the cache policy for negative lookups if the user has not187* already specified a cache policy for it using a188* command-property.189* @param newPolicy the value in seconds for how long the lookup190* should be cached191*/192public static synchronized void setNegativeIfNotSet(int newPolicy) {193/*194* When setting the new value we may want to signal that the195* cache should be flushed, though this doesn't seem strictly196* necessary.197*/198if (!propertyNegativeSet) {199// Negative caching does not seem to have any security200// implications.201// checkValue(newPolicy, negativeCachePolicy);202negativeCachePolicy = newPolicy;203}204}205206private static void checkValue(int newPolicy, int oldPolicy) {207/*208* If malicious code gets a hold of this method, prevent209* setting the cache policy to something laxer or some210* invalid negative value.211*/212if (newPolicy == FOREVER)213return;214215if ((oldPolicy == FOREVER) ||216(newPolicy < oldPolicy) ||217(newPolicy < FOREVER)) {218219throw new220SecurityException("can't make InetAddress cache more lax");221}222}223}224225226