author | jccollet |
Tue, 26 May 2009 16:03:51 +0200 | |
changeset 2928 | 80b0b6c2d527 |
parent 1334 | 21b652819b97 |
child 3865 | cc2ca3d07bbb |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2005-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.util.concurrent.LinkedBlockingQueue; |
|
29 |
import java.util.concurrent.TimeUnit; |
|
30 |
import java.io.IOException; |
|
31 |
import sun.net.NetProperties; |
|
32 |
import java.security.AccessController; |
|
33 |
import java.security.PrivilegedAction; |
|
34 |
||
35 |
/** |
|
36 |
* This class is used to cleanup any remaining data that may be on a KeepAliveStream |
|
37 |
* so that the connection can be cached in the KeepAliveCache. |
|
38 |
* Instances of this class can be used as a FIFO queue for KeepAliveCleanerEntry objects. |
|
39 |
* Executing this Runnable removes each KeepAliveCleanerEntry from the Queue, reads |
|
40 |
* the reamining bytes on its KeepAliveStream, and if successful puts the connection in |
|
41 |
* the KeepAliveCache. |
|
42 |
* |
|
43 |
* @author Chris Hegarty |
|
44 |
*/ |
|
45 |
||
1334
21b652819b97
6746836: java.net exception classes don't specify serialVersionUID
chegar
parents:
715
diff
changeset
|
46 |
@SuppressWarnings("serial") // never serialized |
2 | 47 |
public class KeepAliveStreamCleaner extends LinkedBlockingQueue<KeepAliveCleanerEntry> implements Runnable |
48 |
{ |
|
49 |
// maximum amount of remaining data that we will try to cleanup |
|
50 |
protected static int MAX_DATA_REMAINING = 512; |
|
51 |
||
52 |
// maximum amount of KeepAliveStreams to be queued |
|
53 |
protected static int MAX_CAPACITY = 10; |
|
54 |
||
55 |
// timeout for both socket and poll on the queue |
|
56 |
protected static final int TIMEOUT = 5000; |
|
57 |
||
58 |
// max retries for skipping data |
|
59 |
private static final int MAX_RETRIES = 5; |
|
60 |
||
61 |
static { |
|
62 |
final String maxDataKey = "http.KeepAlive.remainingData"; |
|
51 | 63 |
int maxData = AccessController.doPrivileged( |
64 |
new PrivilegedAction<Integer>() { |
|
65 |
public Integer run() { |
|
66 |
return NetProperties.getInteger(maxDataKey, MAX_DATA_REMAINING); |
|
67 |
}}).intValue() * 1024; |
|
2 | 68 |
MAX_DATA_REMAINING = maxData; |
69 |
||
70 |
final String maxCapacityKey = "http.KeepAlive.queuedConnections"; |
|
51 | 71 |
int maxCapacity = AccessController.doPrivileged( |
72 |
new PrivilegedAction<Integer>() { |
|
73 |
public Integer run() { |
|
74 |
return NetProperties.getInteger(maxCapacityKey, MAX_CAPACITY); |
|
75 |
}}).intValue(); |
|
2 | 76 |
MAX_CAPACITY = maxCapacity; |
77 |
||
78 |
} |
|
79 |
||
80 |
||
81 |
public KeepAliveStreamCleaner() |
|
82 |
{ |
|
83 |
super(MAX_CAPACITY); |
|
84 |
} |
|
85 |
||
86 |
public KeepAliveStreamCleaner(int capacity) |
|
87 |
{ |
|
88 |
super(capacity); |
|
89 |
} |
|
90 |
||
91 |
public void run() |
|
92 |
{ |
|
93 |
KeepAliveCleanerEntry kace = null; |
|
94 |
||
95 |
do { |
|
96 |
try { |
|
97 |
kace = poll((long)TIMEOUT, TimeUnit.MILLISECONDS); |
|
98 |
if(kace == null) |
|
99 |
break; |
|
100 |
||
101 |
KeepAliveStream kas = kace.getKeepAliveStream(); |
|
102 |
||
103 |
if (kas != null) { |
|
104 |
synchronized(kas) { |
|
105 |
HttpClient hc = kace.getHttpClient(); |
|
106 |
try { |
|
107 |
if (hc != null && !hc.isInKeepAliveCache()) { |
|
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1334
diff
changeset
|
108 |
int oldTimeout = hc.getReadTimeout(); |
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1334
diff
changeset
|
109 |
hc.setReadTimeout(TIMEOUT); |
2 | 110 |
long remainingToRead = kas.remainingToRead(); |
111 |
if (remainingToRead > 0) { |
|
112 |
long n = 0; |
|
113 |
int retries = 0; |
|
114 |
while (n < remainingToRead && retries < MAX_RETRIES) { |
|
115 |
remainingToRead = remainingToRead - n; |
|
116 |
n = kas.skip(remainingToRead); |
|
117 |
if (n == 0) |
|
118 |
retries++; |
|
119 |
} |
|
120 |
remainingToRead = remainingToRead - n; |
|
121 |
} |
|
122 |
if (remainingToRead == 0) { |
|
2928
80b0b6c2d527
6726695: HttpURLConnection shoul support 'Expect: 100-contimue' headers for PUT
jccollet
parents:
1334
diff
changeset
|
123 |
hc.setReadTimeout(oldTimeout); |
2 | 124 |
hc.finished(); |
125 |
} else |
|
126 |
hc.closeServer(); |
|
127 |
} |
|
128 |
} catch (IOException ioe) { |
|
129 |
hc.closeServer(); |
|
130 |
} finally { |
|
131 |
kas.setClosed(); |
|
132 |
} |
|
133 |
} |
|
134 |
} |
|
135 |
} catch (InterruptedException ie) { } |
|
136 |
} while (kace != null); |
|
137 |
} |
|
138 |
} |