author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 46096 | jdk/src/java.base/share/classes/jdk/internal/module/ModulePatcher.java@62c77b334012 |
child 51327 | a19fda433921 |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
36511 | 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 |
package jdk.internal.module; |
|
26 |
||
27 |
import java.io.Closeable; |
|
28 |
import java.io.File; |
|
29 |
import java.io.IOError; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.InputStream; |
|
32 |
import java.io.UncheckedIOException; |
|
33 |
import java.lang.module.ModuleDescriptor; |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
34 |
import java.lang.module.ModuleDescriptor.Builder; |
36511 | 35 |
import java.lang.module.ModuleReader; |
36 |
import java.lang.module.ModuleReference; |
|
37 |
import java.net.MalformedURLException; |
|
38 |
import java.net.URI; |
|
39 |
import java.net.URL; |
|
40 |
import java.nio.ByteBuffer; |
|
41 |
import java.nio.file.Files; |
|
42 |
import java.nio.file.Path; |
|
43 |
import java.nio.file.Paths; |
|
44 |
import java.util.ArrayList; |
|
45 |
import java.util.Collections; |
|
46 |
import java.util.HashMap; |
|
47 |
import java.util.HashSet; |
|
48 |
import java.util.List; |
|
49 |
import java.util.Map; |
|
50 |
import java.util.Optional; |
|
51 |
import java.util.Set; |
|
52 |
import java.util.jar.JarEntry; |
|
53 |
import java.util.jar.JarFile; |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
54 |
import java.util.stream.Collectors; |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
55 |
import java.util.stream.Stream; |
36511 | 56 |
|
36674
7ab530dd6f10
8149122: Move sun.misc.URLClassPath and Resouce to an internal package
chegar
parents:
36511
diff
changeset
|
57 |
import jdk.internal.loader.Resource; |
36511 | 58 |
import jdk.internal.misc.JavaLangModuleAccess; |
59 |
import jdk.internal.misc.SharedSecrets; |
|
60 |
import sun.net.www.ParseUtil; |
|
61 |
||
62 |
||
63 |
/** |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
64 |
* Provides support for patching modules, mostly the boot layer. |
36511 | 65 |
*/ |
66 |
||
67 |
public final class ModulePatcher { |
|
68 |
||
69 |
private static final JavaLangModuleAccess JLMA |
|
70 |
= SharedSecrets.getJavaLangModuleAccess(); |
|
71 |
||
72 |
// module name -> sequence of patches (directories or JAR files) |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
73 |
private final Map<String, List<Path>> map; |
36511 | 74 |
|
75 |
/** |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
76 |
* Initialize the module patcher with the given map. The map key is |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
77 |
* the module name, the value is a list of path strings. |
36511 | 78 |
*/ |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
79 |
public ModulePatcher(Map<String, List<String>> input) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
80 |
if (input.isEmpty()) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
81 |
this.map = Collections.emptyMap(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
82 |
} else { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
83 |
Map<String, List<Path>> map = new HashMap<>(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
84 |
for (Map.Entry<String, List<String>> e : input.entrySet()) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
85 |
String mn = e.getKey(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
86 |
List<Path> paths = e.getValue().stream() |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
87 |
.map(Paths::get) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
88 |
.collect(Collectors.toList()); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
89 |
map.put(mn, paths); |
36511 | 90 |
} |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
91 |
this.map = map; |
36511 | 92 |
} |
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
37779
diff
changeset
|
93 |
} |
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
37779
diff
changeset
|
94 |
|
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
37779
diff
changeset
|
95 |
/** |
36511 | 96 |
* Returns a module reference that interposes on the given module if |
97 |
* needed. If there are no patches for the given module then the module |
|
98 |
* reference is simply returned. Otherwise the patches for the module |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
99 |
* are scanned (to find any new packages) and a new module reference is |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
100 |
* returned. |
36511 | 101 |
* |
102 |
* @throws UncheckedIOException if an I/O error is detected |
|
103 |
*/ |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
104 |
public ModuleReference patchIfNeeded(ModuleReference mref) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
105 |
// if there are no patches for the module then nothing to do |
36511 | 106 |
ModuleDescriptor descriptor = mref.descriptor(); |
107 |
String mn = descriptor.name(); |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
108 |
List<Path> paths = map.get(mn); |
36511 | 109 |
if (paths == null) |
110 |
return mref; |
|
111 |
||
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
112 |
// Scan the JAR file or directory tree to get the set of packages. |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
113 |
// For automatic modules then packages that do not contain class files |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
114 |
// must be ignored. |
36511 | 115 |
Set<String> packages = new HashSet<>(); |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
116 |
boolean isAutomatic = descriptor.isAutomatic(); |
36511 | 117 |
try { |
118 |
for (Path file : paths) { |
|
119 |
if (Files.isRegularFile(file)) { |
|
120 |
||
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36674
diff
changeset
|
121 |
// JAR file - do not open as a multi-release JAR as this |
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36674
diff
changeset
|
122 |
// is not supported by the boot class loader |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
123 |
try (JarFile jf = new JarFile(file.toString())) { |
36511 | 124 |
jf.stream() |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
125 |
.filter(e -> !e.isDirectory() |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
126 |
&& (!isAutomatic || e.getName().endsWith(".class"))) |
36511 | 127 |
.map(e -> toPackageName(file, e)) |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
128 |
.filter(Checks::isPackageName) |
36511 | 129 |
.forEach(packages::add); |
130 |
} |
|
131 |
||
132 |
} else if (Files.isDirectory(file)) { |
|
133 |
||
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
134 |
// exploded directory without following sym links |
36511 | 135 |
Path top = file; |
136 |
Files.find(top, Integer.MAX_VALUE, |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
137 |
((path, attrs) -> attrs.isRegularFile())) |
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
138 |
.filter(path -> (!isAutomatic |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
139 |
|| path.toString().endsWith(".class")) |
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
140 |
&& !isHidden(path)) |
36511 | 141 |
.map(path -> toPackageName(top, path)) |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
142 |
.filter(Checks::isPackageName) |
36511 | 143 |
.forEach(packages::add); |
144 |
||
145 |
} |
|
146 |
} |
|
147 |
||
148 |
} catch (IOException ioe) { |
|
149 |
throw new UncheckedIOException(ioe); |
|
150 |
} |
|
151 |
||
152 |
// if there are new packages then we need a new ModuleDescriptor |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
153 |
packages.removeAll(descriptor.packages()); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
154 |
if (!packages.isEmpty()) { |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
155 |
Builder builder = JLMA.newModuleBuilder(descriptor.name(), |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
156 |
/*strict*/ false, |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
157 |
descriptor.modifiers()); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
158 |
if (!descriptor.isAutomatic()) { |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
159 |
descriptor.requires().forEach(builder::requires); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
160 |
descriptor.exports().forEach(builder::exports); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
161 |
descriptor.opens().forEach(builder::opens); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
162 |
descriptor.uses().forEach(builder::uses); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
163 |
} |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
164 |
descriptor.provides().forEach(builder::provides); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
165 |
|
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
166 |
descriptor.version().ifPresent(builder::version); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
167 |
descriptor.mainClass().ifPresent(builder::mainClass); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
168 |
|
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
169 |
// original + new packages |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
170 |
builder.packages(descriptor.packages()); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
171 |
builder.packages(packages); |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
172 |
|
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42774
diff
changeset
|
173 |
descriptor = builder.build(); |
36511 | 174 |
} |
175 |
||
37779
7c84df693837
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36674
diff
changeset
|
176 |
// return a module reference to the patched module |
36511 | 177 |
URI location = mref.location().orElse(null); |
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
178 |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
179 |
ModuleTarget target = null; |
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
180 |
ModuleHashes recordedHashes = null; |
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
181 |
ModuleHashes.HashSupplier hasher = null; |
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
182 |
ModuleResolution mres = null; |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
183 |
if (mref instanceof ModuleReferenceImpl) { |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
184 |
ModuleReferenceImpl impl = (ModuleReferenceImpl)mref; |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
185 |
target = impl.moduleTarget(); |
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
186 |
recordedHashes = impl.recordedHashes(); |
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
187 |
hasher = impl.hasher(); |
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
188 |
mres = impl.moduleResolution(); |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
189 |
} |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
190 |
|
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
191 |
return new ModuleReferenceImpl(descriptor, |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
192 |
location, |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
193 |
() -> new PatchedModuleReader(paths, mref), |
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
194 |
this, |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
195 |
target, |
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
196 |
recordedHashes, |
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
197 |
hasher, |
42703
20c39ea4a507
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42338
diff
changeset
|
198 |
mres); |
36511 | 199 |
|
200 |
} |
|
201 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
202 |
/** |
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45004
diff
changeset
|
203 |
* Returns true is this module patcher has patches. |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
204 |
*/ |
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45004
diff
changeset
|
205 |
public boolean hasPatches() { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
45004
diff
changeset
|
206 |
return !map.isEmpty(); |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
207 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41817
diff
changeset
|
208 |
|
42774
74bcf37d15d8
8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents:
42703
diff
changeset
|
209 |
/* |
74bcf37d15d8
8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents:
42703
diff
changeset
|
210 |
* Returns the names of the patched modules. |
74bcf37d15d8
8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents:
42703
diff
changeset
|
211 |
*/ |
74bcf37d15d8
8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents:
42703
diff
changeset
|
212 |
Set<String> patchedModules() { |
74bcf37d15d8
8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents:
42703
diff
changeset
|
213 |
return map.keySet(); |
74bcf37d15d8
8168836: Minor clean up on warning/error messages on --add-exports and --add-reads
mchung
parents:
42703
diff
changeset
|
214 |
} |
36511 | 215 |
|
216 |
/** |
|
217 |
* A ModuleReader that reads resources from a patched module. |
|
218 |
* |
|
219 |
* This class is public so as to expose the findResource method to the |
|
220 |
* built-in class loaders and avoid locating the resource twice during |
|
221 |
* class loading (once to locate the resource, the second to gets the |
|
222 |
* URL for the CodeSource). |
|
223 |
*/ |
|
224 |
public static class PatchedModuleReader implements ModuleReader { |
|
225 |
private final List<ResourceFinder> finders; |
|
226 |
private final ModuleReference mref; |
|
227 |
private final URL delegateCodeSourceURL; |
|
228 |
private volatile ModuleReader delegate; |
|
229 |
||
230 |
/** |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
231 |
* Creates the ModuleReader to reads resources in a patched module. |
36511 | 232 |
*/ |
233 |
PatchedModuleReader(List<Path> patches, ModuleReference mref) { |
|
234 |
List<ResourceFinder> finders = new ArrayList<>(); |
|
235 |
boolean initialized = false; |
|
236 |
try { |
|
237 |
for (Path file : patches) { |
|
238 |
if (Files.isRegularFile(file)) { |
|
239 |
finders.add(new JarResourceFinder(file)); |
|
240 |
} else { |
|
241 |
finders.add(new ExplodedResourceFinder(file)); |
|
242 |
} |
|
243 |
} |
|
244 |
initialized = true; |
|
245 |
} catch (IOException ioe) { |
|
246 |
throw new UncheckedIOException(ioe); |
|
247 |
} finally { |
|
248 |
// close all ResourceFinder in the event of an error |
|
249 |
if (!initialized) closeAll(finders); |
|
250 |
} |
|
251 |
||
252 |
this.finders = finders; |
|
253 |
this.mref = mref; |
|
254 |
this.delegateCodeSourceURL = codeSourceURL(mref); |
|
255 |
} |
|
256 |
||
257 |
/** |
|
258 |
* Closes all resource finders. |
|
259 |
*/ |
|
260 |
private static void closeAll(List<ResourceFinder> finders) { |
|
261 |
for (ResourceFinder finder : finders) { |
|
262 |
try { finder.close(); } catch (IOException ioe) { } |
|
263 |
} |
|
264 |
} |
|
265 |
||
266 |
/** |
|
267 |
* Returns the code source URL for the given module. |
|
268 |
*/ |
|
269 |
private static URL codeSourceURL(ModuleReference mref) { |
|
270 |
try { |
|
271 |
Optional<URI> ouri = mref.location(); |
|
272 |
if (ouri.isPresent()) |
|
273 |
return ouri.get().toURL(); |
|
274 |
} catch (MalformedURLException e) { } |
|
275 |
return null; |
|
276 |
} |
|
277 |
||
278 |
/** |
|
279 |
* Returns the ModuleReader to delegate to when the resource is not |
|
280 |
* found in a patch location. |
|
281 |
*/ |
|
282 |
private ModuleReader delegate() throws IOException { |
|
283 |
ModuleReader r = delegate; |
|
284 |
if (r == null) { |
|
285 |
synchronized (this) { |
|
286 |
r = delegate; |
|
287 |
if (r == null) { |
|
288 |
delegate = r = mref.open(); |
|
289 |
} |
|
290 |
} |
|
291 |
} |
|
292 |
return r; |
|
293 |
} |
|
294 |
||
295 |
/** |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
296 |
* Finds a resources in the patch locations. Returns null if not found |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
297 |
* or the name is "module-info.class" as that cannot be overridden. |
36511 | 298 |
*/ |
299 |
private Resource findResourceInPatch(String name) throws IOException { |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
300 |
if (!name.equals("module-info.class")) { |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
301 |
for (ResourceFinder finder : finders) { |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
302 |
Resource r = finder.find(name); |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
303 |
if (r != null) |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
304 |
return r; |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
305 |
} |
36511 | 306 |
} |
307 |
return null; |
|
308 |
} |
|
309 |
||
310 |
/** |
|
311 |
* Finds a resource of the given name in the patched module. |
|
312 |
*/ |
|
313 |
public Resource findResource(String name) throws IOException { |
|
314 |
||
315 |
// patch locations |
|
316 |
Resource r = findResourceInPatch(name); |
|
317 |
if (r != null) |
|
318 |
return r; |
|
319 |
||
320 |
// original module |
|
321 |
ByteBuffer bb = delegate().read(name).orElse(null); |
|
322 |
if (bb == null) |
|
323 |
return null; |
|
324 |
||
325 |
return new Resource() { |
|
326 |
private <T> T shouldNotGetHere(Class<T> type) { |
|
327 |
throw new InternalError("should not get here"); |
|
328 |
} |
|
329 |
@Override |
|
330 |
public String getName() { |
|
331 |
return shouldNotGetHere(String.class); |
|
332 |
} |
|
333 |
@Override |
|
334 |
public URL getURL() { |
|
335 |
return shouldNotGetHere(URL.class); |
|
336 |
} |
|
337 |
@Override |
|
338 |
public URL getCodeSourceURL() { |
|
339 |
return delegateCodeSourceURL; |
|
340 |
} |
|
341 |
@Override |
|
342 |
public ByteBuffer getByteBuffer() throws IOException { |
|
343 |
return bb; |
|
344 |
} |
|
345 |
@Override |
|
346 |
public InputStream getInputStream() throws IOException { |
|
347 |
return shouldNotGetHere(InputStream.class); |
|
348 |
} |
|
349 |
@Override |
|
350 |
public int getContentLength() throws IOException { |
|
351 |
return shouldNotGetHere(int.class); |
|
352 |
} |
|
353 |
}; |
|
354 |
} |
|
355 |
||
356 |
@Override |
|
357 |
public Optional<URI> find(String name) throws IOException { |
|
358 |
Resource r = findResourceInPatch(name); |
|
359 |
if (r != null) { |
|
360 |
URI uri = URI.create(r.getURL().toString()); |
|
361 |
return Optional.of(uri); |
|
362 |
} else { |
|
363 |
return delegate().find(name); |
|
364 |
} |
|
365 |
} |
|
366 |
||
367 |
@Override |
|
368 |
public Optional<InputStream> open(String name) throws IOException { |
|
369 |
Resource r = findResourceInPatch(name); |
|
370 |
if (r != null) { |
|
371 |
return Optional.of(r.getInputStream()); |
|
372 |
} else { |
|
373 |
return delegate().open(name); |
|
374 |
} |
|
375 |
} |
|
376 |
||
377 |
@Override |
|
378 |
public Optional<ByteBuffer> read(String name) throws IOException { |
|
379 |
Resource r = findResourceInPatch(name); |
|
380 |
if (r != null) { |
|
381 |
ByteBuffer bb = r.getByteBuffer(); |
|
382 |
assert !bb.isDirect(); |
|
383 |
return Optional.of(bb); |
|
384 |
} else { |
|
385 |
return delegate().read(name); |
|
386 |
} |
|
387 |
} |
|
388 |
||
389 |
@Override |
|
390 |
public void release(ByteBuffer bb) { |
|
391 |
if (bb.isDirect()) { |
|
392 |
try { |
|
393 |
delegate().release(bb); |
|
394 |
} catch (IOException ioe) { |
|
395 |
throw new InternalError(ioe); |
|
396 |
} |
|
397 |
} |
|
398 |
} |
|
399 |
||
400 |
@Override |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
401 |
public Stream<String> list() throws IOException { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
402 |
Stream<String> s = delegate().list(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
403 |
for (ResourceFinder finder : finders) { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
404 |
s = Stream.concat(s, finder.list()); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
405 |
} |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
406 |
return s.distinct(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
407 |
} |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
408 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
409 |
@Override |
36511 | 410 |
public void close() throws IOException { |
411 |
closeAll(finders); |
|
412 |
delegate().close(); |
|
413 |
} |
|
414 |
} |
|
415 |
||
416 |
||
417 |
/** |
|
418 |
* A resource finder that find resources in a patch location. |
|
419 |
*/ |
|
420 |
private static interface ResourceFinder extends Closeable { |
|
421 |
Resource find(String name) throws IOException; |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
422 |
Stream<String> list() throws IOException; |
36511 | 423 |
} |
424 |
||
425 |
||
426 |
/** |
|
427 |
* A ResourceFinder that finds resources in a JAR file. |
|
428 |
*/ |
|
429 |
private static class JarResourceFinder implements ResourceFinder { |
|
430 |
private final JarFile jf; |
|
431 |
private final URL csURL; |
|
432 |
||
433 |
JarResourceFinder(Path path) throws IOException { |
|
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
434 |
this.jf = new JarFile(path.toString()); |
36511 | 435 |
this.csURL = path.toUri().toURL(); |
436 |
} |
|
437 |
||
438 |
@Override |
|
439 |
public void close() throws IOException { |
|
440 |
jf.close(); |
|
441 |
} |
|
442 |
||
443 |
@Override |
|
444 |
public Resource find(String name) throws IOException { |
|
445 |
JarEntry entry = jf.getJarEntry(name); |
|
446 |
if (entry == null) |
|
447 |
return null; |
|
448 |
||
449 |
return new Resource() { |
|
450 |
@Override |
|
451 |
public String getName() { |
|
452 |
return name; |
|
453 |
} |
|
454 |
@Override |
|
455 |
public URL getURL() { |
|
456 |
String encodedPath = ParseUtil.encodePath(name, false); |
|
457 |
try { |
|
458 |
return new URL("jar:" + csURL + "!/" + encodedPath); |
|
459 |
} catch (MalformedURLException e) { |
|
460 |
return null; |
|
461 |
} |
|
462 |
} |
|
463 |
@Override |
|
464 |
public URL getCodeSourceURL() { |
|
465 |
return csURL; |
|
466 |
} |
|
467 |
@Override |
|
468 |
public ByteBuffer getByteBuffer() throws IOException { |
|
469 |
byte[] bytes = getInputStream().readAllBytes(); |
|
470 |
return ByteBuffer.wrap(bytes); |
|
471 |
} |
|
472 |
@Override |
|
473 |
public InputStream getInputStream() throws IOException { |
|
474 |
return jf.getInputStream(entry); |
|
475 |
} |
|
476 |
@Override |
|
477 |
public int getContentLength() throws IOException { |
|
478 |
long size = entry.getSize(); |
|
479 |
return (size > Integer.MAX_VALUE) ? -1 : (int) size; |
|
480 |
} |
|
481 |
}; |
|
482 |
} |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
483 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
484 |
@Override |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
485 |
public Stream<String> list() throws IOException { |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
486 |
return jf.stream().map(JarEntry::getName); |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
487 |
} |
36511 | 488 |
} |
489 |
||
490 |
||
491 |
/** |
|
492 |
* A ResourceFinder that finds resources on the file system. |
|
493 |
*/ |
|
494 |
private static class ExplodedResourceFinder implements ResourceFinder { |
|
495 |
private final Path dir; |
|
496 |
||
497 |
ExplodedResourceFinder(Path dir) { |
|
498 |
this.dir = dir; |
|
499 |
} |
|
500 |
||
501 |
@Override |
|
502 |
public void close() { } |
|
503 |
||
504 |
@Override |
|
505 |
public Resource find(String name) throws IOException { |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
506 |
Path file = Resources.toFilePath(dir, name); |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
507 |
if (file != null) { |
45004
ea3137042a61
8178380: Module system implementation refresh (5/2017)
alanb
parents:
44545
diff
changeset
|
508 |
return newResource(name, dir, file); |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
509 |
} else { |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
510 |
return null; |
36511 | 511 |
} |
512 |
} |
|
513 |
||
514 |
private Resource newResource(String name, Path top, Path file) { |
|
515 |
return new Resource() { |
|
516 |
@Override |
|
517 |
public String getName() { |
|
518 |
return name; |
|
519 |
} |
|
520 |
@Override |
|
521 |
public URL getURL() { |
|
522 |
try { |
|
523 |
return file.toUri().toURL(); |
|
524 |
} catch (IOException | IOError e) { |
|
525 |
return null; |
|
526 |
} |
|
527 |
} |
|
528 |
@Override |
|
529 |
public URL getCodeSourceURL() { |
|
530 |
try { |
|
531 |
return top.toUri().toURL(); |
|
532 |
} catch (IOException | IOError e) { |
|
533 |
return null; |
|
534 |
} |
|
535 |
} |
|
536 |
@Override |
|
537 |
public ByteBuffer getByteBuffer() throws IOException { |
|
538 |
return ByteBuffer.wrap(Files.readAllBytes(file)); |
|
539 |
} |
|
540 |
@Override |
|
541 |
public InputStream getInputStream() throws IOException { |
|
542 |
return Files.newInputStream(file); |
|
543 |
} |
|
544 |
@Override |
|
545 |
public int getContentLength() throws IOException { |
|
546 |
long size = Files.size(file); |
|
547 |
return (size > Integer.MAX_VALUE) ? -1 : (int)size; |
|
548 |
} |
|
549 |
}; |
|
550 |
} |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
551 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
552 |
@Override |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
553 |
public Stream<String> list() throws IOException { |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
554 |
return Files.walk(dir, Integer.MAX_VALUE) |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
555 |
.map(f -> Resources.toResourceName(dir, f)) |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
556 |
.filter(s -> s.length() > 0); |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
557 |
} |
36511 | 558 |
} |
559 |
||
560 |
||
561 |
/** |
|
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
562 |
* Derives a package name from the file path of an entry in an exploded patch |
36511 | 563 |
*/ |
564 |
private static String toPackageName(Path top, Path file) { |
|
565 |
Path entry = top.relativize(file); |
|
566 |
Path parent = entry.getParent(); |
|
567 |
if (parent == null) { |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
568 |
return warnIfModuleInfo(top, entry.toString()); |
36511 | 569 |
} else { |
570 |
return parent.toString().replace(File.separatorChar, '.'); |
|
571 |
} |
|
572 |
} |
|
573 |
||
574 |
/** |
|
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
575 |
* Returns true if the given file exists and is a hidden file |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
576 |
*/ |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
577 |
private boolean isHidden(Path file) { |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
578 |
try { |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
579 |
return Files.isHidden(file); |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
580 |
} catch (IOException ioe) { |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
581 |
return false; |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
582 |
} |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
583 |
} |
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
584 |
|
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
44359
diff
changeset
|
585 |
/** |
36511 | 586 |
* Derives a package name from the name of an entry in a JAR file. |
587 |
*/ |
|
588 |
private static String toPackageName(Path file, JarEntry entry) { |
|
589 |
String name = entry.getName(); |
|
590 |
int index = name.lastIndexOf("/"); |
|
591 |
if (index == -1) { |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
592 |
return warnIfModuleInfo(file, name); |
36511 | 593 |
} else { |
594 |
return name.substring(0, index).replace('/', '.'); |
|
595 |
} |
|
596 |
} |
|
597 |
||
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
598 |
private static String warnIfModuleInfo(Path file, String e) { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
599 |
if (e.equals("module-info.class")) |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
40261
diff
changeset
|
600 |
System.err.println("WARNING: " + e + " ignored in patch: " + file); |
36511 | 601 |
return ""; |
602 |
} |
|
36674
7ab530dd6f10
8149122: Move sun.misc.URLClassPath and Resouce to an internal package
chegar
parents:
36511
diff
changeset
|
603 |
} |