jdk/test/sun/misc/JarIndex/metaInfFilenames/Basic.java
author alanb
Wed, 12 Feb 2014 14:07:55 +0000
changeset 22959 2d9d076cee41
parent 11119 6ff03c1202ce
child 28522 c1aedcf211da
permissions -rw-r--r--
8034776: Remove sun.misc.Service Reviewed-by: sundar, lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
     1
/*
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
     4
 *
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
     8
 *
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    13
 * accompanied this code).
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    14
 *
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    18
 *
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    21
 * questions.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    22
 */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    23
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    24
/*
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    25
 * @test
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    26
 * @bug 6887710
22959
2d9d076cee41 8034776: Remove sun.misc.Service
alanb
parents: 11119
diff changeset
    27
 * @summary Verify the impact of sun.misc.JarIndex.metaInfFilenames on ServiceLoader
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    28
 * @run main/othervm Basic
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    29
 */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    30
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    31
import java.io.IOException;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    32
import java.io.BufferedReader;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    33
import java.io.File;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    34
import java.io.FileInputStream;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    35
import java.io.InputStream;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    36
import java.io.InputStreamReader;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    37
import java.io.OutputStream;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    38
import java.net.InetSocketAddress;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    39
import java.net.URI;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    40
import java.net.URL;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    41
import java.net.URLClassLoader;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    42
import java.util.Arrays;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    43
import java.util.Iterator;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    44
import java.util.ServiceLoader;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    45
import com.sun.net.httpserver.Headers;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    46
import com.sun.net.httpserver.HttpExchange;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    47
import com.sun.net.httpserver.HttpHandler;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    48
import com.sun.net.httpserver.HttpServer;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    49
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    50
/**
22959
2d9d076cee41 8034776: Remove sun.misc.Service
alanb
parents: 11119
diff changeset
    51
 * Verifies the impact of sun.misc.JarIndex.metaInfFilenames on ServiceLoader
2d9d076cee41 8034776: Remove sun.misc.Service
alanb
parents: 11119
diff changeset
    52
 * and on finding resources via Class.getResource.
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    53
 *
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    54
 * 1) Compile the test sources:
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    55
 *   jarA:
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    56
 *     META-INF/services/my.happy.land
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    57
 *     com/message/spi/MessageService.java
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    58
 *     a/A.java
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    59
 *   jarB:
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    60
 *     META-INF/JAVA2.DS
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    61
 *     META-INF/services/no.name.service
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    62
 *     b/B.java
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    63
 *   jarC:
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    64
 *     META-INF/fonts.mf
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    65
 *     META-INF/fonts/Company-corporate.ttf
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    66
 *     META-INF/fonts/kidpr.ttf
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    67
 *     META-INF/services/com.message.spi.MessageService
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    68
 *     my/impl/StandardMessageService.java
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    69
 *
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    70
 * 2) Build three jar files a.jar, b.jar, c.jar
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    71
 *
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    72
 * 3) Create an index in a.jar (jar -i a.jar b.jar c.jar)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    73
 *      with sun.misc.JarIndex.metaInfFilenames=true
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    74
 *
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    75
 * 4) Start a HTTP server serving out the three jars.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    76
 *
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    77
 * The test then tries to locate services/resources within the jars using
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    78
 * URLClassLoader. Each request to the HTTP server is recorded to ensure
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    79
 * only the correct amount of requests are being made.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    80
 *
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    81
 * Note: Needs jdk/lib/tools.jar in the classpath to compile and run.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    82
 */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    83
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    84
public class Basic {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    85
    static final String slash = File.separator;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    86
    static final String[] testSources =  {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    87
         "jarA" + slash + "a" + slash + "A.java",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    88
         "jarA" + slash + "com" + slash + "message" + slash + "spi" + slash + "MessageService.java",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    89
         "jarB" + slash + "b" + slash + "B.java",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    90
         "jarC" + slash + "my" + slash + "impl" + slash + "StandardMessageService.java"};
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    91
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    92
    static final String testSrc = System.getProperty("test.src");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    93
    static final String testSrcDir = testSrc != null ? testSrc : ".";
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    94
    static final String testClasses = System.getProperty("test.classes");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    95
    static final String testClassesDir = testClasses != null ? testClasses : ".";
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    96
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    97
    static JarHttpServer httpServer;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    98
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    99
    public static void main(String[] args) throws Exception {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   100
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   101
        // Set global url cache to false so that we can track every jar request.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   102
        (new URL("http://localhost/")).openConnection().setDefaultUseCaches(false);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   103
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   104
        buildTest();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   105
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   106
        try {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   107
            httpServer = new JarHttpServer(testClassesDir);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   108
            httpServer.start();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   109
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   110
            doTest(httpServer.getAddress());
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   111
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   112
        } catch (IOException ioe) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   113
            ioe.printStackTrace();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   114
        } finally {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   115
            if (httpServer != null) { httpServer.stop(2); }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   116
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   117
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   118
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   119
    static void buildTest() {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   120
        /* compile the source that will be used to generate the jars */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   121
        for (int i=0; i<testSources.length; i++)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   122
            testSources[i] = testSrcDir + slash + testSources[i];
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   123
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   124
        compile("-d" , testClassesDir,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   125
                "-sourcepath", testSrcDir,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   126
                testSources[0], testSources[1], testSources[2], testSources[3]);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   127
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   128
        /* build the 3 jar files */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   129
        jar("-cf", testClassesDir + slash + "a.jar",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   130
            "-C", testClassesDir, "a",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   131
            "-C", testClassesDir, "com",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   132
            "-C", testSrcDir + slash + "jarA", "META-INF");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   133
        jar("-cf", testClassesDir + slash + "b.jar",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   134
            "-C", testClassesDir, "b",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   135
            "-C", testSrcDir + slash + "jarB", "META-INF");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   136
        jar("-cf", testClassesDir + slash + "c.jar",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   137
            "-C", testClassesDir, "my",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   138
            "-C", testSrcDir + slash + "jarC", "META-INF");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   139
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   140
        /* Create an index in a.jar for b.jar and c.jar */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   141
        createIndex(testClassesDir);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   142
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   143
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   144
    /* run jar <args> */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   145
    static void jar(String... args) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   146
        debug("Running: jar " + Arrays.toString(args));
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   147
        sun.tools.jar.Main jar = new sun.tools.jar.Main(System.out, System.err, "jar");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   148
        if (!jar.run(args)) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   149
            throw new RuntimeException("jar failed: args=" + Arrays.toString(args));
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   150
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   151
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   152
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   153
    /* run javac <args> */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   154
    static void compile(String... args) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   155
        debug("Running: javac " + Arrays.toString(args));
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 10705
diff changeset
   156
        if (com.sun.tools.javac.Main.compile(args) != 0) {
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   157
             throw new RuntimeException("javac failed: args=" + Arrays.toString(args));
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   158
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   159
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   160
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   161
    static String jar;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   162
    static {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   163
        String javaHome = System.getProperty("java.home");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   164
        if (javaHome.endsWith("jre")) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   165
            int index = javaHome.lastIndexOf(slash);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   166
            if (index != -1)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   167
                javaHome = javaHome.substring(0, index);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   168
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   170
        jar = javaHome + slash+  "bin" + slash + "jar";
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   171
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   172
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   173
    /* create the index */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   174
    static void createIndex(String workingDir) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   175
        // ProcessBuilder is used so that the current directory can be set
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   176
        // to the directory that directly contains the jars.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   177
        debug("Running jar to create the index");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   178
        ProcessBuilder pb = new ProcessBuilder(
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   179
           jar, "-J-Dsun.misc.JarIndex.metaInfFilenames=true", "-i", "a.jar", "b.jar", "c.jar");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   180
        pb.directory(new File(workingDir));
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   181
        //pd.inheritIO();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   182
        try {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   183
            Process p = pb.start();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   184
            if(p.waitFor() != 0)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   185
                throw new RuntimeException("jar indexing failed");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   186
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   187
            if(debug && p != null) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   188
                String line = null;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   189
                BufferedReader reader =
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   190
                         new BufferedReader(new InputStreamReader(p.getInputStream()));
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   191
                while((line = reader.readLine()) != null)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   192
                    debug(line);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   193
                reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   194
                while((line = reader.readLine()) != null)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   195
                    debug(line);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   196
            }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   197
        } catch(InterruptedException ie) { throw new RuntimeException(ie);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   198
        } catch(IOException e) { throw new RuntimeException(e); }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   199
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   200
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   201
    static final boolean debug = true;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   202
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   203
    static void debug(Object message) { if (debug) System.out.println(message); }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   204
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   205
    /* service define in c.jar */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   206
    static final String messageService = "com.message.spi.MessageService";
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   207
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   208
    /* a service that is not defined in any of the jars */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   209
    static final String unknownService = "java.lang.Object";
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   210
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   211
    static void doTest(InetSocketAddress serverAddress) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   212
        URL baseURL = new URL("http://localhost:" + serverAddress.getPort() + "/");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   213
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   214
        int failed = 0;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   215
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   216
        // Tests using java.util.SerivceLoader
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   217
        if (!javaUtilServiceLoaderTest(baseURL, messageService, true, false, true)) {
22959
2d9d076cee41 8034776: Remove sun.misc.Service
alanb
parents: 11119
diff changeset
   218
            System.out.println("Test: ServiceLoader looking for " + messageService + ", failed");
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   219
            failed++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   220
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   221
        if (!javaUtilServiceLoaderTest(baseURL, unknownService, false, false, false)) {
22959
2d9d076cee41 8034776: Remove sun.misc.Service
alanb
parents: 11119
diff changeset
   222
            System.out.println("Test: ServiceLoader looking for " + unknownService + " failed");
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   223
            failed++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   224
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   225
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   226
        // Tests using java.lang.Class (similar to the FontManager in javafx)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   227
        if (!klassLoader(baseURL, "/META-INF/fonts.mf", true, false, true)) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   228
            System.out.println("Test: klassLoader looking for /META-INF/fonts.mf failed");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   229
            failed++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   230
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   231
        if (!klassLoader(baseURL, "/META-INF/unknown.mf", false, false, false)) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   232
            System.out.println("Test: klassLoader looking for /META-INF/unknown.mf failed");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   233
            failed++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   234
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   235
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   236
        if (failed > 0)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   237
            throw new RuntimeException("Failed: " + failed + " tests");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   238
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   239
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   240
    static boolean javaUtilServiceLoaderTest(URL baseURL,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   241
                                             String serviceClass,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   242
                                             boolean expectToFind,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   243
                                             boolean expectbDotJar,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   244
                                             boolean expectcDotJar) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   245
        debug("----------------------------------");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   246
        debug("Running test with java.util.ServiceLoader looking for " + serviceClass);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   247
        URLClassLoader loader = getLoader(baseURL);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   248
        httpServer.reset();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   249
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 10705
diff changeset
   250
        Class<?> messageServiceClass = null;
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   251
        try {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   252
            messageServiceClass = loader.loadClass(serviceClass);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   253
        } catch (ClassNotFoundException cnfe) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   254
            System.err.println(cnfe);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   255
            throw new RuntimeException("Error in test: " + cnfe);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   256
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   257
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 10705
diff changeset
   258
        Iterator<?> iterator = (ServiceLoader.load(messageServiceClass, loader)).iterator();
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   259
        if (expectToFind && !iterator.hasNext()) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   260
            debug(messageServiceClass + " NOT found.");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   261
            return false;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   262
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   263
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   264
        while (iterator.hasNext()) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   265
            debug("found " + iterator.next() + " " + messageService);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   266
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   267
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   268
        debug("HttpServer: " + httpServer);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   269
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   270
        if (!expectbDotJar && httpServer.bDotJar > 0) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   271
            debug("Unexpeced request sent to the httpserver for b.jar");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   272
            return false;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   273
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   274
        if (!expectcDotJar && httpServer.cDotJar > 0) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   275
            debug("Unexpeced request sent to the httpserver for c.jar");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   276
            return false;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   277
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   278
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   279
        return true;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   280
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   281
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   282
    /* Tries to find a resource in a similar way to the font manager in javafx
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   283
     * com.sun.javafx.scene.text.FontManager */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   284
    static boolean klassLoader(URL baseURL,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   285
                               String resource,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   286
                               boolean expectToFind,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   287
                               boolean expectbDotJar,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   288
                               boolean expectcDotJar) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   289
        debug("----------------------------------");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   290
        debug("Running test looking for " + resource);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   291
        URLClassLoader loader = getLoader(baseURL);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   292
        httpServer.reset();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   293
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 10705
diff changeset
   294
        Class<?> ADotAKlass = null;
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   295
        try {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   296
            ADotAKlass = loader.loadClass("a.A");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   297
        } catch (ClassNotFoundException cnfe) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   298
            System.err.println(cnfe);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   299
            throw new RuntimeException("Error in test: " + cnfe);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   300
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   301
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   302
        URL u = ADotAKlass.getResource(resource);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   303
        if (expectToFind && u == null) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   304
            System.out.println("Expected to find " + resource + " but didn't");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   305
            return false;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   306
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   307
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   308
        debug("HttpServer: " + httpServer);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   309
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   310
        if (!expectbDotJar && httpServer.bDotJar > 0) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   311
            debug("Unexpeced request sent to the httpserver for b.jar");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   312
            return false;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   313
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   314
        if (!expectcDotJar && httpServer.cDotJar > 0) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   315
            debug("Unexpeced request sent to the httpserver for c.jar");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   316
            return false;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   317
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   318
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   319
        return true;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   320
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   321
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   322
    static URLClassLoader getLoader(URL baseURL) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   323
        ClassLoader loader = Basic.class.getClassLoader();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   324
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   325
        while (loader.getParent() != null)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   326
            loader = loader.getParent();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   327
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   328
        return new URLClassLoader( new URL[]{
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   329
            new URL(baseURL, "a.jar"),
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   330
            new URL(baseURL, "b.jar"),
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   331
            new URL(baseURL, "c.jar")}, loader );
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   332
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   333
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   334
    /**
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   335
     * HTTP Server to server the jar files.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   336
     */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   337
    static class JarHttpServer implements HttpHandler {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   338
        final String docsDir;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   339
        final HttpServer httpServer;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   340
        int aDotJar, bDotJar, cDotJar;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   341
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   342
        JarHttpServer(String docsDir) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   343
            this.docsDir = docsDir;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   344
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   345
            httpServer = HttpServer.create(new InetSocketAddress(0), 0);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   346
            httpServer.createContext("/", this);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   347
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   348
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   349
        void start() throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   350
            httpServer.start();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   351
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   352
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   353
        void stop(int delay) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   354
            httpServer.stop(delay);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   355
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   356
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   357
        InetSocketAddress getAddress() {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   358
            return httpServer.getAddress();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   359
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   360
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   361
        void reset() {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   362
            aDotJar = bDotJar = cDotJar = 0;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   363
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   364
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   365
        @Override
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   366
        public String toString() {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   367
            return "aDotJar=" + aDotJar + ", bDotJar=" + bDotJar + ", cDotJar=" + cDotJar;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   368
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   369
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   370
        public void handle(HttpExchange t) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   371
            InputStream is = t.getRequestBody();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   372
            Headers map = t.getRequestHeaders();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   373
            Headers rmap = t.getResponseHeaders();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   374
            URI uri = t.getRequestURI();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   375
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   376
            debug("Server: received request for " + uri);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   377
            String path = uri.getPath();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   378
            if (path.endsWith("a.jar"))
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   379
                aDotJar++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   380
            else if (path.endsWith("b.jar"))
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   381
                bDotJar++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   382
            else if (path.endsWith("c.jar"))
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   383
                cDotJar++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   384
            else
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   385
                System.out.println("Unexpected resource request" + path);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   386
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   387
            while (is.read() != -1);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   388
            is.close();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   389
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   390
            File file = new File(docsDir, path);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   391
            if (!file.exists())
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   392
                throw new RuntimeException("Error: request for " + file);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   393
            long clen = file.length();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   394
            t.sendResponseHeaders (200, clen);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   395
            OutputStream os = t.getResponseBody();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   396
            FileInputStream fis = new FileInputStream(file);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   397
            try {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   398
                byte[] buf = new byte [16 * 1024];
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   399
                int len;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   400
                while ((len=fis.read(buf)) != -1) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   401
                    os.write (buf, 0, len);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   402
                }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   403
            } catch (IOException e) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   404
                e.printStackTrace();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   405
            }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   406
            fis.close();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   407
            os.close();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   408
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   409
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   410
}