jdk/test/tools/jar/index/MetaInf.java
author mchung
Wed, 12 Oct 2016 15:41:00 -0700
changeset 41484 834b7539ada3
parent 30820 0d4717a011d3
permissions -rw-r--r--
8164689: Retrofit jar, jlink, jmod as a ToolProvider Reviewed-by: alanb, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5813
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3288
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3288
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3288
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    26
 * @bug 4408526 6854795
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    27
 * @modules jdk.jartool
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Index the non-meta files in META-INF, such as META-INF/services.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    32
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.jar.*;
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    34
import java.util.spi.ToolProvider;
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    35
import java.util.zip.ZipFile;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class MetaInf {
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    38
    static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    39
        .orElseThrow(() ->
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    40
            new RuntimeException("jar tool not found")
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    41
        );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static String jarName = "a.jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static String INDEX = "META-INF/INDEX.LIST";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static String SERVICES = "META-INF/services";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static String contents =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        System.getProperty("test.src") + File.separatorChar + "jarcontents";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    49
    static void run(String ... args) {
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 30820
diff changeset
    50
        if (JAR_TOOL.run(System.out, System.err, args) != 0)
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    51
            throw new Error("jar failed: args=" + Arrays.toString(args));
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    52
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    54
    static void copy(File from, File to) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    55
        FileInputStream in = new FileInputStream(from);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    56
        FileOutputStream out = new FileOutputStream(to);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    57
        try {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    58
            byte[] buf = new byte[8192];
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    59
            int n;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    60
            while ((n = in.read(buf)) != -1)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    61
                out.write(buf, 0, n);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    62
        } finally {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    63
            in.close();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    64
            out.close();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    65
        }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    66
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    67
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    68
    static boolean contains(File jarFile, String entryName)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    69
        throws IOException {
5813
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
    70
        ZipFile zf = new ZipFile(jarFile);
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
    71
        if ( zf != null ) {
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
    72
            boolean result = zf.getEntry(entryName) != null;
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
    73
            zf.close();
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
    74
            return result;
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
    75
        }
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
    76
        return false;
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    77
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    78
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    79
    static void checkContains(File jarFile, String entryName)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    80
        throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    81
        if (! contains(jarFile, entryName))
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    82
            throw new Error(String.format("expected jar %s to contain %s",
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    83
                                          jarFile, entryName));
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    84
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    85
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    86
    static void testIndex(String jarName) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    87
        System.err.printf("jarName=%s%n", jarName);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    88
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    89
        File jar = new File(jarName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // Create a jar to be indexed.
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    92
        run("cf", jarName, "-C", contents, SERVICES);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    93
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    94
        for (int i = 0; i < 2; i++) {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    95
            run("i", jarName);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    96
            checkContains(jar, INDEX);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    97
            checkContains(jar, SERVICES);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        JarFile f = new JarFile(jarName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        BufferedReader index =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            new BufferedReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    new InputStreamReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                            f.getInputStream(f.getJarEntry(INDEX))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        String line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        while ((line = index.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (line.equals(SERVICES)) {
5813
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
   108
                index.close();
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
   109
                f.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
5813
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
   113
        index.close();
8e455cd3e807 6962617: Testcase changes, cleanup of problem list for jdk_tools targets
ohair
parents: 5506
diff changeset
   114
        f.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        throw new Error(SERVICES + " not indexed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   117
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   118
    public static void main(String[] args) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   119
        testIndex("a.jar");             // a path with parent == null
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   120
        testIndex("./a.zip");           // a path with parent != null
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   121
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   122
        // Try indexing a jar in the default temp directory.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   123
        File tmpFile = File.createTempFile("MetaInf", null, null);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   124
        try {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   125
            testIndex(tmpFile.getPath());
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   126
        } finally {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   127
            tmpFile.delete();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   128
        }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   129
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
}