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