jdk/test/sun/misc/JarIndex/metaInfFilenames/Basic.java
author xiaofeya
Tue, 20 Jan 2015 13:49:28 +0000
changeset 28522 c1aedcf211da
parent 22959 2d9d076cee41
child 30820 0d4717a011d3
permissions -rw-r--r--
8061448: Cleanup sun/misc/JarIndex tests to remove the check for the jre directory Reviewed-by: 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
 */
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
public class Basic {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    84
    static final String slash = File.separator;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    85
    static final String[] testSources =  {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    86
         "jarA" + slash + "a" + slash + "A.java",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    87
         "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
    88
         "jarB" + slash + "b" + slash + "B.java",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    89
         "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
    90
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    91
    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
    92
    static final String testSrcDir = testSrc != null ? testSrc : ".";
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    93
    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
    94
    static final String testClassesDir = testClasses != null ? testClasses : ".";
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    95
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    96
    static JarHttpServer httpServer;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    97
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    98
    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
    99
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   100
        // 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
   101
        (new URL("http://localhost/")).openConnection().setDefaultUseCaches(false);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   102
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   103
        buildTest();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   104
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   105
        try {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   106
            httpServer = new JarHttpServer(testClassesDir);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   107
            httpServer.start();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   108
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   109
            doTest(httpServer.getAddress());
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   110
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   111
        } catch (IOException ioe) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   112
            ioe.printStackTrace();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   113
        } finally {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   114
            if (httpServer != null) { httpServer.stop(2); }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   115
        }
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
    static void buildTest() {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   119
        /* 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
   120
        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
   121
            testSources[i] = testSrcDir + slash + testSources[i];
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   122
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   123
        compile("-d" , testClassesDir,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   124
                "-sourcepath", testSrcDir,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   125
                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
   126
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   127
        /* build the 3 jar files */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   128
        jar("-cf", testClassesDir + slash + "a.jar",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   129
            "-C", testClassesDir, "a",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   130
            "-C", testClassesDir, "com",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   131
            "-C", testSrcDir + slash + "jarA", "META-INF");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   132
        jar("-cf", testClassesDir + slash + "b.jar",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   133
            "-C", testClassesDir, "b",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   134
            "-C", testSrcDir + slash + "jarB", "META-INF");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   135
        jar("-cf", testClassesDir + slash + "c.jar",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   136
            "-C", testClassesDir, "my",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   137
            "-C", testSrcDir + slash + "jarC", "META-INF");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   138
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   139
        /* 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
   140
        createIndex(testClassesDir);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   141
    }
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
    /* run jar <args> */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   144
    static void jar(String... args) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   145
        debug("Running: jar " + Arrays.toString(args));
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   146
        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
   147
        if (!jar.run(args)) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   148
            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
   149
        }
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
    /* run javac <args> */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   153
    static void compile(String... args) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   154
        debug("Running: javac " + Arrays.toString(args));
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 10705
diff changeset
   155
        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
   156
             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
   157
        }
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
    static String jar;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   161
    static {
28522
c1aedcf211da 8061448: Cleanup sun/misc/JarIndex tests to remove the check for the jre directory
xiaofeya
parents: 22959
diff changeset
   162
        jar = System.getProperty("java.home") + slash+  "bin" + slash + "jar";
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   163
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   164
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   165
    /* create the index */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   166
    static void createIndex(String workingDir) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   167
        // 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
   168
        // 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
   169
        debug("Running jar to create the index");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   170
        ProcessBuilder pb = new ProcessBuilder(
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   171
           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
   172
        pb.directory(new File(workingDir));
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   173
        //pd.inheritIO();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   174
        try {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   175
            Process p = pb.start();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   176
            if(p.waitFor() != 0)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   177
                throw new RuntimeException("jar indexing failed");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   178
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   179
            if(debug && p != null) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   180
                String line = null;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   181
                BufferedReader reader =
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   182
                         new BufferedReader(new InputStreamReader(p.getInputStream()));
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   183
                while((line = reader.readLine()) != null)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   184
                    debug(line);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   185
                reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   186
                while((line = reader.readLine()) != null)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   187
                    debug(line);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   188
            }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   189
        } catch(InterruptedException ie) { throw new RuntimeException(ie);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   190
        } catch(IOException e) { throw new RuntimeException(e); }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   191
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   192
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   193
    static final boolean debug = true;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   194
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   195
    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
   196
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   197
    /* service define in c.jar */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   198
    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
   199
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   200
    /* 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
   201
    static final String unknownService = "java.lang.Object";
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 doTest(InetSocketAddress serverAddress) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   204
        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
   205
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   206
        int failed = 0;
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
        // Tests using java.util.SerivceLoader
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   209
        if (!javaUtilServiceLoaderTest(baseURL, messageService, true, false, true)) {
22959
2d9d076cee41 8034776: Remove sun.misc.Service
alanb
parents: 11119
diff changeset
   210
            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
   211
            failed++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   212
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   213
        if (!javaUtilServiceLoaderTest(baseURL, unknownService, false, false, false)) {
22959
2d9d076cee41 8034776: Remove sun.misc.Service
alanb
parents: 11119
diff changeset
   214
            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
   215
            failed++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   216
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   217
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   218
        // 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
   219
        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
   220
            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
   221
            failed++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   222
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   223
        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
   224
            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
   225
            failed++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   226
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   227
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   228
        if (failed > 0)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   229
            throw new RuntimeException("Failed: " + failed + " tests");
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
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   232
    static boolean javaUtilServiceLoaderTest(URL baseURL,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   233
                                             String serviceClass,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   234
                                             boolean expectToFind,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   235
                                             boolean expectbDotJar,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   236
                                             boolean expectcDotJar) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   237
        debug("----------------------------------");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   238
        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
   239
        URLClassLoader loader = getLoader(baseURL);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   240
        httpServer.reset();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   241
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 10705
diff changeset
   242
        Class<?> messageServiceClass = null;
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   243
        try {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   244
            messageServiceClass = loader.loadClass(serviceClass);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   245
        } catch (ClassNotFoundException cnfe) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   246
            System.err.println(cnfe);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   247
            throw new RuntimeException("Error in test: " + cnfe);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   248
        }
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
        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
   251
        if (expectToFind && !iterator.hasNext()) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   252
            debug(messageServiceClass + " NOT found.");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   253
            return false;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   254
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   255
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   256
        while (iterator.hasNext()) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   257
            debug("found " + iterator.next() + " " + messageService);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   258
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   259
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   260
        debug("HttpServer: " + httpServer);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   261
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   262
        if (!expectbDotJar && httpServer.bDotJar > 0) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   263
            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
   264
            return false;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   265
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   266
        if (!expectcDotJar && httpServer.cDotJar > 0) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   267
            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
   268
            return false;
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
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   271
        return true;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   272
    }
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
    /* 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
   275
     * com.sun.javafx.scene.text.FontManager */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   276
    static boolean klassLoader(URL baseURL,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   277
                               String resource,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   278
                               boolean expectToFind,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   279
                               boolean expectbDotJar,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   280
                               boolean expectcDotJar) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   281
        debug("----------------------------------");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   282
        debug("Running test looking for " + resource);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   283
        URLClassLoader loader = getLoader(baseURL);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   284
        httpServer.reset();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   285
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 10705
diff changeset
   286
        Class<?> ADotAKlass = null;
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   287
        try {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   288
            ADotAKlass = loader.loadClass("a.A");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   289
        } catch (ClassNotFoundException cnfe) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   290
            System.err.println(cnfe);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   291
            throw new RuntimeException("Error in test: " + cnfe);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   292
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   293
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   294
        URL u = ADotAKlass.getResource(resource);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   295
        if (expectToFind && u == null) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   296
            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
   297
            return false;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   298
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   299
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   300
        debug("HttpServer: " + httpServer);
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
        if (!expectbDotJar && httpServer.bDotJar > 0) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   303
            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
   304
            return false;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   305
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   306
        if (!expectcDotJar && httpServer.cDotJar > 0) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   307
            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
   308
            return false;
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
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   311
        return true;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   312
    }
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
    static URLClassLoader getLoader(URL baseURL) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   315
        ClassLoader loader = Basic.class.getClassLoader();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   316
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   317
        while (loader.getParent() != null)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   318
            loader = loader.getParent();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   319
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   320
        return new URLClassLoader( new URL[]{
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   321
            new URL(baseURL, "a.jar"),
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   322
            new URL(baseURL, "b.jar"),
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   323
            new URL(baseURL, "c.jar")}, loader );
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
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   326
    /**
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   327
     * HTTP Server to server the jar files.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   328
     */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   329
    static class JarHttpServer implements HttpHandler {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   330
        final String docsDir;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   331
        final HttpServer httpServer;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   332
        int aDotJar, bDotJar, cDotJar;
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
        JarHttpServer(String docsDir) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   335
            this.docsDir = docsDir;
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
            httpServer = HttpServer.create(new InetSocketAddress(0), 0);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   338
            httpServer.createContext("/", this);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   339
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   340
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   341
        void start() throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   342
            httpServer.start();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   343
        }
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
        void stop(int delay) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   346
            httpServer.stop(delay);
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
        InetSocketAddress getAddress() {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   350
            return httpServer.getAddress();
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 reset() {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   354
            aDotJar = bDotJar = cDotJar = 0;
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
        @Override
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   358
        public String toString() {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   359
            return "aDotJar=" + aDotJar + ", bDotJar=" + bDotJar + ", cDotJar=" + cDotJar;
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
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   362
        public void handle(HttpExchange t) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   363
            InputStream is = t.getRequestBody();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   364
            Headers map = t.getRequestHeaders();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   365
            Headers rmap = t.getResponseHeaders();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   366
            URI uri = t.getRequestURI();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   367
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   368
            debug("Server: received request for " + uri);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   369
            String path = uri.getPath();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   370
            if (path.endsWith("a.jar"))
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   371
                aDotJar++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   372
            else if (path.endsWith("b.jar"))
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   373
                bDotJar++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   374
            else if (path.endsWith("c.jar"))
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   375
                cDotJar++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   376
            else
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   377
                System.out.println("Unexpected resource request" + path);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   378
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   379
            while (is.read() != -1);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   380
            is.close();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   381
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   382
            File file = new File(docsDir, path);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   383
            if (!file.exists())
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   384
                throw new RuntimeException("Error: request for " + file);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   385
            long clen = file.length();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   386
            t.sendResponseHeaders (200, clen);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   387
            OutputStream os = t.getResponseBody();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   388
            FileInputStream fis = new FileInputStream(file);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   389
            try {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   390
                byte[] buf = new byte [16 * 1024];
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   391
                int len;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   392
                while ((len=fis.read(buf)) != -1) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   393
                    os.write (buf, 0, len);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   394
                }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   395
            } catch (IOException e) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   396
                e.printStackTrace();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   397
            }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   398
            fis.close();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   399
            os.close();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   400
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   401
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   402
}