src/java.base/share/classes/sun/security/ssl/SSLSessionContextImpl.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/java.base/share/classes/sun/security/ssl/SSLSessionContextImpl.java@3317bb8137f4
child 50768 68fa3d4026ea
child 56542 56aaa6cb3693
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 14665
diff changeset
     2
 * Copyright (c) 1999, 2012, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Vector;
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
    31
import java.util.Locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.net.ssl.SSLSession;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.net.ssl.SSLSessionContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    36
import sun.security.util.Cache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    39
final class SSLSessionContextImpl implements SSLSessionContext {
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
    40
    private Cache<SessionId, SSLSessionImpl> sessionCache;
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
    41
                                        // session cache, session id as key
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
    42
    private Cache<String, SSLSessionImpl> sessionHostPortCache;
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
    43
                                        // session cache, "host:port" as key
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    44
    private int cacheLimit;             // the max cache size
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    45
    private int timeout;                // timeout in seconds
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    46
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    47
    // package private
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    48
    SSLSessionContextImpl() {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    49
        cacheLimit = getDefaultCacheLimit();    // default cache size
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    50
        timeout = 86400;                        // default, 24 hours
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    51
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    52
        // use soft reference
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    53
        sessionCache = Cache.newSoftMemoryCache(cacheLimit, timeout);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    54
        sessionHostPortCache = Cache.newSoftMemoryCache(cacheLimit, timeout);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    58
     * Returns the <code>SSLSession</code> bound to the specified session id.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10785
diff changeset
    60
    @Override
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    61
    public SSLSession getSession(byte[] sessionId) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    62
        if (sessionId == null) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    63
            throw new NullPointerException("session id cannot be null");
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    64
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    65
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
    66
        SSLSessionImpl sess = sessionCache.get(new SessionId(sessionId));
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    67
        if (!isTimedout(sess)) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    68
            return sess;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    69
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    70
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    71
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Returns an enumeration of the active SSL sessions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10785
diff changeset
    77
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public Enumeration<byte[]> getIds() {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    79
        SessionCacheVisitor scVisitor = new SessionCacheVisitor();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    80
        sessionCache.accept(scVisitor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    82
        return scVisitor.getSessionIds();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    85
    /**
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    86
     * Sets the timeout limit for cached <code>SSLSession</code> objects
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    87
     *
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    88
     * Note that after reset the timeout, the cached session before
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    89
     * should be timed within the shorter one of the old timeout and the
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    90
     * new timeout.
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    91
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10785
diff changeset
    92
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public void setSessionTimeout(int seconds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                 throws IllegalArgumentException {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    95
        if (seconds < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new IllegalArgumentException();
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    97
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    98
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
    99
        if (timeout != seconds) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   100
            sessionCache.setTimeout(seconds);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   101
            sessionHostPortCache.setTimeout(seconds);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   102
            timeout = seconds;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   103
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   106
    /**
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   107
     * Gets the timeout limit for cached <code>SSLSession</code> objects
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   108
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10785
diff changeset
   109
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public int getSessionTimeout() {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   111
        return timeout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   114
    /**
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   115
     * Sets the size of the cache used for storing
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   116
     * <code>SSLSession</code> objects.
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   117
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10785
diff changeset
   118
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public void setSessionCacheSize(int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                 throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (size < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   124
        if (cacheLimit != size) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   125
            sessionCache.setCapacity(size);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   126
            sessionHostPortCache.setCapacity(size);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   127
            cacheLimit = size;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   128
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   131
    /**
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   132
     * Gets the size of the cache used for storing
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   133
     * <code>SSLSession</code> objects.
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   134
     */
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10785
diff changeset
   135
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public int getSessionCacheSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return cacheLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   140
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   141
    // package-private method, used ONLY by ServerHandshaker
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    SSLSessionImpl get(byte[] id) {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   143
        return (SSLSessionImpl)getSession(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   146
    // package-private method, used ONLY by ClientHandshaker
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    SSLSessionImpl get(String hostname, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
         * If no session caching info is available, we won't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         * get one, so exit before doing a lookup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (hostname == null && port == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   155
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
   156
        SSLSessionImpl sess = sessionHostPortCache.get(getKey(hostname, port));
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   157
        if (!isTimedout(sess)) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   158
            return sess;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   159
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   160
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   161
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private String getKey(String hostname, int port) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   165
        return (hostname + ":" +
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 5506
diff changeset
   166
            String.valueOf(port)).toLowerCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   169
    // cache a SSLSession
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   170
    //
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   171
    // In SunJSSE implementation, a session is created while getting a
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   172
    // client hello or a server hello message, and cached while the
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   173
    // handshaking finished.
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   174
    // Here we time the session from the time it cached instead of the
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   175
    // time it created, which is a little longer than the expected. So
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   176
    // please do check isTimedout() while getting entry from the cache.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    void put(SSLSessionImpl s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        sessionCache.put(s.getSessionId(), s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   180
        // If no hostname/port info is available, don't add this one.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if ((s.getPeerHost() != null) && (s.getPeerPort() != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            sessionHostPortCache.put(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                getKey(s.getPeerHost(), s.getPeerPort()), s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   185
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        s.setContext(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   189
    // package-private method, remove a cached SSLSession
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   190
    void remove(SessionId key) {
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
   191
        SSLSessionImpl s = sessionCache.get(key);
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   192
        if (s != null) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   193
            sessionCache.remove(key);
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   194
            sessionHostPortCache.remove(
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   195
                        getKey(s.getPeerHost(), s.getPeerPort()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   199
    private int getDefaultCacheLimit() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        int cacheLimit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        String s = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                new java.security.PrivilegedAction<String>() {
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10785
diff changeset
   204
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    return System.getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        "javax.net.ssl.sessionCacheSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            cacheLimit = (s != null) ? Integer.valueOf(s).intValue() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return (cacheLimit > 0) ? cacheLimit : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   217
    boolean isTimedout(SSLSession sess) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   218
        if (timeout == 0) {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   219
            return false;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   220
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   221
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   222
        if ((sess != null) && ((sess.getCreationTime() + timeout * 1000L)
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   223
                                        <= (System.currentTimeMillis()))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            sess.invalidate();
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   225
            return true;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   226
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   227
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   228
        return false;
2
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
    final class SessionCacheVisitor
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
   232
            implements Cache.CacheVisitor<SessionId, SSLSessionImpl> {
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   233
        Vector<byte[]> ids = null;
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   234
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
   235
        // public void visit(java.util.Map<K,V> map) {}
14664
e71aa0962e70 8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents: 10785
diff changeset
   236
        @Override
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
   237
        public void visit(java.util.Map<SessionId, SSLSessionImpl> map) {
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
   238
            ids = new Vector<>(map.size());
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   239
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
   240
            for (SessionId key : map.keySet()) {
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
   241
                SSLSessionImpl value = map.get(key);
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   242
                if (!isTimedout(value)) {
10785
1d42311b6355 7092897: sun.security.util.Cache should be generified
mullan
parents: 10369
diff changeset
   243
                    ids.addElement(key.getId());
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   244
                }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   245
            }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   246
        }
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   247
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   248
        public Enumeration<byte[]> getSessionIds() {
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   249
            return  ids != null ? ids.elements() :
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   250
                                  new Vector<byte[]>().elements();
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   251
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
2060
75e464ce81af 4918870: Examine session cache implementation (sun.misc.Cache)
xuelei
parents: 2
diff changeset
   253
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
}