1 /* |
|
2 * Copyright (c) 2015, 2019, 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.lang.Runtime.Version; |
|
31 import java.nio.file.NoSuchFileException; |
|
32 import java.nio.file.Path; |
|
33 import java.util.Arrays; |
|
34 import java.util.HashMap; |
|
35 import java.util.Map; |
|
36 import java.util.TreeMap; |
|
37 import java.util.function.Consumer; |
|
38 import java.util.function.Function; |
|
39 import java.util.jar.Attributes; |
|
40 import java.util.jar.Manifest; |
|
41 |
|
42 /** |
|
43 * Adds aliasing to ZipFileSystem to support multi-release jar files. An alias map |
|
44 * is created by {@link JarFileSystem#createVersionedLinks(int)}. The map is then |
|
45 * consulted when an entry is looked up in {@link JarFileSystem#getInode(byte[])} |
|
46 * to determine if the entry has a corresponding versioned entry. If so, the |
|
47 * versioned entry is returned. |
|
48 * |
|
49 * @author Steve Drach |
|
50 */ |
|
51 class JarFileSystem extends ZipFileSystem { |
|
52 // lookup needs to be initialized because isMultiReleaseJar is called before createVersionedLinks |
|
53 private Function<byte[], byte[]> lookup = path -> path; |
|
54 |
|
55 @Override |
|
56 IndexNode getInode(byte[] path) { |
|
57 // check for an alias to a versioned entry |
|
58 return super.getInode(lookup.apply(path)); |
|
59 } |
|
60 |
|
61 JarFileSystem(ZipFileSystemProvider provider, Path zfpath, Map<String,?> env) throws IOException { |
|
62 super(provider, zfpath, env); |
|
63 Object o = getRuntimeVersion(env); |
|
64 if (isMultiReleaseJar() && (o != null)) { |
|
65 int version; |
|
66 if (o instanceof String) { |
|
67 String s = (String)o; |
|
68 if (s.equals("runtime")) { |
|
69 version = Runtime.version().feature(); |
|
70 } else if (s.matches("^[1-9][0-9]*$")) { |
|
71 version = Version.parse(s).feature(); |
|
72 } else { |
|
73 throw new IllegalArgumentException("Invalid runtime version"); |
|
74 } |
|
75 } else if (o instanceof Integer) { |
|
76 version = Version.parse(((Integer)o).toString()).feature(); |
|
77 } else if (o instanceof Version) { |
|
78 version = ((Version)o).feature(); |
|
79 } else { |
|
80 throw new IllegalArgumentException("env parameter must be String, Integer, " |
|
81 + "or Version"); |
|
82 } |
|
83 createVersionedLinks(version < 0 ? 0 : version); |
|
84 setReadOnly(); |
|
85 } |
|
86 } |
|
87 |
|
88 /** |
|
89 * Utility method to get the release version for a multi-release JAR. It |
|
90 * first checks the documented property {@code releaseVersion} and if not |
|
91 * found checks the original property {@code multi-release} |
|
92 * @param env ZIP FS map |
|
93 * @return release version or null if it is not specified |
|
94 */ |
|
95 private Object getRuntimeVersion(Map<String, ?> env) { |
|
96 Object o = null; |
|
97 if (env.containsKey(ZipFileSystemProvider.PROPERTY_RELEASE_VERSION)) { |
|
98 o = env.get(ZipFileSystemProvider.PROPERTY_RELEASE_VERSION); |
|
99 } else { |
|
100 o = env.get(ZipFileSystemProvider.PROPERTY_MULTI_RELEASE); |
|
101 } |
|
102 return o; |
|
103 } |
|
104 |
|
105 private boolean isMultiReleaseJar() throws IOException { |
|
106 try (InputStream is = newInputStream(getBytes("/META-INF/MANIFEST.MF"))) { |
|
107 String multiRelease = new Manifest(is).getMainAttributes() |
|
108 .getValue(Attributes.Name.MULTI_RELEASE); |
|
109 return "true".equalsIgnoreCase(multiRelease); |
|
110 } catch (NoSuchFileException x) { |
|
111 return false; |
|
112 } |
|
113 } |
|
114 |
|
115 /** |
|
116 * create a map of aliases for versioned entries, for example: |
|
117 * version/PackagePrivate.class -> META-INF/versions/9/version/PackagePrivate.class |
|
118 * version/PackagePrivate.java -> META-INF/versions/9/version/PackagePrivate.java |
|
119 * version/Version.class -> META-INF/versions/10/version/Version.class |
|
120 * version/Version.java -> META-INF/versions/10/version/Version.java |
|
121 * |
|
122 * then wrap the map in a function that getEntry can use to override root |
|
123 * entry lookup for entries that have corresponding versioned entries |
|
124 */ |
|
125 private void createVersionedLinks(int version) { |
|
126 IndexNode verdir = getInode(getBytes("/META-INF/versions")); |
|
127 // nothing to do, if no /META-INF/versions |
|
128 if (verdir == null) { |
|
129 return; |
|
130 } |
|
131 // otherwise, create a map and for each META-INF/versions/{n} directory |
|
132 // put all the leaf inodes, i.e. entries, into the alias map |
|
133 // possibly shadowing lower versioned entries |
|
134 HashMap<IndexNode, byte[]> aliasMap = new HashMap<>(); |
|
135 getVersionMap(version, verdir).values().forEach(versionNode -> |
|
136 walk(versionNode.child, entryNode -> |
|
137 aliasMap.put( |
|
138 getOrCreateInode(getRootName(entryNode, versionNode), entryNode.isdir), |
|
139 entryNode.name)) |
|
140 ); |
|
141 lookup = path -> { |
|
142 byte[] entry = aliasMap.get(IndexNode.keyOf(path)); |
|
143 return entry == null ? path : entry; |
|
144 }; |
|
145 } |
|
146 |
|
147 /** |
|
148 * create a sorted version map of version -> inode, for inodes <= max version |
|
149 * 9 -> META-INF/versions/9 |
|
150 * 10 -> META-INF/versions/10 |
|
151 */ |
|
152 private TreeMap<Integer, IndexNode> getVersionMap(int version, IndexNode metaInfVersions) { |
|
153 TreeMap<Integer,IndexNode> map = new TreeMap<>(); |
|
154 IndexNode child = metaInfVersions.child; |
|
155 while (child != null) { |
|
156 Integer key = getVersion(child, metaInfVersions); |
|
157 if (key != null && key <= version) { |
|
158 map.put(key, child); |
|
159 } |
|
160 child = child.sibling; |
|
161 } |
|
162 return map; |
|
163 } |
|
164 |
|
165 /** |
|
166 * extract the integer version number -- META-INF/versions/9 returns 9 |
|
167 */ |
|
168 private Integer getVersion(IndexNode inode, IndexNode metaInfVersions) { |
|
169 try { |
|
170 byte[] fullName = inode.name; |
|
171 return Integer.parseInt(getString(Arrays |
|
172 .copyOfRange(fullName, metaInfVersions.name.length + 1, fullName.length))); |
|
173 } catch (NumberFormatException x) { |
|
174 // ignore this even though it might indicate issues with the JAR structure |
|
175 return null; |
|
176 } |
|
177 } |
|
178 |
|
179 /** |
|
180 * walk the IndexNode tree processing all leaf nodes |
|
181 */ |
|
182 private void walk(IndexNode inode, Consumer<IndexNode> consumer) { |
|
183 if (inode == null) return; |
|
184 if (inode.isDir()) { |
|
185 walk(inode.child, consumer); |
|
186 } else { |
|
187 consumer.accept(inode); |
|
188 } |
|
189 walk(inode.sibling, consumer); |
|
190 } |
|
191 |
|
192 /** |
|
193 * extract the root name from a versioned entry name |
|
194 * given inode for META-INF/versions/9/foo/bar.class |
|
195 * and prefix META-INF/versions/9/ |
|
196 * returns foo/bar.class |
|
197 */ |
|
198 private byte[] getRootName(IndexNode inode, IndexNode prefix) { |
|
199 byte[] fullName = inode.name; |
|
200 return Arrays.copyOfRange(fullName, prefix.name.length, fullName.length); |
|
201 } |
|
202 } |
|