src/java.base/share/classes/sun/net/www/http/KeepAliveCache.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 36680 jdk/src/java.base/share/classes/sun/net/www/http/KeepAliveCache.java@c9fe8f8e7e6f
child 47361 74e1913a98c0
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
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8167
diff changeset
     2
 * Copyright (c) 1996, 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: 3865
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: 3865
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: 3865
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3865
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3865
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.www.http;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.NotSerializableException;
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 715
diff changeset
    30
import java.util.ArrayList;
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 715
diff changeset
    31
import java.util.HashMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URL;
35786
9d26db88aa14 8149653: Move sun.misc.InnocuousThread to jdk.internal.misc
chegar
parents: 29986
diff changeset
    33
import jdk.internal.misc.InnocuousThread;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * A class that implements a cache of idle Http connections for keep-alive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author Stephen R. Pietrowicz (NCSA)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Dave Brown
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    41
public class KeepAliveCache
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 715
diff changeset
    42
    extends HashMap<KeepAliveKey, ClientVector>
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    43
    implements Runnable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final long serialVersionUID = -2937172892064557949L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /* maximum # keep-alive connections to maintain at once
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * This should be 2 by the HTTP spec, but because we don't support pipe-lining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * a larger value is more appropriate. So we now set a default of 5, and the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * refers to the number of idle connections per destination (in the cache) only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * It can be reset by setting system property "http.maxConnections".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static final int MAX_CONNECTIONS = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static int result = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static int getMaxConnections() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (result == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            result = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                new sun.security.action.GetIntegerAction("http.maxConnections",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                                         MAX_CONNECTIONS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                .intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (result <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                result = MAX_CONNECTIONS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static final int LIFETIME = 5000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private Thread keepAliveTimer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public KeepAliveCache() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Register this URL and HttpClient (that supports keep-alive) with the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param url  The URL contains info about the host and port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param http The HttpClient to be cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public synchronized void put(final URL url, Object obj, HttpClient http) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        boolean startThread = (keepAliveTimer == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (!startThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (!keepAliveTimer.isAlive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                startThread = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (startThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            /* Unfortunately, we can't always believe the keep-alive timeout we got
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
             * back from the server.  If I'm connected through a Netscape proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
             * to a server that sent me a keep-alive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
             * time of 15 sec, the proxy unilaterally terminates my connection
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    93
             * The robustness to get around this is in HttpClient.parseHTTP()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            final KeepAliveCache cache = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            java.security.AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29920
diff changeset
    97
                new java.security.PrivilegedAction<>() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    98
                public Void run() {
36680
c9fe8f8e7e6f 8151571: InnocuousThread cannot be created during early startup
mchung
parents: 35786
diff changeset
    99
                    keepAliveTimer = InnocuousThread.newSystemThread("Keep-Alive-Timer", cache);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    keepAliveTimer.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    keepAliveTimer.setPriority(Thread.MAX_PRIORITY - 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    keepAliveTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        KeepAliveKey key = new KeepAliveKey(url, obj);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   109
        ClientVector v = super.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (v == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            int keepAliveTimeout = http.getKeepAliveTimeout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            v = new ClientVector(keepAliveTimeout > 0?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                 keepAliveTimeout*1000 : LIFETIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            v.put(http);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            super.put(key, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            v.put(http);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   122
    /* remove an obsolete HttpClient from its VectorCache */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public synchronized void remove (HttpClient h, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        KeepAliveKey key = new KeepAliveKey(h.url, obj);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   125
        ClientVector v = super.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            v.remove(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (v.empty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                removeVector(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   134
    /* called by a clientVector thread when all its connections have timed out
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * and that vector of connections should be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    synchronized void removeVector(KeepAliveKey k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        super.remove(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Check to see if this URL has a cached HttpClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   144
    public synchronized HttpClient get(URL url, Object obj) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        KeepAliveKey key = new KeepAliveKey(url, obj);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   147
        ClientVector v = super.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (v == null) { // nothing in cache yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return v.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /* Sleeps for an alloted timeout, then checks for timed out connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Errs on the side of caution (leave connections idle for a relatively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * short time).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 715
diff changeset
   158
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                Thread.sleep(LIFETIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            } catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                /* Remove all unused HttpClients.  Starting from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                 * bottom of the stack (the least-recently used first).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                 * REMIND: It'd be nice to not remove *all* connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                 * that aren't presently in use.  One could have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                 * a second ago that's still perfectly valid, and we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                 * needlessly axing it.  But it's not clear how to do this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                 * cleanly, and doing it right may be more trouble than it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                 * worth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                long currentTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   177
                ArrayList<KeepAliveKey> keysToRemove
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29920
diff changeset
   178
                    = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   180
                for (KeepAliveKey key : keySet()) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   181
                    ClientVector v = get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    synchronized (v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        for (i = 0; i < v.size(); i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   186
                            KeepAliveEntry e = v.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                            if ((currentTime - e.idleStartTime) > v.nap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                HttpClient h = e.hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                h.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        v.subList(0, i).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        if (v.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                            keysToRemove.add(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   201
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   202
                for (KeepAliveKey key : keysToRemove) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   203
                    removeVector(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } while (size() > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Do not serialize this class!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private void writeObject(java.io.ObjectOutputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        throw new NotSerializableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private void readObject(java.io.ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        throw new NotSerializableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
/* FILO order for recycling HttpClients, should run in a thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * to time them out.  If > maxConns are in use, block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   230
class ClientVector extends java.util.Stack<KeepAliveEntry> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private static final long serialVersionUID = -8680532108106489459L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    // sleep time in milliseconds, before cache clear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    int nap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    ClientVector (int nap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        this.nap = nap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    synchronized HttpClient get() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (empty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            // Loop until we find a connection that has not timed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            HttpClient hc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            long currentTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            do {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   250
                KeepAliveEntry e = pop();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                if ((currentTime - e.idleStartTime) > nap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    e.hc.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    hc = e.hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            } while ((hc== null) && (!empty()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /* return a still valid, unused HttpClient */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    synchronized void put(HttpClient h) {
7989
ec76fe03fdd4 7010903: impl. of http.maxConnections is different from the description in JavaSE document
chegar
parents: 5506
diff changeset
   263
        if (size() >= KeepAliveCache.getMaxConnections()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            h.closeServer(); // otherwise the connection remains in limbo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            push(new KeepAliveEntry(h, System.currentTimeMillis()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Do not serialize this class!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private void writeObject(java.io.ObjectOutputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        throw new NotSerializableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private void readObject(java.io.ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        throw new NotSerializableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
class KeepAliveKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private String      protocol = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private String      host = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private int         port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private Object      obj = null; // additional key, such as socketfactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param url the URL containing the protocol, host and port information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public KeepAliveKey(URL url, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        this.protocol = url.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        this.host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        this.port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        this.obj = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Determine whether or not two objects of this type are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 715
diff changeset
   306
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if ((obj instanceof KeepAliveKey) == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        KeepAliveKey kae = (KeepAliveKey)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return host.equals(kae.host)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            && (port == kae.port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            && protocol.equals(kae.protocol)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            && this.obj == kae.obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * The hashCode() for this object is the string hashCode() of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * concatenation of the protocol, host name and port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 715
diff changeset
   321
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        String str = protocol+host+port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return this.obj == null? str.hashCode() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            str.hashCode() + this.obj.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
class KeepAliveEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    HttpClient hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    long idleStartTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    KeepAliveEntry(HttpClient hc, long idleStartTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        this.hc = hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        this.idleStartTime = idleStartTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
}