0N/A/*
1879N/A * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
1472N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1472N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
1879N/A
1879N/Apackage sun.net;
1879N/A
1879N/Aimport java.security.PrivilegedAction;
1879N/Aimport java.security.Security;
0N/A
0N/Apublic final class InetAddressCachePolicy {
0N/A
0N/A // Controls the cache policy for successful lookups only
0N/A private static final String cachePolicyProp = "networkaddress.cache.ttl";
0N/A private static final String cachePolicyPropFallback =
0N/A "sun.net.inetaddr.ttl";
0N/A
0N/A // Controls the cache policy for negative lookups only
0N/A private static final String negativeCachePolicyProp =
0N/A "networkaddress.cache.negative.ttl";
0N/A private static final String negativeCachePolicyPropFallback =
0N/A "sun.net.inetaddr.negative.ttl";
0N/A
0N/A public static final int FOREVER = -1;
0N/A public static final int NEVER = 0;
0N/A
0N/A /* default value for positive lookups */
0N/A public static final int DEFAULT_POSITIVE = 30;
0N/A
0N/A /* The Java-level namelookup cache policy for successful lookups:
0N/A *
0N/A * -1: caching forever
0N/A * any positive value: the number of seconds to cache an address for
0N/A *
0N/A * default value is forever (FOREVER), as we let the platform do the
0N/A * caching. For security reasons, this caching is made forever when
0N/A * a security manager is set.
0N/A */
0N/A private static int cachePolicy = FOREVER;
0N/A
0N/A /* The Java-level namelookup cache policy for negative lookups:
0N/A *
0N/A * -1: caching forever
0N/A * any positive value: the number of seconds to cache an address for
0N/A *
0N/A * default value is 0. It can be set to some other value for
0N/A * performance reasons.
0N/A */
0N/A private static int negativeCachePolicy = NEVER;
0N/A
0N/A /*
0N/A * Whether or not the cache policy for successful lookups was set
0N/A * using a property (cmd line).
0N/A */
0N/A private static boolean propertySet;
0N/A
0N/A /*
0N/A * Whether or not the cache policy for negative lookups was set
0N/A * using a property (cmd line).
0N/A */
0N/A private static boolean propertyNegativeSet;
0N/A
0N/A /*
0N/A * Initialize
0N/A */
0N/A static {
0N/A Integer tmp = null;
0N/A
0N/A try {
0N/A tmp = new Integer(
0N/A java.security.AccessController.doPrivileged (
0N/A new PrivilegedAction<String>() {
0N/A public String run() {
0N/A return Security.getProperty(cachePolicyProp);
0N/A }
0N/A }));
0N/A } catch (NumberFormatException e) {
0N/A // ignore
0N/A }
0N/A if (tmp != null) {
0N/A cachePolicy = tmp.intValue();
0N/A if (cachePolicy < 0) {
0N/A cachePolicy = FOREVER;
0N/A }
0N/A propertySet = true;
0N/A } else {
0N/A tmp = java.security.AccessController.doPrivileged
0N/A (new sun.security.action.GetIntegerAction(cachePolicyPropFallback));
0N/A if (tmp != null) {
0N/A cachePolicy = tmp.intValue();
0N/A if (cachePolicy < 0) {
0N/A cachePolicy = FOREVER;
0N/A }
0N/A propertySet = true;
0N/A } else {
0N/A /* No properties defined for positive caching. If there is no
0N/A * security manager then use the default positive cache value.
0N/A */
0N/A if (System.getSecurityManager() == null) {
0N/A cachePolicy = DEFAULT_POSITIVE;
0N/A }
0N/A }
0N/A }
512N/A
512N/A try {
512N/A tmp = new Integer(
0N/A java.security.AccessController.doPrivileged (
0N/A new PrivilegedAction<String>() {
0N/A public String run() {
0N/A return Security.getProperty(negativeCachePolicyProp);
0N/A }
0N/A }));
0N/A } catch (NumberFormatException e) {
0N/A // ignore
0N/A }
0N/A
0N/A if (tmp != null) {
0N/A negativeCachePolicy = tmp.intValue();
0N/A if (negativeCachePolicy < 0) {
0N/A negativeCachePolicy = FOREVER;
0N/A }
0N/A propertyNegativeSet = true;
0N/A } else {
0N/A tmp = java.security.AccessController.doPrivileged
0N/A (new sun.security.action.GetIntegerAction(negativeCachePolicyPropFallback));
0N/A if (tmp != null) {
0N/A negativeCachePolicy = tmp.intValue();
0N/A if (negativeCachePolicy < 0) {
0N/A negativeCachePolicy = FOREVER;
0N/A }
0N/A propertyNegativeSet = true;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public static synchronized int get() {
0N/A return cachePolicy;
0N/A }
0N/A
0N/A public static synchronized int getNegative() {
0N/A return negativeCachePolicy;
0N/A }
0N/A
0N/A /**
0N/A * Sets the cache policy for successful lookups if the user has not
0N/A * already specified a cache policy for it using a
0N/A * command-property.
0N/A * @param newPolicy the value in seconds for how long the lookup
0N/A * should be cached
0N/A */
0N/A public static synchronized void setIfNotSet(int newPolicy) {
0N/A /*
0N/A * When setting the new value we may want to signal that the
0N/A * cache should be flushed, though this doesn't seem strictly
0N/A * necessary.
0N/A */
0N/A if (!propertySet) {
0N/A checkValue(newPolicy, cachePolicy);
0N/A cachePolicy = newPolicy;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sets the cache policy for negative lookups if the user has not
0N/A * already specified a cache policy for it using a
0N/A * command-property.
0N/A * @param newPolicy the value in seconds for how long the lookup
0N/A * should be cached
0N/A */
0N/A public static synchronized void setNegativeIfNotSet(int newPolicy) {
0N/A /*
0N/A * When setting the new value we may want to signal that the
0N/A * cache should be flushed, though this doesn't seem strictly
0N/A * necessary.
0N/A */
0N/A if (!propertyNegativeSet) {
0N/A // Negative caching does not seem to have any security
0N/A // implications.
0N/A // checkValue(newPolicy, negativeCachePolicy);
0N/A negativeCachePolicy = newPolicy;
0N/A }
0N/A }
0N/A
0N/A private static void checkValue(int newPolicy, int oldPolicy) {
0N/A /*
0N/A * If malicious code gets a hold of this method, prevent
0N/A * setting the cache policy to something laxer or some
0N/A * invalid negative value.
0N/A */
0N/A if (newPolicy == FOREVER)
0N/A return;
0N/A
0N/A if ((oldPolicy == FOREVER) ||
0N/A (newPolicy < oldPolicy) ||
0N/A (newPolicy < FOREVER)) {
0N/A
0N/A throw new
0N/A SecurityException("can't make InetAddress cache more lax");
0N/A }
0N/A }
0N/A}
0N/A