jdk/src/share/classes/sun/net/www/http/KeepAliveCache.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 3865 cc2ca3d07bbb
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1996-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.NotSerializableException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.concurrent.ConcurrentHashMap;
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
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    42
    extends ConcurrentHashMap<KeepAliveKey, ClientVector>
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(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    97
                new java.security.PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    98
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                   // We want to create the Keep-Alive-Timer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    // system threadgroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    ThreadGroup grp = Thread.currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    ThreadGroup parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    while ((parent = grp.getParent()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        grp = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    keepAliveTimer = new Thread(grp, cache, "Keep-Alive-Timer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    keepAliveTimer.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    keepAliveTimer.setPriority(Thread.MAX_PRIORITY - 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    keepAliveTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        KeepAliveKey key = new KeepAliveKey(url, obj);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   117
        ClientVector v = super.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (v == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            int keepAliveTimeout = http.getKeepAliveTimeout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            v = new ClientVector(keepAliveTimeout > 0?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                 keepAliveTimeout*1000 : LIFETIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            v.put(http);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            super.put(key, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            v.put(http);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   130
    /* remove an obsolete HttpClient from its VectorCache */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public synchronized void remove (HttpClient h, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        KeepAliveKey key = new KeepAliveKey(h.url, obj);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   133
        ClientVector v = super.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            v.remove(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (v.empty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                removeVector(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   142
    /* called by a clientVector thread when all its connections have timed out
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * and that vector of connections should be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    synchronized void removeVector(KeepAliveKey k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        super.remove(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Check to see if this URL has a cached HttpClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   152
    public synchronized HttpClient get(URL url, Object obj) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        KeepAliveKey key = new KeepAliveKey(url, obj);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   155
        ClientVector v = super.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (v == null) { // nothing in cache yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return v.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /* Sleeps for an alloted timeout, then checks for timed out connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Errs on the side of caution (leave connections idle for a relatively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * short time).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        int total_cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                Thread.sleep(LIFETIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            } catch (InterruptedException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                /* Remove all unused HttpClients.  Starting from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                 * bottom of the stack (the least-recently used first).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                 * REMIND: It'd be nice to not remove *all* connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                 * that aren't presently in use.  One could have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                 * a second ago that's still perfectly valid, and we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                 * needlessly axing it.  But it's not clear how to do this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                 * cleanly, and doing it right may be more trouble than it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                 * worth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                long currentTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   185
                ArrayList<KeepAliveKey> keysToRemove
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   186
                    = new ArrayList<KeepAliveKey>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   188
                for (KeepAliveKey key : keySet()) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   189
                    ClientVector v = get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    synchronized (v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        for (i = 0; i < v.size(); i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   194
                            KeepAliveEntry e = v.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                            if ((currentTime - e.idleStartTime) > v.nap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                HttpClient h = e.hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                h.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        v.subList(0, i).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        if (v.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                            keysToRemove.add(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   209
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   210
                for (KeepAliveKey key : keysToRemove) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   211
                    removeVector(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } while (size() > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Do not serialize this class!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private void writeObject(java.io.ObjectOutputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    throws IOException {
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
    private void readObject(java.io.ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        throw new NotSerializableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
/* FILO order for recycling HttpClients, should run in a thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 * to time them out.  If > maxConns are in use, block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   238
class ClientVector extends java.util.Stack<KeepAliveEntry> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private static final long serialVersionUID = -8680532108106489459L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    // sleep time in milliseconds, before cache clear
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    int 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    ClientVector (int nap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        this.nap = nap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    synchronized HttpClient get() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (empty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            // Loop until we find a connection that has not timed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            HttpClient hc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            long currentTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            do {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   258
                KeepAliveEntry e = pop();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                if ((currentTime - e.idleStartTime) > nap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    e.hc.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    hc = e.hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            } while ((hc== null) && (!empty()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            return hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /* return a still valid, unused HttpClient */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    synchronized void put(HttpClient h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (size() > KeepAliveCache.getMaxConnections()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            h.closeServer(); // otherwise the connection remains in limbo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            push(new KeepAliveEntry(h, System.currentTimeMillis()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Do not serialize this class!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private void writeObject(java.io.ObjectOutputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    throws IOException {
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
    private void readObject(java.io.ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        throw new NotSerializableException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
class KeepAliveKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private String      protocol = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    private String      host = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    private int         port = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    private Object      obj = null; // additional key, such as socketfactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param url the URL containing the protocol, host and port information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public KeepAliveKey(URL url, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        this.protocol = url.getProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        this.host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        this.port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        this.obj = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Determine whether or not two objects of this type are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if ((obj instanceof KeepAliveKey) == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        KeepAliveKey kae = (KeepAliveKey)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return host.equals(kae.host)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            && (port == kae.port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            && protocol.equals(kae.protocol)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            && this.obj == kae.obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * The hashCode() for this object is the string hashCode() of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * concatenation of the protocol, host name and port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        String str = protocol+host+port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return this.obj == null? str.hashCode() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            str.hashCode() + this.obj.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
class KeepAliveEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    HttpClient hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    long idleStartTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    KeepAliveEntry(HttpClient hc, long idleStartTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        this.hc = hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        this.idleStartTime = idleStartTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
}