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