jdk/test/java/net/URLClassLoader/HttpTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5165 5693cc1e95c6
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002 Sun Microsystems, Inc.  All Rights Reserved.
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4636331
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check that URLClassLoader doesn't create excessive http
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          connections
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class HttpTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
     * Simple http server to service http requests. Auto shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
     * if "idle" (no requests) for 10 seconds. Forks worker thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * to service persistent connections. Work threads shutdown if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * "idle" for 5 seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static class HttpServer implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        private static HttpServer svr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        private static Counters cnts = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        private static ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        private static Object counterLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        private static int getCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        private static int headCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        class Worker extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            Worker(Socket s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                this.s = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                    InputStream in = s.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                        // read entire request from client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                        byte b[] = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                        int n, total=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                        // max 5 seconds to wait for new request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                        s.setSoTimeout(5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                n = in.read(b, total, b.length-total);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                // max 0.5 seconds between each segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                // of request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                s.setSoTimeout(500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                if (n > 0) total += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                            } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                        } catch (SocketTimeoutException e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                        if (total == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                            s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        boolean getRequest = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                        if (b[0] == 'G' && b[1] == 'E' && b[2] == 'T')
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                            getRequest = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        synchronized (counterLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                            if (getRequest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                getCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                headCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        // response to client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        PrintStream out = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                        s.getOutputStream() ));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        out.print("HTTP/1.1 200 OK\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        out.print("Content-Length: 75000\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                        out.print("\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        if (getRequest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                            for (int i=0; i<75*1000; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                out.write( (byte)'.' );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    } // for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        HttpServer() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                // shutdown if no request in 10 seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                ss.setSoTimeout(10000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    Socket s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    (new Worker(s)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        public static HttpServer create() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (svr != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                return svr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            cnts = new Counters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            svr = new HttpServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            (new Thread(svr)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return svr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        public static void shutdown() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (svr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                svr = null;
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
        public int port() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        public static class Counters {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                synchronized (counterLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    getCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    headCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            public int getCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                synchronized (counterLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    return getCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            public int headCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                synchronized (counterLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    return headCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                synchronized (counterLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    return "GET count: " + getCount + "; " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                       "HEAD count: " + headCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        public Counters counters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return cnts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        // create http server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        HttpServer svr = HttpServer.create();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // create class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        URL urls[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            { new URL("http://localhost:" + svr.port() + "/dir1/"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
              new URL("http://localhost:" + svr.port() + "/dir2/") };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        URLClassLoader cl = new URLClassLoader(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // Test 1 - check that getResource does single HEAD request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        svr.counters().reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        URL url = cl.getResource("foo.gif");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        System.out.println(svr.counters());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (svr.counters().getCount() > 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            svr.counters().headCount() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // Test 2 - check that getResourceAsStream does at most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        //          one GET request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        svr.counters().reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        InputStream in = cl.getResourceAsStream("foo2.gif");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        System.out.println(svr.counters());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (svr.counters().getCount() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        // Test 3 - check that getResources only does HEAD requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        svr.counters().reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        Enumeration e = cl.getResources("foos.gif");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        } catch (NoSuchElementException exc) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        System.out.println(svr.counters());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (svr.counters().getCount() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // shutdown http server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        svr.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new Exception("Excessive http connections established - Test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
}