jdk/test/sun/misc/JarIndex/metaInfFilenames/Basic.java
author shurailine
Tue, 27 Oct 2015 20:06:02 -0700
changeset 33402 1156d495a525
parent 30820 0d4717a011d3
permissions -rw-r--r--
8140336: Add @modules for exported dependencies to jdk_core tests Reviewed-by: alanb, mchung
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
/*
33402
1156d495a525 8140336: Add @modules for exported dependencies to jdk_core tests
shurailine
parents: 30820
diff changeset
     2
 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
8169
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
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 28522
diff changeset
    28
 * @modules jdk.jartool/sun.tools.jar
33402
1156d495a525 8140336: Add @modules for exported dependencies to jdk_core tests
shurailine
parents: 30820
diff changeset
    29
 *          jdk.httpserver
1156d495a525 8140336: Add @modules for exported dependencies to jdk_core tests
shurailine
parents: 30820
diff changeset
    30
 *          jdk.compiler
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    31
 * @run main/othervm Basic
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    32
 */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    33
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    34
import java.io.IOException;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    35
import java.io.BufferedReader;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    36
import java.io.File;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    37
import java.io.FileInputStream;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    38
import java.io.InputStream;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    39
import java.io.InputStreamReader;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    40
import java.io.OutputStream;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    41
import java.net.InetSocketAddress;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    42
import java.net.URI;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    43
import java.net.URL;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    44
import java.net.URLClassLoader;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    45
import java.util.Arrays;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    46
import java.util.Iterator;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    47
import java.util.ServiceLoader;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    48
import com.sun.net.httpserver.Headers;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    49
import com.sun.net.httpserver.HttpExchange;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    50
import com.sun.net.httpserver.HttpHandler;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    51
import com.sun.net.httpserver.HttpServer;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    52
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    53
/**
22959
2d9d076cee41 8034776: Remove sun.misc.Service
alanb
parents: 11119
diff changeset
    54
 * Verifies the impact of sun.misc.JarIndex.metaInfFilenames on ServiceLoader
2d9d076cee41 8034776: Remove sun.misc.Service
alanb
parents: 11119
diff changeset
    55
 * 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
    56
 *
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    57
 * 1) Compile the test sources:
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    58
 *   jarA:
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    59
 *     META-INF/services/my.happy.land
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    60
 *     com/message/spi/MessageService.java
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    61
 *     a/A.java
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    62
 *   jarB:
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    63
 *     META-INF/JAVA2.DS
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    64
 *     META-INF/services/no.name.service
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    65
 *     b/B.java
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    66
 *   jarC:
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    67
 *     META-INF/fonts.mf
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    68
 *     META-INF/fonts/Company-corporate.ttf
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    69
 *     META-INF/fonts/kidpr.ttf
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    70
 *     META-INF/services/com.message.spi.MessageService
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    71
 *     my/impl/StandardMessageService.java
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    72
 *
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    73
 * 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
    74
 *
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    75
 * 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
    76
 *      with sun.misc.JarIndex.metaInfFilenames=true
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    77
 *
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    78
 * 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
    79
 *
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    80
 * 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
    81
 * 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
    82
 * 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
    83
 *
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    84
 */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    85
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    86
public class Basic {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    87
    static final String slash = File.separator;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    88
    static final String[] testSources =  {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    89
         "jarA" + slash + "a" + slash + "A.java",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    90
         "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
    91
         "jarB" + slash + "b" + slash + "B.java",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    92
         "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
    93
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    94
    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
    95
    static final String testSrcDir = testSrc != null ? testSrc : ".";
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
    96
    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
    97
    static final String testClassesDir = testClasses != null ? testClasses : ".";
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
    static JarHttpServer httpServer;
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
    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
   102
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   103
        // 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
   104
        (new URL("http://localhost/")).openConnection().setDefaultUseCaches(false);
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
        buildTest();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   107
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   108
        try {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   109
            httpServer = new JarHttpServer(testClassesDir);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   110
            httpServer.start();
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
            doTest(httpServer.getAddress());
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   113
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   114
        } catch (IOException ioe) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   115
            ioe.printStackTrace();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   116
        } finally {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   117
            if (httpServer != null) { httpServer.stop(2); }
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
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   120
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   121
    static void buildTest() {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   122
        /* 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
   123
        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
   124
            testSources[i] = testSrcDir + slash + testSources[i];
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   125
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   126
        compile("-d" , testClassesDir,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   127
                "-sourcepath", testSrcDir,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   128
                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
   129
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   130
        /* build the 3 jar files */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   131
        jar("-cf", testClassesDir + slash + "a.jar",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   132
            "-C", testClassesDir, "a",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   133
            "-C", testClassesDir, "com",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   134
            "-C", testSrcDir + slash + "jarA", "META-INF");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   135
        jar("-cf", testClassesDir + slash + "b.jar",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   136
            "-C", testClassesDir, "b",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   137
            "-C", testSrcDir + slash + "jarB", "META-INF");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   138
        jar("-cf", testClassesDir + slash + "c.jar",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   139
            "-C", testClassesDir, "my",
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   140
            "-C", testSrcDir + slash + "jarC", "META-INF");
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
        /* 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
   143
        createIndex(testClassesDir);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   144
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   145
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   146
    /* run jar <args> */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   147
    static void jar(String... args) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   148
        debug("Running: jar " + Arrays.toString(args));
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   149
        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
   150
        if (!jar.run(args)) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   151
            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
   152
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   153
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   154
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   155
    /* run javac <args> */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   156
    static void compile(String... args) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   157
        debug("Running: javac " + Arrays.toString(args));
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 10705
diff changeset
   158
        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
   159
             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
   160
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   161
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   162
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   163
    static String jar;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   164
    static {
28522
c1aedcf211da 8061448: Cleanup sun/misc/JarIndex tests to remove the check for the jre directory
xiaofeya
parents: 22959
diff changeset
   165
        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
   166
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   167
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   168
    /* create the index */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   169
    static void createIndex(String workingDir) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   170
        // 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
   171
        // 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
   172
        debug("Running jar to create the index");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   173
        ProcessBuilder pb = new ProcessBuilder(
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   174
           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
   175
        pb.directory(new File(workingDir));
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   176
        //pd.inheritIO();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   177
        try {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   178
            Process p = pb.start();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   179
            if(p.waitFor() != 0)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   180
                throw new RuntimeException("jar indexing failed");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   181
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   182
            if(debug && p != null) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   183
                String line = null;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   184
                BufferedReader reader =
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   185
                         new BufferedReader(new InputStreamReader(p.getInputStream()));
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
                reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   189
                while((line = reader.readLine()) != null)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   190
                    debug(line);
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
        } catch(InterruptedException ie) { throw new RuntimeException(ie);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   193
        } catch(IOException e) { throw new RuntimeException(e); }
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
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   196
    static final boolean debug = true;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   197
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   198
    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
   199
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   200
    /* service define in c.jar */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   201
    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
   202
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   203
    /* 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
   204
    static final String unknownService = "java.lang.Object";
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
    static void doTest(InetSocketAddress serverAddress) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   207
        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
   208
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   209
        int failed = 0;
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
        // Tests using java.util.SerivceLoader
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   212
        if (!javaUtilServiceLoaderTest(baseURL, messageService, true, false, true)) {
22959
2d9d076cee41 8034776: Remove sun.misc.Service
alanb
parents: 11119
diff changeset
   213
            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
   214
            failed++;
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
        if (!javaUtilServiceLoaderTest(baseURL, unknownService, false, false, false)) {
22959
2d9d076cee41 8034776: Remove sun.misc.Service
alanb
parents: 11119
diff changeset
   217
            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
   218
            failed++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   219
        }
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
        // 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
   222
        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
   223
            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
   224
            failed++;
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
        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
   227
            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
   228
            failed++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   229
        }
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 (failed > 0)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   232
            throw new RuntimeException("Failed: " + failed + " tests");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   233
    }
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
    static boolean javaUtilServiceLoaderTest(URL baseURL,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   236
                                             String serviceClass,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   237
                                             boolean expectToFind,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   238
                                             boolean expectbDotJar,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   239
                                             boolean expectcDotJar) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   240
        debug("----------------------------------");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   241
        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
   242
        URLClassLoader loader = getLoader(baseURL);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   243
        httpServer.reset();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   244
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 10705
diff changeset
   245
        Class<?> messageServiceClass = null;
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   246
        try {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   247
            messageServiceClass = loader.loadClass(serviceClass);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   248
        } catch (ClassNotFoundException cnfe) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   249
            System.err.println(cnfe);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   250
            throw new RuntimeException("Error in test: " + cnfe);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   251
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   252
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 10705
diff changeset
   253
        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
   254
        if (expectToFind && !iterator.hasNext()) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   255
            debug(messageServiceClass + " NOT found.");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   256
            return false;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   257
        }
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
        while (iterator.hasNext()) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   260
            debug("found " + iterator.next() + " " + messageService);
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
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   263
        debug("HttpServer: " + httpServer);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   264
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   265
        if (!expectbDotJar && httpServer.bDotJar > 0) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   266
            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
   267
            return false;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   268
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   269
        if (!expectcDotJar && httpServer.cDotJar > 0) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   270
            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
   271
            return false;
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
        return true;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   275
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   276
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   277
    /* 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
   278
     * com.sun.javafx.scene.text.FontManager */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   279
    static boolean klassLoader(URL baseURL,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   280
                               String resource,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   281
                               boolean expectToFind,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   282
                               boolean expectbDotJar,
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   283
                               boolean expectcDotJar) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   284
        debug("----------------------------------");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   285
        debug("Running test looking for " + resource);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   286
        URLClassLoader loader = getLoader(baseURL);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   287
        httpServer.reset();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   288
11119
6ff03c1202ce 7116722: Miscellaneous warnings sun.misc ( and related classes )
chegar
parents: 10705
diff changeset
   289
        Class<?> ADotAKlass = null;
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   290
        try {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   291
            ADotAKlass = loader.loadClass("a.A");
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   292
        } catch (ClassNotFoundException cnfe) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   293
            System.err.println(cnfe);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   294
            throw new RuntimeException("Error in test: " + cnfe);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   295
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   296
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   297
        URL u = ADotAKlass.getResource(resource);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   298
        if (expectToFind && u == null) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   299
            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
   300
            return false;
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
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   303
        debug("HttpServer: " + httpServer);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   304
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   305
        if (!expectbDotJar && httpServer.bDotJar > 0) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   306
            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
   307
            return false;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   308
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   309
        if (!expectcDotJar && httpServer.cDotJar > 0) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   310
            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
   311
            return false;
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
        return true;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   315
    }
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
    static URLClassLoader getLoader(URL baseURL) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   318
        ClassLoader loader = Basic.class.getClassLoader();
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
        while (loader.getParent() != null)
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   321
            loader = loader.getParent();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   322
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   323
        return new URLClassLoader( new URL[]{
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   324
            new URL(baseURL, "a.jar"),
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   325
            new URL(baseURL, "b.jar"),
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   326
            new URL(baseURL, "c.jar")}, loader );
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
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   329
    /**
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   330
     * HTTP Server to server the jar files.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   331
     */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   332
    static class JarHttpServer implements HttpHandler {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   333
        final String docsDir;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   334
        final HttpServer httpServer;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   335
        int aDotJar, bDotJar, cDotJar;
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
        JarHttpServer(String docsDir) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   338
            this.docsDir = docsDir;
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
            httpServer = HttpServer.create(new InetSocketAddress(0), 0);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   341
            httpServer.createContext("/", this);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   342
        }
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
        void start() throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   345
            httpServer.start();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   346
        }
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
        void stop(int delay) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   349
            httpServer.stop(delay);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   350
        }
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
        InetSocketAddress getAddress() {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   353
            return httpServer.getAddress();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   354
        }
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
        void reset() {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   357
            aDotJar = bDotJar = cDotJar = 0;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   358
        }
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
        @Override
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   361
        public String toString() {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   362
            return "aDotJar=" + aDotJar + ", bDotJar=" + bDotJar + ", cDotJar=" + cDotJar;
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
        public void handle(HttpExchange t) throws IOException {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   366
            InputStream is = t.getRequestBody();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   367
            Headers map = t.getRequestHeaders();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   368
            Headers rmap = t.getResponseHeaders();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   369
            URI uri = t.getRequestURI();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   370
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   371
            debug("Server: received request for " + uri);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   372
            String path = uri.getPath();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   373
            if (path.endsWith("a.jar"))
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   374
                aDotJar++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   375
            else if (path.endsWith("b.jar"))
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   376
                bDotJar++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   377
            else if (path.endsWith("c.jar"))
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   378
                cDotJar++;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   379
            else
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   380
                System.out.println("Unexpected resource request" + path);
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
            while (is.read() != -1);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   383
            is.close();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   384
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   385
            File file = new File(docsDir, path);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   386
            if (!file.exists())
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   387
                throw new RuntimeException("Error: request for " + file);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   388
            long clen = file.length();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   389
            t.sendResponseHeaders (200, clen);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   390
            OutputStream os = t.getResponseBody();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   391
            FileInputStream fis = new FileInputStream(file);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   392
            try {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   393
                byte[] buf = new byte [16 * 1024];
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   394
                int len;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   395
                while ((len=fis.read(buf)) != -1) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   396
                    os.write (buf, 0, len);
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
            } catch (IOException e) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   399
                e.printStackTrace();
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
            fis.close();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   402
            os.close();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   403
        }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   404
    }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents:
diff changeset
   405
}