jdk/test/tools/jar/index/MetaInf.java
author xdono
Tue, 28 Jul 2009 12:12:54 -0700
changeset 3288 db82a42da273
parent 3218 855a6257b729
child 5506 202f599c92aa
permissions -rw-r--r--
6862919: Update copyright year Summary: Update copyright for files that have been modified in 2009, up to 07/09 Reviewed-by: tbell, ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
3288
db82a42da273 6862919: Update copyright year
xdono
parents: 3218
diff changeset
     2
 * Copyright 2003-2009 Sun Microsystems, Inc.  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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    26
 * @bug 4408526 6854795
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Index the non-meta files in META-INF, such as META-INF/services.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    31
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.tools.jar.Main;
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    34
import java.util.zip.ZipFile;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class MetaInf {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static String jarName = "a.jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static String INDEX = "META-INF/INDEX.LIST";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static String SERVICES = "META-INF/services";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static String contents =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        System.getProperty("test.src") + File.separatorChar + "jarcontents";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    44
    static void run(String ... args) {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    45
        if (! new Main(System.out, System.err, "jar").run(args))
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    46
            throw new Error("jar failed: args=" + Arrays.toString(args));
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    47
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    49
    static void copy(File from, File to) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    50
        FileInputStream in = new FileInputStream(from);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    51
        FileOutputStream out = new FileOutputStream(to);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    52
        try {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    53
            byte[] buf = new byte[8192];
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    54
            int n;
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    55
            while ((n = in.read(buf)) != -1)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    56
                out.write(buf, 0, n);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    57
        } finally {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    58
            in.close();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    59
            out.close();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    60
        }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    61
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    62
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    63
    static boolean contains(File jarFile, String entryName)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    64
        throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    65
        return new ZipFile(jarFile).getEntry(entryName) != null;
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 void checkContains(File jarFile, String entryName)
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    69
        throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    70
        if (! contains(jarFile, entryName))
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    71
            throw new Error(String.format("expected jar %s to contain %s",
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    72
                                          jarFile, entryName));
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    73
    }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    74
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    75
    static void testIndex(String jarName) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    76
        System.err.printf("jarName=%s%n", jarName);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    77
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    78
        File jar = new File(jarName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // Create a jar to be indexed.
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    81
        run("cf", jarName, "-C", contents, SERVICES);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    82
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    83
        for (int i = 0; i < 2; i++) {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    84
            run("i", jarName);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    85
            checkContains(jar, INDEX);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
    86
            checkContains(jar, SERVICES);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        JarFile f = new JarFile(jarName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        BufferedReader index =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            new BufferedReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    new InputStreamReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                            f.getInputStream(f.getJarEntry(INDEX))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        String line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        while ((line = index.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (line.equals(SERVICES)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        throw new Error(SERVICES + " not indexed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
3218
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   102
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   103
    public static void main(String[] args) throws IOException {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   104
        testIndex("a.jar");             // a path with parent == null
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   105
        testIndex("./a.zip");           // a path with parent != null
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   106
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   107
        // Try indexing a jar in the default temp directory.
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   108
        File tmpFile = File.createTempFile("MetaInf", null, null);
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   109
        try {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   110
            testIndex(tmpFile.getPath());
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   111
        } finally {
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   112
            tmpFile.delete();
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   113
        }
855a6257b729 6854795: Miscellaneous improvements to "jar"
martin
parents: 2
diff changeset
   114
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
}