jdk/src/java.base/share/classes/sun/net/www/http/KeepAliveCache.java
author darcy
Thu, 23 Apr 2015 09:32:35 -0700
changeset 29986 97167d851fc4
parent 29920 f81c14f472ab
child 35786 9d26db88aa14
permissions -rw-r--r--
8078467: Update core libraries to use diamond with anonymous classes Reviewed-by: mchung, alanb
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;
29920
f81c14f472ab 8042322: Enhance thread contexts in networking and nio
chegar
parents: 25859
diff changeset
    33
import sun.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() {
29920
f81c14f472ab 8042322: Enhance thread contexts in networking and nio
chegar
parents: 25859
diff changeset
    99
                    keepAliveTimer = new InnocuousThread(cache, "Keep-Alive-Timer");
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);
8167
7599e778785f 7008595: Class loader leak caused by keepAliveTimer thread in KeepAliveCache
chegar
parents: 7989
diff changeset
   102
                    // Set the context class loader to null in order to avoid
7599e778785f 7008595: Class loader leak caused by keepAliveTimer thread in KeepAliveCache
chegar
parents: 7989
diff changeset
   103
                    // keeping a strong reference to an application classloader.
7599e778785f 7008595: Class loader leak caused by keepAliveTimer thread in KeepAliveCache
chegar
parents: 7989
diff changeset
   104
                    keepAliveTimer.setContextClassLoader(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    keepAliveTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        KeepAliveKey key = new KeepAliveKey(url, obj);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   112
        ClientVector v = super.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (v == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            int keepAliveTimeout = http.getKeepAliveTimeout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            v = new ClientVector(keepAliveTimeout > 0?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                 keepAliveTimeout*1000 : LIFETIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            v.put(http);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            super.put(key, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            v.put(http);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   125
    /* remove an obsolete HttpClient from its VectorCache */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public synchronized void remove (HttpClient h, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        KeepAliveKey key = new KeepAliveKey(h.url, obj);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   128
        ClientVector v = super.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            v.remove(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (v.empty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                removeVector(key);
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
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   137
    /* called by a clientVector thread when all its connections have timed out
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * and that vector of connections should be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    synchronized void removeVector(KeepAliveKey k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        super.remove(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Check to see if this URL has a cached HttpClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   147
    public synchronized HttpClient get(URL url, Object obj) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        KeepAliveKey key = new KeepAliveKey(url, obj);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   150
        ClientVector v = super.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (v == null) { // nothing in cache yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return v.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /* Sleeps for an alloted timeout, then checks for timed out connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Errs on the side of caution (leave connections idle for a relatively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * short time).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 715
diff changeset
   161
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                Thread.sleep(LIFETIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            } catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                /* Remove all unused HttpClients.  Starting from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                 * bottom of the stack (the least-recently used first).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                 * REMIND: It'd be nice to not remove *all* connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                 * that aren't presently in use.  One could have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                 * a second ago that's still perfectly valid, and we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                 * needlessly axing it.  But it's not clear how to do this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                 * cleanly, and doing it right may be more trouble than it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                 * worth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                long currentTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   180
                ArrayList<KeepAliveKey> keysToRemove
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29920
diff changeset
   181
                    = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   183
                for (KeepAliveKey key : keySet()) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   184
                    ClientVector v = get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    synchronized (v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        for (i = 0; i < v.size(); i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   189
                            KeepAliveEntry e = v.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                            if ((currentTime - e.idleStartTime) > v.nap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                HttpClient h = e.hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                h.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        v.subList(0, i).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        if (v.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                            keysToRemove.add(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   204
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   205
                for (KeepAliveKey key : keysToRemove) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   206
                    removeVector(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } while (size() > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Do not serialize this class!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private void writeObject(java.io.ObjectOutputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        throw new NotSerializableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private void readObject(java.io.ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        throw new NotSerializableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
/* FILO order for recycling HttpClients, should run in a thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * to time them out.  If > maxConns are in use, block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   233
class ClientVector extends java.util.Stack<KeepAliveEntry> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private static final long serialVersionUID = -8680532108106489459L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    // sleep time in milliseconds, before cache clear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    int nap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    ClientVector (int nap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        this.nap = nap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    synchronized HttpClient get() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (empty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            // Loop until we find a connection that has not timed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            HttpClient hc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            long currentTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            do {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   253
                KeepAliveEntry e = pop();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                if ((currentTime - e.idleStartTime) > nap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    e.hc.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    hc = e.hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            } while ((hc== null) && (!empty()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            return hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /* return a still valid, unused HttpClient */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    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
   266
        if (size() >= KeepAliveCache.getMaxConnections()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            h.closeServer(); // otherwise the connection remains in limbo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            push(new KeepAliveEntry(h, System.currentTimeMillis()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Do not serialize this class!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private void writeObject(java.io.ObjectOutputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        throw new NotSerializableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private void readObject(java.io.ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        throw new NotSerializableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
class KeepAliveKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    private String      protocol = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private String      host = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    private int         port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    private Object      obj = null; // additional key, such as socketfactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param url the URL containing the protocol, host and port information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public KeepAliveKey(URL url, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        this.protocol = url.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        this.host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        this.port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        this.obj = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Determine whether or not two objects of this type are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 715
diff changeset
   309
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if ((obj instanceof KeepAliveKey) == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        KeepAliveKey kae = (KeepAliveKey)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return host.equals(kae.host)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            && (port == kae.port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            && protocol.equals(kae.protocol)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            && this.obj == kae.obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * The hashCode() for this object is the string hashCode() of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * concatenation of the protocol, host name and port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
3865
cc2ca3d07bbb 6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents: 715
diff changeset
   324
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        String str = protocol+host+port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return this.obj == null? str.hashCode() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            str.hashCode() + this.obj.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
class KeepAliveEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    HttpClient hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    long idleStartTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    KeepAliveEntry(HttpClient hc, long idleStartTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        this.hc = hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        this.idleStartTime = idleStartTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
}