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") 39 .orElseThrow(() -> 40 new RuntimeException("jar tool not found") 41 ); 42 43 static String jarName = "a.jar"; 44 static String INDEX = "META-INF/INDEX.LIST"; 45 static String SERVICES = "META-INF/services"; 46 static String contents = 47 System.getProperty("test.src") + File.separatorChar + "jarcontents"; 48 49 static void run(String ... args) { 50 if (JAR_TOOL.run(System.out, System.err, args) != 0) 51 throw new Error("jar failed: args=" + Arrays.toString(args)); 52 } 53 54 static void copy(File from, File to) throws IOException { 55 FileInputStream in = new FileInputStream(from); 56 FileOutputStream out = new FileOutputStream(to); 57 try { 58 byte[] buf = new byte[8192]; 59 int n; 60 while ((n = in.read(buf)) != -1) 61 out.write(buf, 0, n); 62 } finally { 63 in.close(); 64 out.close(); 65 } 66 } 67 68 static boolean contains(File jarFile, String entryName) 69 throws IOException { 70 ZipFile zf = new ZipFile(jarFile); 71 if ( zf != null ) { 72 boolean result = zf.getEntry(entryName) != null; 73 zf.close(); 74 return result; 75 } 76 return false; 77 } 78 79 static void checkContains(File jarFile, String entryName) 80 throws IOException { 81 if (! contains(jarFile, entryName)) 82 throw new Error(String.format("expected jar %s to contain %s", 83 jarFile, entryName)); 84 } 85 86 static void testIndex(String jarName) throws IOException { 87 System.err.printf("jarName=%s%n", jarName); 88 89 File jar = new File(jarName); 90 91 // Create a jar to be indexed. 92 run("cf", jarName, "-C", contents, SERVICES); 93 94 for (int i = 0; i < 2; i++) { 95 run("i", jarName); 96 checkContains(jar, INDEX); 97 checkContains(jar, SERVICES); 98 } 99 100 JarFile f = new JarFile(jarName); 101 BufferedReader index = 102 new BufferedReader( 103 new InputStreamReader( 104 f.getInputStream(f.getJarEntry(INDEX)))); 105 String line; 106 while ((line = index.readLine()) != null) { 107 if (line.equals(SERVICES)) { 108 index.close(); 109 f.close(); 110 return; 111 } 112 } 113 index.close(); 114 f.close(); 115 throw new Error(SERVICES + " not indexed."); 116 } 117 118 public static void main(String[] args) throws IOException { 119 testIndex("a.jar"); // a path with parent == null 120 testIndex("./a.zip"); // a path with parent != null 121 122 // Try indexing a jar in the default temp directory. 123 File tmpFile = File.createTempFile("MetaInf", null, null); 124 try { 125 testIndex(tmpFile.getPath()); 126 } finally { 127 tmpFile.delete(); 128 } 129 } 130 }