jdk/src/java.base/share/classes/sun/security/util/Cache.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 25859 3317bb8137f4
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 2002, 2011, 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: 2060
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: 2060
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: 2060
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2060
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2060
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.security.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.ref.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Abstract base class and factory for caches. A cache is a key-value mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * It has properties that make it more suitable for caching than a Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The factory methods can be used to obtain two different implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * They have the following properties:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *  . keys and values reside in memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *  . keys and values must be non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *  . maximum size. Replacements are made in LRU order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *  . optional lifetime, specified in seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
    46
 *  . safe for concurrent use by multiple threads
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *  . values are held by either standard references or via SoftReferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *    SoftReferences have the advantage that they are automatically cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *    by the garbage collector in response to memory demand. This makes it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *    possible to simple set the maximum size to a very large value and let
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *    the GC automatically size the cache dynamically depending on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *    amount of available memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * However, note that because of the way SoftReferences are implemented in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * HotSpot at the moment, this may not work perfectly as it clears them fairly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * eagerly. Performance may be improved if the Java heap size is set to larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * value using e.g. java -ms64M -mx128M foo.Test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Cache sizing: the memory cache is implemented on top of a LinkedHashMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * In its current implementation, the number of buckets (NOT entries) in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * (Linked)HashMaps is always a power of two. It is recommended to set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * maximum cache size to value that uses those buckets fully. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * if a cache with somewhere between 500 and 1000 entries is desired, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * maximum size of 750 would be a good choice: try 1024 buckets, with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * load factor of 0.75f, the number of entries can be calculated as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * buckets / 4 * 3. As mentioned above, with a SoftReference cache, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * generally reasonable to set the size to a fairly large value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 */
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
    72
public abstract class Cache<K,V> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    protected Cache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Return the number of currently valid entries in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public abstract int size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Remove all entries from the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public abstract void clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Add an entry to the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
    91
    public abstract void put(K key, V value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Get a value from the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
    96
    public abstract V get(Object key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Remove an entry from the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public abstract void remove(Object key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   104
     * Set the maximum size.
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   105
     */
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   106
    public abstract void setCapacity(int size);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   107
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   108
    /**
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   109
     * Set the timeout(in seconds).
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   110
     */
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   111
    public abstract void setTimeout(int timeout);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   112
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   113
    /**
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   114
     * accept a visitor
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   115
     */
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   116
    public abstract void accept(CacheVisitor<K,V> visitor);
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   117
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   118
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Return a new memory cache with the specified maximum size, unlimited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * lifetime for entries, with the values held by SoftReferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   122
    public static <K,V> Cache<K,V> newSoftMemoryCache(int size) {
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   123
        return new MemoryCache<>(true, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Return a new memory cache with the specified maximum size, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * specified maximum lifetime (in seconds), with the values held
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * by SoftReferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   131
    public static <K,V> Cache<K,V> newSoftMemoryCache(int size, int timeout) {
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   132
        return new MemoryCache<>(true, size, timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Return a new memory cache with the specified maximum size, unlimited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * lifetime for entries, with the values held by standard references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   139
    public static <K,V> Cache<K,V> newHardMemoryCache(int size) {
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   140
        return new MemoryCache<>(false, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Return a dummy cache that does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   146
    @SuppressWarnings("unchecked")
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   147
    public static <K,V> Cache<K,V> newNullCache() {
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   148
        return (Cache<K,V>) NullCache.INSTANCE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Return a new memory cache with the specified maximum size, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * specified maximum lifetime (in seconds), with the values held
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * by standard references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   156
    public static <K,V> Cache<K,V> newHardMemoryCache(int size, int timeout) {
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   157
        return new MemoryCache<>(false, size, timeout);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Utility class that wraps a byte array and implements the equals()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * and hashCode() contract in a way suitable for Maps and caches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public static class EqualByteArray {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        private final byte[] b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        private volatile int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        public EqualByteArray(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            this.b = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            int h = hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (h == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                h = b.length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                for (int i = 0; i < b.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    h += (b[i] & 0xff) * 37;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                hash = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (obj instanceof EqualByteArray == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            EqualByteArray other = (EqualByteArray)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return Arrays.equals(this.b, other.b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   197
    public interface CacheVisitor<K,V> {
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   198
        public void visit(Map<K,V> map);
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   199
    }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   200
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   203
class NullCache<K,V> extends Cache<K,V> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   205
    static final Cache<Object,Object> INSTANCE = new NullCache<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private NullCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   219
    public void put(K key, V value) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   223
    public V get(Object key) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public void remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   231
    public void setCapacity(int size) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   232
        // empty
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   233
    }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   234
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   235
    public void setTimeout(int timeout) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   236
        // empty
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   237
    }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   238
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   239
    public void accept(CacheVisitor<K,V> visitor) {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   240
        // empty
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   241
    }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   242
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   245
class MemoryCache<K,V> extends Cache<K,V> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   247
    private static final float LOAD_FACTOR = 0.75f;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    // XXXX
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   250
    private static final boolean DEBUG = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   252
    private final Map<K, CacheEntry<K,V>> cacheMap;
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   253
    private int maxSize;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   254
    private long lifetime;
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   255
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   256
    // ReferenceQueue is of type V instead of Cache<K,V>
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   257
    // to allow SoftCacheEntry to extend SoftReference<V>
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   258
    private final ReferenceQueue<V> queue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public MemoryCache(boolean soft, int maxSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        this(soft, maxSize, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public MemoryCache(boolean soft, int maxSize, int lifetime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        this.maxSize = maxSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        this.lifetime = lifetime * 1000;
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   267
        if (soft)
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   268
            this.queue = new ReferenceQueue<>();
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   269
        else
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   270
            this.queue = null;
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   271
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        int buckets = (int)(maxSize / LOAD_FACTOR) + 1;
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   273
        cacheMap = new LinkedHashMap<>(buckets, LOAD_FACTOR, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Empty the reference queue and remove all corresponding entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * from the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * This method should be called at the beginning of each public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private void emptyQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (queue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        int startSize = cacheMap.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        while (true) {
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   289
            @SuppressWarnings("unchecked")
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   290
            CacheEntry<K,V> entry = (CacheEntry<K,V>)queue.poll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   294
            K key = entry.getKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                // key is null, entry has already been removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   299
            CacheEntry<K,V> currentEntry = cacheMap.remove(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            // check if the entry in the map corresponds to the expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            // entry. If not, readd the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if ((currentEntry != null) && (entry != currentEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                cacheMap.put(key, currentEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            int endSize = cacheMap.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            if (startSize != endSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                System.out.println("*** Expunged " + (startSize - endSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        + " entries, " + endSize + " entries left");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Scan all entries and remove all expired ones.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private void expungeExpiredEntries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        emptyQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (lifetime == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        int cnt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        long time = System.currentTimeMillis();
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   325
        for (Iterator<CacheEntry<K,V>> t = cacheMap.values().iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                t.hasNext(); ) {
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   327
            CacheEntry<K,V> entry = t.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            if (entry.isValid(time) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                t.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                cnt++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (cnt != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                System.out.println("Removed " + cnt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        + " expired entries, remaining " + cacheMap.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public synchronized int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        expungeExpiredEntries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return cacheMap.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public synchronized void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (queue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            // if this is a SoftReference cache, first invalidate() all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            // entries so that GC does not have to enqueue them
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   350
            for (CacheEntry<K,V> entry : cacheMap.values()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                entry.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            while (queue.poll() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        cacheMap.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   360
    public synchronized void put(K key, V value) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        emptyQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        long expirationTime = (lifetime == 0) ? 0 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                        System.currentTimeMillis() + lifetime;
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   364
        CacheEntry<K,V> newEntry = newEntry(key, value, expirationTime, queue);
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   365
        CacheEntry<K,V> oldEntry = cacheMap.put(key, newEntry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (oldEntry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            oldEntry.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   370
        if (maxSize > 0 && cacheMap.size() > maxSize) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            expungeExpiredEntries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (cacheMap.size() > maxSize) { // still too large?
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   373
                Iterator<CacheEntry<K,V>> t = cacheMap.values().iterator();
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   374
                CacheEntry<K,V> lruEntry = t.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    System.out.println("** Overflow removal "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        + lruEntry.getKey() + " | " + lruEntry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                t.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                lruEntry.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   385
    public synchronized V get(Object key) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        emptyQueue();
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   387
        CacheEntry<K,V> entry = cacheMap.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (entry == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        long time = (lifetime == 0) ? 0 : System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (entry.isValid(time) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                System.out.println("Ignoring expired entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            cacheMap.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        return entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public synchronized void remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        emptyQueue();
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   404
        CacheEntry<K,V> entry = cacheMap.remove(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            entry.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   410
    public synchronized void setCapacity(int size) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   411
        expungeExpiredEntries();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   412
        if (size > 0 && cacheMap.size() > size) {
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   413
            Iterator<CacheEntry<K,V>> t = cacheMap.values().iterator();
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   414
            for (int i = cacheMap.size() - size; i > 0; i--) {
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   415
                CacheEntry<K,V> lruEntry = t.next();
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   416
                if (DEBUG) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   417
                    System.out.println("** capacity reset removal "
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   418
                        + lruEntry.getKey() + " | " + lruEntry.getValue());
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   419
                }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   420
                t.remove();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   421
                lruEntry.invalidate();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   422
            }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   423
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   424
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   425
        maxSize = size > 0 ? size : 0;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   426
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   427
        if (DEBUG) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   428
            System.out.println("** capacity reset to " + size);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   429
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   430
    }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   431
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   432
    public synchronized void setTimeout(int timeout) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   433
        emptyQueue();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   434
        lifetime = timeout > 0 ? timeout * 1000L : 0L;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   435
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   436
        if (DEBUG) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   437
            System.out.println("** lifetime reset to " + timeout);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   438
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   439
    }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   440
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   441
    // it is a heavyweight method.
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   442
    public synchronized void accept(CacheVisitor<K,V> visitor) {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   443
        expungeExpiredEntries();
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   444
        Map<K,V> cached = getCachedEntries();
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   445
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   446
        visitor.visit(cached);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   447
    }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   448
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   449
    private Map<K,V> getCachedEntries() {
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   450
        Map<K,V> kvmap = new HashMap<>(cacheMap.size());
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   451
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   452
        for (CacheEntry<K,V> entry : cacheMap.values()) {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   453
            kvmap.put(entry.getKey(), entry.getValue());
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   454
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   455
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   456
        return kvmap;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   457
    }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   458
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   459
    protected CacheEntry<K,V> newEntry(K key, V value,
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   460
            long expirationTime, ReferenceQueue<V> queue) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if (queue != null) {
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   462
            return new SoftCacheEntry<>(key, value, expirationTime, queue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        } else {
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   464
            return new HardCacheEntry<>(key, value, expirationTime);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   468
    private static interface CacheEntry<K,V> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        boolean isValid(long currentTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        void invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   474
        K getKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   476
        V getValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   480
    private static class HardCacheEntry<K,V> implements CacheEntry<K,V> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   482
        private K key;
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   483
        private V value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        private long expirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   486
        HardCacheEntry(K key, V value, long expirationTime) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            this.expirationTime = expirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   492
        public K getKey() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   496
        public V getValue() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        public boolean isValid(long currentTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            boolean valid = (currentTime <= expirationTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            if (valid == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            return valid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        public void invalidate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            expirationTime = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   515
    private static class SoftCacheEntry<K,V>
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   516
            extends SoftReference<V>
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   517
            implements CacheEntry<K,V> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   519
        private K key;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        private long expirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   522
        SoftCacheEntry(K key, V value, long expirationTime,
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   523
                ReferenceQueue<V> queue) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            super(value, queue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            this.expirationTime = expirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   529
        public K getKey() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10336
diff changeset
   533
        public V getValue() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            return get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        public boolean isValid(long currentTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            boolean valid = (currentTime <= expirationTime) && (get() != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            if (valid == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            return valid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        public void invalidate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            expirationTime = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
}