jdk/src/share/classes/sun/net/InetAddressCachePolicy.java
author juh
Wed, 10 Jul 2013 18:01:22 -0700 (2013-07-11)
changeset 18800 e7fa560afcfb
parent 7668 d4a77089c587
child 24041 2bb2c08c958b
permissions -rw-r--r--
8020318: Fix doclint issues in java.net Reviewed-by: darcy, khazra
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7023
diff changeset
     2
 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
public final class InetAddressCachePolicy {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    // Controls the cache policy for successful lookups only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private static final String cachePolicyProp = "networkaddress.cache.ttl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private static final String cachePolicyPropFallback =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        "sun.net.inetaddr.ttl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    // Controls the cache policy for negative lookups only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static final String negativeCachePolicyProp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        "networkaddress.cache.negative.ttl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static final String negativeCachePolicyPropFallback =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        "sun.net.inetaddr.negative.ttl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static final int FOREVER = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static final int NEVER = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /* default value for positive lookups */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static final int DEFAULT_POSITIVE = 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /* The Java-level namelookup cache policy for successful lookups:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * -1: caching forever
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * any positive value: the number of seconds to cache an address for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * default value is forever (FOREVER), as we let the platform do the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * caching. For security reasons, this caching is made forever when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * a security manager is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
7023
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
    59
    private static int cachePolicy = FOREVER;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /* The Java-level namelookup cache policy for negative lookups:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * -1: caching forever
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * any positive value: the number of seconds to cache an address for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * default value is 0. It can be set to some other value for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * performance reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
7023
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
    69
    private static int negativeCachePolicy = NEVER;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Whether or not the cache policy for successful lookups was set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * using a property (cmd line).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
7023
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
    75
    private static boolean propertySet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Whether or not the cache policy for negative lookups was set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * using a property (cmd line).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
7023
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
    81
    private static boolean propertyNegativeSet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Integer tmp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            tmp = new Integer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
              java.security.AccessController.doPrivileged (
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                new PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                  public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                      return Security.getProperty(cachePolicyProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
              }));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (tmp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            cachePolicy = tmp.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (cachePolicy < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                cachePolicy = FOREVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
7023
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
   105
            propertySet = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            tmp = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                (new sun.security.action.GetIntegerAction(cachePolicyPropFallback));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (tmp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                cachePolicy = tmp.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                if (cachePolicy < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    cachePolicy = FOREVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                }
7023
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
   114
                propertySet = true;
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
   115
            } else {
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
   116
                /* No properties defined for positive caching. If there is no
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
   117
                 * security manager then use the default positive cache value.
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
   118
                 */
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
   119
                if (System.getSecurityManager() == null) {
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
   120
                    cachePolicy = DEFAULT_POSITIVE;
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
   121
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            tmp = new Integer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
              java.security.AccessController.doPrivileged (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                new PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                  public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                      return Security.getProperty(negativeCachePolicyProp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
              }));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (tmp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            negativeCachePolicy = tmp.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (negativeCachePolicy < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                negativeCachePolicy = FOREVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
7023
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
   142
            propertyNegativeSet = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            tmp = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                (new sun.security.action.GetIntegerAction(negativeCachePolicyPropFallback));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (tmp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                negativeCachePolicy = tmp.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                if (negativeCachePolicy < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    negativeCachePolicy = FOREVER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                }
7023
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
   151
                propertyNegativeSet = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public static synchronized int get() {
7023
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
   157
        return cachePolicy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public static synchronized int getNegative() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return negativeCachePolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Sets the cache policy for successful lookups if the user has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * already specified a cache policy for it using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * command-property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param newPolicy the value in seconds for how long the lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * should be cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public static synchronized void setIfNotSet(int newPolicy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         * When setting the new value we may want to signal that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         * cache should be flushed, though this doesn't seem strictly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         * necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         */
7023
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
   177
        if (!propertySet) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            checkValue(newPolicy, cachePolicy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            cachePolicy = newPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Sets the cache policy for negative lookups if the user has not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * already specified a cache policy for it using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * command-property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param newPolicy the value in seconds for how long the lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * should be cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public static synchronized void setNegativeIfNotSet(int newPolicy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         * When setting the new value we may want to signal that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
         * cache should be flushed, though this doesn't seem strictly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         * necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         */
7023
9509e6a18f6e 6992859: InetAddressCachePolicy.setIfNotSet() fails
chegar
parents: 5506
diff changeset
   196
        if (!propertyNegativeSet) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            // Negative caching does not seem to have any security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // implications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            // checkValue(newPolicy, negativeCachePolicy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            negativeCachePolicy = newPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private static void checkValue(int newPolicy, int oldPolicy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
         * If malicious code gets a hold of this method, prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         * setting the cache policy to something laxer or some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         * invalid negative value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (newPolicy == FOREVER)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if ((oldPolicy == FOREVER) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            (newPolicy < oldPolicy) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            (newPolicy < FOREVER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                SecurityException("can't make InetAddress cache more lax");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
}