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