2
|
1 |
/*
|
715
|
2 |
* Copyright 1996-2008 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.net.www.http;
|
|
27 |
|
|
28 |
import java.io.InputStream;
|
|
29 |
import java.io.IOException;
|
|
30 |
import java.io.NotSerializableException;
|
|
31 |
import java.util.*;
|
|
32 |
import java.net.URL;
|
|
33 |
import java.util.concurrent.ConcurrentHashMap;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* A class that implements a cache of idle Http connections for keep-alive
|
|
37 |
*
|
|
38 |
* @author Stephen R. Pietrowicz (NCSA)
|
|
39 |
* @author Dave Brown
|
|
40 |
*/
|
51
|
41 |
public class KeepAliveCache
|
|
42 |
extends ConcurrentHashMap<KeepAliveKey, ClientVector>
|
|
43 |
implements Runnable {
|
2
|
44 |
private static final long serialVersionUID = -2937172892064557949L;
|
|
45 |
|
|
46 |
/* maximum # keep-alive connections to maintain at once
|
|
47 |
* This should be 2 by the HTTP spec, but because we don't support pipe-lining
|
|
48 |
* a larger value is more appropriate. So we now set a default of 5, and the value
|
|
49 |
* refers to the number of idle connections per destination (in the cache) only.
|
|
50 |
* It can be reset by setting system property "http.maxConnections".
|
|
51 |
*/
|
|
52 |
static final int MAX_CONNECTIONS = 5;
|
|
53 |
static int result = -1;
|
|
54 |
static int getMaxConnections() {
|
|
55 |
if (result == -1) {
|
|
56 |
result = java.security.AccessController.doPrivileged(
|
|
57 |
new sun.security.action.GetIntegerAction("http.maxConnections",
|
|
58 |
MAX_CONNECTIONS))
|
|
59 |
.intValue();
|
|
60 |
if (result <= 0)
|
|
61 |
result = MAX_CONNECTIONS;
|
|
62 |
}
|
|
63 |
return result;
|
|
64 |
}
|
|
65 |
|
|
66 |
static final int LIFETIME = 5000;
|
|
67 |
|
|
68 |
private Thread keepAliveTimer = null;
|
|
69 |
|
|
70 |
/**
|
|
71 |
* Constructor
|
|
72 |
*/
|
|
73 |
public KeepAliveCache() {}
|
|
74 |
|
|
75 |
/**
|
|
76 |
* Register this URL and HttpClient (that supports keep-alive) with the cache
|
|
77 |
* @param url The URL contains info about the host and port
|
|
78 |
* @param http The HttpClient to be cached
|
|
79 |
*/
|
|
80 |
public synchronized void put(final URL url, Object obj, HttpClient http) {
|
|
81 |
boolean startThread = (keepAliveTimer == null);
|
|
82 |
if (!startThread) {
|
|
83 |
if (!keepAliveTimer.isAlive()) {
|
|
84 |
startThread = true;
|
|
85 |
}
|
|
86 |
}
|
|
87 |
if (startThread) {
|
|
88 |
clear();
|
|
89 |
/* Unfortunately, we can't always believe the keep-alive timeout we got
|
|
90 |
* back from the server. If I'm connected through a Netscape proxy
|
|
91 |
* to a server that sent me a keep-alive
|
|
92 |
* time of 15 sec, the proxy unilaterally terminates my connection
|
51
|
93 |
* The robustness to get around this is in HttpClient.parseHTTP()
|
2
|
94 |
*/
|
|
95 |
final KeepAliveCache cache = this;
|
|
96 |
java.security.AccessController.doPrivileged(
|
51
|
97 |
new java.security.PrivilegedAction<Void>() {
|
|
98 |
public Void run() {
|
2
|
99 |
// We want to create the Keep-Alive-Timer in the
|
|
100 |
// system threadgroup
|
|
101 |
ThreadGroup grp = Thread.currentThread().getThreadGroup();
|
|
102 |
ThreadGroup parent = null;
|
|
103 |
while ((parent = grp.getParent()) != null) {
|
|
104 |
grp = parent;
|
|
105 |
}
|
|
106 |
|
|
107 |
keepAliveTimer = new Thread(grp, cache, "Keep-Alive-Timer");
|
|
108 |
keepAliveTimer.setDaemon(true);
|
|
109 |
keepAliveTimer.setPriority(Thread.MAX_PRIORITY - 2);
|
|
110 |
keepAliveTimer.start();
|
|
111 |
return null;
|
|
112 |
}
|
|
113 |
});
|
|
114 |
}
|
|
115 |
|
|
116 |
KeepAliveKey key = new KeepAliveKey(url, obj);
|
51
|
117 |
ClientVector v = super.get(key);
|
2
|
118 |
|
|
119 |
if (v == null) {
|
|
120 |
int keepAliveTimeout = http.getKeepAliveTimeout();
|
|
121 |
v = new ClientVector(keepAliveTimeout > 0?
|
|
122 |
keepAliveTimeout*1000 : LIFETIME);
|
|
123 |
v.put(http);
|
|
124 |
super.put(key, v);
|
|
125 |
} else {
|
|
126 |
v.put(http);
|
|
127 |
}
|
|
128 |
}
|
|
129 |
|
51
|
130 |
/* remove an obsolete HttpClient from its VectorCache */
|
2
|
131 |
public synchronized void remove (HttpClient h, Object obj) {
|
|
132 |
KeepAliveKey key = new KeepAliveKey(h.url, obj);
|
51
|
133 |
ClientVector v = super.get(key);
|
2
|
134 |
if (v != null) {
|
|
135 |
v.remove(h);
|
|
136 |
if (v.empty()) {
|
|
137 |
removeVector(key);
|
|
138 |
}
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
51
|
142 |
/* called by a clientVector thread when all its connections have timed out
|
2
|
143 |
* and that vector of connections should be removed.
|
|
144 |
*/
|
|
145 |
synchronized void removeVector(KeepAliveKey k) {
|
|
146 |
super.remove(k);
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* Check to see if this URL has a cached HttpClient
|
|
151 |
*/
|
51
|
152 |
public synchronized HttpClient get(URL url, Object obj) {
|
2
|
153 |
|
|
154 |
KeepAliveKey key = new KeepAliveKey(url, obj);
|
51
|
155 |
ClientVector v = super.get(key);
|
2
|
156 |
if (v == null) { // nothing in cache yet
|
|
157 |
return null;
|
|
158 |
}
|
|
159 |
return v.get();
|
|
160 |
}
|
|
161 |
|
|
162 |
/* Sleeps for an alloted timeout, then checks for timed out connections.
|
|
163 |
* Errs on the side of caution (leave connections idle for a relatively
|
|
164 |
* short time).
|
|
165 |
*/
|
|
166 |
public void run() {
|
|
167 |
int total_cache;
|
|
168 |
do {
|
|
169 |
try {
|
|
170 |
Thread.sleep(LIFETIME);
|
|
171 |
} catch (InterruptedException e) {}
|
|
172 |
synchronized (this) {
|
|
173 |
/* Remove all unused HttpClients. Starting from the
|
|
174 |
* bottom of the stack (the least-recently used first).
|
|
175 |
* REMIND: It'd be nice to not remove *all* connections
|
|
176 |
* that aren't presently in use. One could have been added
|
|
177 |
* a second ago that's still perfectly valid, and we're
|
|
178 |
* needlessly axing it. But it's not clear how to do this
|
|
179 |
* cleanly, and doing it right may be more trouble than it's
|
|
180 |
* worth.
|
|
181 |
*/
|
|
182 |
|
|
183 |
long currentTime = System.currentTimeMillis();
|
|
184 |
|
51
|
185 |
ArrayList<KeepAliveKey> keysToRemove
|
|
186 |
= new ArrayList<KeepAliveKey>();
|
2
|
187 |
|
51
|
188 |
for (KeepAliveKey key : keySet()) {
|
|
189 |
ClientVector v = get(key);
|
2
|
190 |
synchronized (v) {
|
|
191 |
int i;
|
|
192 |
|
|
193 |
for (i = 0; i < v.size(); i++) {
|
51
|
194 |
KeepAliveEntry e = v.elementAt(i);
|
2
|
195 |
if ((currentTime - e.idleStartTime) > v.nap) {
|
|
196 |
HttpClient h = e.hc;
|
|
197 |
h.closeServer();
|
|
198 |
} else {
|
|
199 |
break;
|
|
200 |
}
|
|
201 |
}
|
|
202 |
v.subList(0, i).clear();
|
|
203 |
|
|
204 |
if (v.size() == 0) {
|
|
205 |
keysToRemove.add(key);
|
|
206 |
}
|
|
207 |
}
|
|
208 |
}
|
51
|
209 |
|
|
210 |
for (KeepAliveKey key : keysToRemove) {
|
|
211 |
removeVector(key);
|
2
|
212 |
}
|
|
213 |
}
|
|
214 |
} while (size() > 0);
|
|
215 |
|
|
216 |
return;
|
|
217 |
}
|
|
218 |
|
|
219 |
/*
|
|
220 |
* Do not serialize this class!
|
|
221 |
*/
|
|
222 |
private void writeObject(java.io.ObjectOutputStream stream)
|
|
223 |
throws IOException {
|
|
224 |
throw new NotSerializableException();
|
|
225 |
}
|
|
226 |
|
|
227 |
private void readObject(java.io.ObjectInputStream stream)
|
|
228 |
throws IOException, ClassNotFoundException {
|
|
229 |
throw new NotSerializableException();
|
|
230 |
}
|
|
231 |
}
|
|
232 |
|
|
233 |
/* FILO order for recycling HttpClients, should run in a thread
|
|
234 |
* to time them out. If > maxConns are in use, block.
|
|
235 |
*/
|
|
236 |
|
|
237 |
|
51
|
238 |
class ClientVector extends java.util.Stack<KeepAliveEntry> {
|
2
|
239 |
private static final long serialVersionUID = -8680532108106489459L;
|
|
240 |
|
|
241 |
// sleep time in milliseconds, before cache clear
|
|
242 |
int nap;
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
ClientVector (int nap) {
|
|
247 |
this.nap = nap;
|
|
248 |
}
|
|
249 |
|
|
250 |
synchronized HttpClient get() {
|
|
251 |
if (empty()) {
|
|
252 |
return null;
|
|
253 |
} else {
|
|
254 |
// Loop until we find a connection that has not timed out
|
|
255 |
HttpClient hc = null;
|
|
256 |
long currentTime = System.currentTimeMillis();
|
|
257 |
do {
|
51
|
258 |
KeepAliveEntry e = pop();
|
2
|
259 |
if ((currentTime - e.idleStartTime) > nap) {
|
|
260 |
e.hc.closeServer();
|
|
261 |
} else {
|
|
262 |
hc = e.hc;
|
|
263 |
}
|
|
264 |
} while ((hc== null) && (!empty()));
|
|
265 |
return hc;
|
|
266 |
}
|
|
267 |
}
|
|
268 |
|
|
269 |
/* return a still valid, unused HttpClient */
|
|
270 |
synchronized void put(HttpClient h) {
|
|
271 |
if (size() > KeepAliveCache.getMaxConnections()) {
|
|
272 |
h.closeServer(); // otherwise the connection remains in limbo
|
|
273 |
} else {
|
|
274 |
push(new KeepAliveEntry(h, System.currentTimeMillis()));
|
|
275 |
}
|
|
276 |
}
|
|
277 |
|
|
278 |
/*
|
|
279 |
* Do not serialize this class!
|
|
280 |
*/
|
|
281 |
private void writeObject(java.io.ObjectOutputStream stream)
|
|
282 |
throws IOException {
|
|
283 |
throw new NotSerializableException();
|
|
284 |
}
|
|
285 |
|
|
286 |
private void readObject(java.io.ObjectInputStream stream)
|
|
287 |
throws IOException, ClassNotFoundException {
|
|
288 |
throw new NotSerializableException();
|
|
289 |
}
|
|
290 |
}
|
|
291 |
|
|
292 |
|
|
293 |
class KeepAliveKey {
|
|
294 |
private String protocol = null;
|
|
295 |
private String host = null;
|
|
296 |
private int port = 0;
|
|
297 |
private Object obj = null; // additional key, such as socketfactory
|
|
298 |
|
|
299 |
/**
|
|
300 |
* Constructor
|
|
301 |
*
|
|
302 |
* @param url the URL containing the protocol, host and port information
|
|
303 |
*/
|
|
304 |
public KeepAliveKey(URL url, Object obj) {
|
|
305 |
this.protocol = url.getProtocol();
|
|
306 |
this.host = url.getHost();
|
|
307 |
this.port = url.getPort();
|
|
308 |
this.obj = obj;
|
|
309 |
}
|
|
310 |
|
|
311 |
/**
|
|
312 |
* Determine whether or not two objects of this type are equal
|
|
313 |
*/
|
|
314 |
public boolean equals(Object obj) {
|
|
315 |
if ((obj instanceof KeepAliveKey) == false)
|
|
316 |
return false;
|
|
317 |
KeepAliveKey kae = (KeepAliveKey)obj;
|
|
318 |
return host.equals(kae.host)
|
|
319 |
&& (port == kae.port)
|
|
320 |
&& protocol.equals(kae.protocol)
|
|
321 |
&& this.obj == kae.obj;
|
|
322 |
}
|
|
323 |
|
|
324 |
/**
|
|
325 |
* The hashCode() for this object is the string hashCode() of
|
|
326 |
* concatenation of the protocol, host name and port.
|
|
327 |
*/
|
|
328 |
public int hashCode() {
|
|
329 |
String str = protocol+host+port;
|
|
330 |
return this.obj == null? str.hashCode() :
|
|
331 |
str.hashCode() + this.obj.hashCode();
|
|
332 |
}
|
|
333 |
}
|
|
334 |
|
|
335 |
class KeepAliveEntry {
|
|
336 |
HttpClient hc;
|
|
337 |
long idleStartTime;
|
|
338 |
|
|
339 |
KeepAliveEntry(HttpClient hc, long idleStartTime) {
|
|
340 |
this.hc = hc;
|
|
341 |
this.idleStartTime = idleStartTime;
|
|
342 |
}
|
|
343 |
}
|