1 /* 2 * Copyright (c) 2015, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.nio.zipfs; 27 28 import java.io.IOException; 29 import java.io.InputStream; 30 import java.nio.file.Path; 31 import java.util.Arrays; 32 import java.util.HashMap; 33 import java.util.Map; 34 import java.util.TreeMap; 35 import java.util.function.Consumer; 36 import java.util.function.Function; 37 import java.util.jar.Attributes; 38 import java.util.jar.Manifest; 39 import jdk.Version; 40 41 /** 42 * Adds aliasing to ZipFileSystem to support multi-release jar files. An alias map 43 * is created by {@link JarFileSystem#createVersionedLinks(int)}. The map is then 44 * consulted when an entry is looked up in {@link JarFileSystem#getEntry(byte[])} 45 * to determine if the entry has a corresponding versioned entry. If so, the 46 * versioned entry is returned. 47 * 48 * @author Steve Drach 49 */ 50 51 class JarFileSystem extends ZipFileSystem { 52 private Function<byte[],byte[]> lookup; 53 54 @Override 55 Entry getEntry(byte[] path) throws IOException { 56 // check for an alias to a versioned entry 57 byte[] versionedPath = lookup.apply(path); 58 return versionedPath == null ? super.getEntry(path) : super.getEntry(versionedPath); 59 } 60 61 JarFileSystem(ZipFileSystemProvider provider, Path zfpath, Map<String,?> env) 62 throws IOException { 63 super(provider, zfpath, env); 64 lookup = path -> path; // lookup needs to be set before isMultiReleaseJar is called 65 // because it eventually calls getEntry 66 if (isMultiReleaseJar()) { 67 int version; 68 Object o = env.get("multi-release"); 69 if (o instanceof String) { 70 String s = (String)o; 71 if (s.equals("runtime")) { 72 version = jdk.Version.current().major(); 73 } else { 74 version = Integer.parseInt(s); 75 } 76 } else if (o instanceof Integer) { 77 version = (Integer)o; 78 } else if (o instanceof Version) { 79 version = ((Version)o).major(); 80 } else { 81 throw new IllegalArgumentException("env parameter must be String, Integer, " 82 + "or Version"); 83 } 84 lookup = createVersionedLinks(version < 0 ? 0 : version); 85 setReadOnly(); 86 } 87 } 88 89 private boolean isMultiReleaseJar() { 90 try (InputStream is = newInputStream(getBytes("META-INF/MANIFEST.MF"))) { 91 return (new Manifest(is)).getMainAttributes() 92 .containsKey(new Attributes.Name("Multi-Release")); | 1 /* 2 * Copyright (c) 2015, 2016, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.nio.zipfs; 27 28 import java.io.IOException; 29 import java.io.InputStream; 30 import java.nio.file.Path; 31 import java.util.Arrays; 32 import java.util.HashMap; 33 import java.util.Map; 34 import java.util.TreeMap; 35 import java.util.function.Consumer; 36 import java.util.function.Function; 37 import java.util.jar.Attributes; 38 import java.util.jar.Manifest; 39 import java.lang.Runtime.Version; 40 41 /** 42 * Adds aliasing to ZipFileSystem to support multi-release jar files. An alias map 43 * is created by {@link JarFileSystem#createVersionedLinks(int)}. The map is then 44 * consulted when an entry is looked up in {@link JarFileSystem#getEntry(byte[])} 45 * to determine if the entry has a corresponding versioned entry. If so, the 46 * versioned entry is returned. 47 * 48 * @author Steve Drach 49 */ 50 51 class JarFileSystem extends ZipFileSystem { 52 private Function<byte[],byte[]> lookup; 53 54 @Override 55 Entry getEntry(byte[] path) throws IOException { 56 // check for an alias to a versioned entry 57 byte[] versionedPath = lookup.apply(path); 58 return versionedPath == null ? super.getEntry(path) : super.getEntry(versionedPath); 59 } 60 61 JarFileSystem(ZipFileSystemProvider provider, Path zfpath, Map<String,?> env) 62 throws IOException { 63 super(provider, zfpath, env); 64 lookup = path -> path; // lookup needs to be set before isMultiReleaseJar is called 65 // because it eventually calls getEntry 66 if (isMultiReleaseJar()) { 67 int version; 68 Object o = env.get("multi-release"); 69 if (o instanceof String) { 70 String s = (String)o; 71 if (s.equals("runtime")) { 72 version = Version.current().major(); 73 } else { 74 version = Integer.parseInt(s); 75 } 76 } else if (o instanceof Integer) { 77 version = (Integer)o; 78 } else if (o instanceof Version) { 79 version = ((Version)o).major(); 80 } else { 81 throw new IllegalArgumentException("env parameter must be String, Integer, " 82 + "or Version"); 83 } 84 lookup = createVersionedLinks(version < 0 ? 0 : version); 85 setReadOnly(); 86 } 87 } 88 89 private boolean isMultiReleaseJar() { 90 try (InputStream is = newInputStream(getBytes("META-INF/MANIFEST.MF"))) { 91 return (new Manifest(is)).getMainAttributes() 92 .containsKey(new Attributes.Name("Multi-Release")); |