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