author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 46188 | jdk/src/java.base/share/classes/jdk/internal/module/SystemModuleFinders.java@4586bc5d28d1 |
child 49285 | 4d2e3f5abb48 |
permissions | -rw-r--r-- |
42703 | 1 |
/* |
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
42703 | 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.ByteArrayInputStream; |
|
28 |
import java.io.IOException; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.io.UncheckedIOException; |
|
31 |
import java.lang.module.ModuleDescriptor; |
|
32 |
import java.lang.module.ModuleFinder; |
|
33 |
import java.lang.module.ModuleReader; |
|
34 |
import java.lang.module.ModuleReference; |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
35 |
import java.lang.reflect.Constructor; |
42703 | 36 |
import java.net.URI; |
37 |
import java.net.URLConnection; |
|
38 |
import java.nio.ByteBuffer; |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
39 |
import java.nio.file.Files; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
40 |
import java.nio.file.Path; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
41 |
import java.nio.file.Paths; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
42 |
import java.security.AccessController; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
43 |
import java.security.PrivilegedAction; |
42703 | 44 |
import java.util.ArrayDeque; |
45 |
import java.util.Collections; |
|
46 |
import java.util.Deque; |
|
47 |
import java.util.HashMap; |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
48 |
import java.util.HashSet; |
42703 | 49 |
import java.util.Iterator; |
50 |
import java.util.Map; |
|
51 |
import java.util.Objects; |
|
52 |
import java.util.Optional; |
|
53 |
import java.util.Set; |
|
54 |
import java.util.Spliterator; |
|
55 |
import java.util.function.Consumer; |
|
56 |
import java.util.function.Supplier; |
|
57 |
import java.util.stream.Stream; |
|
58 |
import java.util.stream.StreamSupport; |
|
59 |
||
60 |
import jdk.internal.jimage.ImageLocation; |
|
61 |
import jdk.internal.jimage.ImageReader; |
|
62 |
import jdk.internal.jimage.ImageReaderFactory; |
|
63 |
import jdk.internal.misc.JavaNetUriAccess; |
|
64 |
import jdk.internal.misc.SharedSecrets; |
|
65 |
import jdk.internal.module.ModuleHashes.HashSupplier; |
|
66 |
||
67 |
/** |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
68 |
* The factory for SystemModules objects and for creating ModuleFinder objects |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
69 |
* that find modules in the runtime image. |
42703 | 70 |
* |
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
71 |
* This class supports initializing the module system when the runtime is an |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
72 |
* images build, an exploded build, or an images build with java.base patched |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
73 |
* by an exploded java.base. It also supports a testing mode that re-parses |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
74 |
* the module-info.class resources in the run-time image. |
42703 | 75 |
*/ |
76 |
||
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
77 |
public final class SystemModuleFinders { |
42703 | 78 |
private static final JavaNetUriAccess JNUA = SharedSecrets.getJavaNetUriAccess(); |
79 |
||
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
80 |
private static final boolean USE_FAST_PATH; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
81 |
static { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
82 |
String value = System.getProperty("jdk.system.module.finder.disableFastPath"); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
83 |
if (value == null) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
84 |
USE_FAST_PATH = true; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
85 |
} else { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
86 |
USE_FAST_PATH = (value.length() > 0) && !Boolean.parseBoolean(value); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
87 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
88 |
} |
42703 | 89 |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
90 |
// cached ModuleFinder returned from ofSystem |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
91 |
private static volatile ModuleFinder cachedSystemModuleFinder; |
42703 | 92 |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
93 |
private SystemModuleFinders() { } |
42703 | 94 |
|
95 |
/** |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
96 |
* Returns the SystemModules object to reconstitute all modules. Returns |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
97 |
* null if this is an exploded build or java.base is patched by an exploded |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
98 |
* build. |
42703 | 99 |
*/ |
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
100 |
static SystemModules allSystemModules() { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
101 |
if (USE_FAST_PATH) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
102 |
return SystemModulesMap.allSystemModules(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
103 |
} else { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
104 |
return null; |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42752
diff
changeset
|
105 |
} |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42752
diff
changeset
|
106 |
} |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42752
diff
changeset
|
107 |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
108 |
/** |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
109 |
* Returns a SystemModules object to reconstitute the modules for the |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
110 |
* given initial module. If the initial module is null then return the |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
111 |
* SystemModules object to reconstitute the default modules. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
112 |
* |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
113 |
* Return null if there is no SystemModules class for the initial module, |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
114 |
* this is an exploded build, or java.base is patched by an exploded build. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
115 |
*/ |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
116 |
static SystemModules systemModules(String initialModule) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
117 |
if (USE_FAST_PATH) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
118 |
if (initialModule == null) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
119 |
return SystemModulesMap.defaultSystemModules(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
120 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
121 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
122 |
String[] initialModules = SystemModulesMap.moduleNames(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
123 |
for (int i = 0; i < initialModules.length; i++) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
124 |
String moduleName = initialModules[i]; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
125 |
if (initialModule.equals(moduleName)) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
126 |
String cn = SystemModulesMap.classNames()[i]; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
127 |
try { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
128 |
// one-arg Class.forName as java.base may not be defined |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
129 |
Constructor<?> ctor = Class.forName(cn).getConstructor(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
130 |
return (SystemModules) ctor.newInstance(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
131 |
} catch (Exception e) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
132 |
throw new InternalError(e); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
133 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
134 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
135 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
136 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
137 |
return null; |
42703 | 138 |
} |
139 |
||
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
140 |
/** |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
141 |
* Returns a ModuleFinder that is backed by the given SystemModules object. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
142 |
* |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
143 |
* @apiNote The returned ModuleFinder is thread safe. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
144 |
*/ |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
145 |
static ModuleFinder of(SystemModules systemModules) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
146 |
ModuleDescriptor[] descriptors = systemModules.moduleDescriptors(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
147 |
ModuleTarget[] targets = systemModules.moduleTargets(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
148 |
ModuleHashes[] recordedHashes = systemModules.moduleHashes(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
149 |
ModuleResolution[] moduleResolutions = systemModules.moduleResolutions(); |
42703 | 150 |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
151 |
int moduleCount = descriptors.length; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
152 |
ModuleReference[] mrefs = new ModuleReference[moduleCount]; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
153 |
@SuppressWarnings(value = {"rawtypes", "unchecked"}) |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
154 |
Map.Entry<String, ModuleReference>[] map |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
155 |
= (Map.Entry<String, ModuleReference>[])new Map.Entry[moduleCount]; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
156 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
157 |
Map<String, byte[]> nameToHash = generateNameToHash(recordedHashes); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
158 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
159 |
for (int i = 0; i < moduleCount; i++) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
160 |
String name = descriptors[i].name(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
161 |
HashSupplier hashSupplier = hashSupplier(nameToHash, name); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
162 |
ModuleReference mref = toModuleReference(descriptors[i], |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
163 |
targets[i], |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
164 |
recordedHashes[i], |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
165 |
hashSupplier, |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
166 |
moduleResolutions[i]); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
167 |
mrefs[i] = mref; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
168 |
map[i] = Map.entry(name, mref); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
169 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
170 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
171 |
return new SystemModuleFinder(mrefs, map); |
42703 | 172 |
} |
173 |
||
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
174 |
/** |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
175 |
* Returns the ModuleFinder to find all system modules. Supports both |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
176 |
* images and exploded builds. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
177 |
* |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
178 |
* @apiNote Used by ModuleFinder.ofSystem() |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
179 |
*/ |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
180 |
public static ModuleFinder ofSystem() { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
181 |
ModuleFinder finder = cachedSystemModuleFinder; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
182 |
if (finder != null) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
183 |
return finder; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
184 |
} |
42703 | 185 |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
186 |
// probe to see if this is an images build |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
187 |
String home = System.getProperty("java.home"); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
188 |
Path modules = Paths.get(home, "lib", "modules"); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
189 |
if (Files.isRegularFile(modules)) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
190 |
if (USE_FAST_PATH) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
191 |
SystemModules systemModules = allSystemModules(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
192 |
if (systemModules != null) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
193 |
finder = of(systemModules); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
194 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
195 |
} |
42703 | 196 |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
197 |
// fall back to parsing the module-info.class files in image |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
198 |
if (finder == null) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
199 |
finder = ofModuleInfos(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
200 |
} |
42703 | 201 |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
202 |
cachedSystemModuleFinder = finder; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
203 |
return finder; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
204 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
205 |
} |
42703 | 206 |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
207 |
// exploded build (do not cache module finder) |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
208 |
Path dir = Paths.get(home, "modules"); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
209 |
if (!Files.isDirectory(dir)) |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
210 |
throw new InternalError("Unable to detect the run-time image"); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
211 |
ModuleFinder f = ModulePath.of(ModuleBootstrap.patcher(), dir); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
212 |
return new ModuleFinder() { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
213 |
@Override |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
214 |
public Optional<ModuleReference> find(String name) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
215 |
PrivilegedAction<Optional<ModuleReference>> pa = () -> f.find(name); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
216 |
return AccessController.doPrivileged(pa); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
217 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
218 |
@Override |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
219 |
public Set<ModuleReference> findAll() { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
220 |
PrivilegedAction<Set<ModuleReference>> pa = f::findAll; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
221 |
return AccessController.doPrivileged(pa); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
222 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
223 |
}; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
224 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
225 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
226 |
/** |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
227 |
* Parses the module-info.class of all module in the runtime image and |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
228 |
* returns a ModuleFinder to find the modules. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
229 |
* |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
230 |
* @apiNote The returned ModuleFinder is thread safe. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
231 |
*/ |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
232 |
private static ModuleFinder ofModuleInfos() { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
233 |
// parse the module-info.class in every module |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
234 |
Map<String, ModuleInfo.Attributes> nameToAttributes = new HashMap<>(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
235 |
Map<String, byte[]> nameToHash = new HashMap<>(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
236 |
ImageReader reader = SystemImage.reader(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
237 |
for (String mn : reader.getModuleNames()) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
238 |
ImageLocation loc = reader.findLocation(mn, "module-info.class"); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
239 |
ModuleInfo.Attributes attrs |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
240 |
= ModuleInfo.read(reader.getResourceBuffer(loc), null); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
241 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
242 |
nameToAttributes.put(mn, attrs); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
243 |
ModuleHashes hashes = attrs.recordedHashes(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
244 |
if (hashes != null) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
245 |
for (String name : hashes.names()) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
246 |
nameToHash.computeIfAbsent(name, k -> hashes.hashFor(name)); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
247 |
} |
42703 | 248 |
} |
249 |
} |
|
250 |
||
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
251 |
// create a ModuleReference for each module |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
252 |
Set<ModuleReference> mrefs = new HashSet<>(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
253 |
Map<String, ModuleReference> nameToModule = new HashMap<>(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
254 |
for (Map.Entry<String, ModuleInfo.Attributes> e : nameToAttributes.entrySet()) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
255 |
String mn = e.getKey(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
256 |
ModuleInfo.Attributes attrs = e.getValue(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
257 |
HashSupplier hashSupplier = hashSupplier(nameToHash, mn); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
258 |
ModuleReference mref = toModuleReference(attrs.descriptor(), |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
259 |
attrs.target(), |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
260 |
attrs.recordedHashes(), |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
261 |
hashSupplier, |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
262 |
attrs.moduleResolution()); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
263 |
mrefs.add(mref); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
264 |
nameToModule.put(mn, mref); |
42703 | 265 |
} |
266 |
||
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
267 |
return new SystemModuleFinder(mrefs, nameToModule); |
42703 | 268 |
} |
269 |
||
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
270 |
/** |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
271 |
* A ModuleFinder that finds module in an array or set of modules. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
272 |
*/ |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
273 |
private static class SystemModuleFinder implements ModuleFinder { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
274 |
final Set<ModuleReference> mrefs; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
275 |
final Map<String, ModuleReference> nameToModule; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
276 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
277 |
SystemModuleFinder(ModuleReference[] array, |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
278 |
Map.Entry<String, ModuleReference>[] map) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
279 |
this.mrefs = Set.of(array); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
280 |
this.nameToModule = Map.ofEntries(map); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
281 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
282 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
283 |
SystemModuleFinder(Set<ModuleReference> mrefs, |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
284 |
Map<String, ModuleReference> nameToModule) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
285 |
this.mrefs = Collections.unmodifiableSet(mrefs); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
286 |
this.nameToModule = Collections.unmodifiableMap(nameToModule); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
287 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
288 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
289 |
@Override |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
290 |
public Optional<ModuleReference> find(String name) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
291 |
Objects.requireNonNull(name); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
292 |
return Optional.ofNullable(nameToModule.get(name)); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
293 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
294 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
295 |
@Override |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
296 |
public Set<ModuleReference> findAll() { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
297 |
return mrefs; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
298 |
} |
42703 | 299 |
} |
300 |
||
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
301 |
/** |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
302 |
* Creates a ModuleReference to the system module. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
303 |
*/ |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
304 |
static ModuleReference toModuleReference(ModuleDescriptor descriptor, |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
305 |
ModuleTarget target, |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
306 |
ModuleHashes recordedHashes, |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
307 |
HashSupplier hasher, |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
308 |
ModuleResolution mres) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
309 |
String mn = descriptor.name(); |
42703 | 310 |
URI uri = JNUA.create("jrt", "/".concat(mn)); |
311 |
||
312 |
Supplier<ModuleReader> readerSupplier = new Supplier<>() { |
|
313 |
@Override |
|
314 |
public ModuleReader get() { |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
315 |
return new SystemModuleReader(mn, uri); |
42703 | 316 |
} |
317 |
}; |
|
318 |
||
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
319 |
ModuleReference mref = new ModuleReferenceImpl(descriptor, |
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
320 |
uri, |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
321 |
readerSupplier, |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
322 |
null, |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
323 |
target, |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
324 |
recordedHashes, |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
325 |
hasher, |
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
43712
diff
changeset
|
326 |
mres); |
42703 | 327 |
|
328 |
// may need a reference to a patched module if --patch-module specified |
|
329 |
mref = ModuleBootstrap.patcher().patchIfNeeded(mref); |
|
330 |
||
331 |
return mref; |
|
332 |
} |
|
333 |
||
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
334 |
/** |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
335 |
* Generates a map of module name to hash value. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
336 |
*/ |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
337 |
static Map<String, byte[]> generateNameToHash(ModuleHashes[] recordedHashes) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
338 |
Map<String, byte[]> nameToHash = null; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
339 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
340 |
boolean secondSeen = false; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
341 |
// record the hashes to build HashSupplier |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
342 |
for (ModuleHashes mh : recordedHashes) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
343 |
if (mh != null) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
344 |
// if only one module contain ModuleHashes, use it |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
345 |
if (nameToHash == null) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
346 |
nameToHash = mh.hashes(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
347 |
} else { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
348 |
if (!secondSeen) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
349 |
nameToHash = new HashMap<>(nameToHash); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
350 |
secondSeen = true; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
351 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
352 |
nameToHash.putAll(mh.hashes()); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
353 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
354 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
355 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
356 |
return (nameToHash != null) ? nameToHash : Collections.emptyMap(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
357 |
} |
42703 | 358 |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
359 |
/** |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
360 |
* Returns a HashSupplier that returns the hash of the given module. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
361 |
*/ |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
362 |
static HashSupplier hashSupplier(Map<String, byte[]> nameToHash, String name) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
363 |
byte[] hash = nameToHash.get(name); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
364 |
if (hash != null) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
365 |
// avoid lambda here |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
366 |
return new HashSupplier() { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
367 |
@Override |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
368 |
public byte[] generate(String algorithm) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
369 |
return hash; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
370 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
371 |
}; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
372 |
} else { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
373 |
return null; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
374 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
375 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
376 |
|
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
377 |
/** |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
378 |
* Holder class for the ImageReader |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
379 |
* |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
380 |
* @apiNote This class must be loaded before a security manager is set. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
381 |
*/ |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
382 |
private static class SystemImage { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
383 |
static final ImageReader READER = ImageReaderFactory.getImageReader(); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
384 |
static ImageReader reader() { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
385 |
return READER; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
386 |
} |
42703 | 387 |
} |
388 |
||
389 |
/** |
|
390 |
* A ModuleReader for reading resources from a module linked into the |
|
391 |
* run-time image. |
|
392 |
*/ |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
393 |
private static class SystemModuleReader implements ModuleReader { |
42703 | 394 |
private final String module; |
395 |
private volatile boolean closed; |
|
396 |
||
397 |
/** |
|
398 |
* If there is a security manager set then check permission to |
|
399 |
* connect to the run-time image. |
|
400 |
*/ |
|
401 |
private static void checkPermissionToConnect(URI uri) { |
|
402 |
SecurityManager sm = System.getSecurityManager(); |
|
403 |
if (sm != null) { |
|
404 |
try { |
|
405 |
URLConnection uc = uri.toURL().openConnection(); |
|
406 |
sm.checkPermission(uc.getPermission()); |
|
407 |
} catch (IOException ioe) { |
|
408 |
throw new UncheckedIOException(ioe); |
|
409 |
} |
|
410 |
} |
|
411 |
} |
|
412 |
||
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
413 |
SystemModuleReader(String module, URI uri) { |
42703 | 414 |
checkPermissionToConnect(uri); |
415 |
this.module = module; |
|
416 |
} |
|
417 |
||
418 |
/** |
|
419 |
* Returns the ImageLocation for the given resource, {@code null} |
|
420 |
* if not found. |
|
421 |
*/ |
|
422 |
private ImageLocation findImageLocation(String name) throws IOException { |
|
423 |
Objects.requireNonNull(name); |
|
424 |
if (closed) |
|
425 |
throw new IOException("ModuleReader is closed"); |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42752
diff
changeset
|
426 |
ImageReader imageReader = SystemImage.reader(); |
42703 | 427 |
if (imageReader != null) { |
428 |
return imageReader.findLocation(module, name); |
|
429 |
} else { |
|
430 |
// not an images build |
|
431 |
return null; |
|
432 |
} |
|
433 |
} |
|
434 |
||
435 |
@Override |
|
436 |
public Optional<URI> find(String name) throws IOException { |
|
437 |
ImageLocation location = findImageLocation(name); |
|
438 |
if (location != null) { |
|
439 |
URI u = URI.create("jrt:/" + module + "/" + name); |
|
440 |
return Optional.of(u); |
|
441 |
} else { |
|
442 |
return Optional.empty(); |
|
443 |
} |
|
444 |
} |
|
445 |
||
446 |
@Override |
|
447 |
public Optional<InputStream> open(String name) throws IOException { |
|
448 |
return read(name).map(this::toInputStream); |
|
449 |
} |
|
450 |
||
451 |
private InputStream toInputStream(ByteBuffer bb) { // ## -> ByteBuffer? |
|
452 |
try { |
|
453 |
int rem = bb.remaining(); |
|
454 |
byte[] bytes = new byte[rem]; |
|
455 |
bb.get(bytes); |
|
456 |
return new ByteArrayInputStream(bytes); |
|
457 |
} finally { |
|
458 |
release(bb); |
|
459 |
} |
|
460 |
} |
|
461 |
||
462 |
@Override |
|
463 |
public Optional<ByteBuffer> read(String name) throws IOException { |
|
464 |
ImageLocation location = findImageLocation(name); |
|
465 |
if (location != null) { |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42752
diff
changeset
|
466 |
return Optional.of(SystemImage.reader().getResourceBuffer(location)); |
42703 | 467 |
} else { |
468 |
return Optional.empty(); |
|
469 |
} |
|
470 |
} |
|
471 |
||
472 |
@Override |
|
473 |
public void release(ByteBuffer bb) { |
|
474 |
Objects.requireNonNull(bb); |
|
475 |
ImageReader.releaseByteBuffer(bb); |
|
476 |
} |
|
477 |
||
478 |
@Override |
|
479 |
public Stream<String> list() throws IOException { |
|
480 |
if (closed) |
|
481 |
throw new IOException("ModuleReader is closed"); |
|
482 |
||
483 |
Spliterator<String> s = new ModuleContentSpliterator(module); |
|
484 |
return StreamSupport.stream(s, false); |
|
485 |
} |
|
486 |
||
487 |
@Override |
|
488 |
public void close() { |
|
489 |
// nothing else to do |
|
490 |
closed = true; |
|
491 |
} |
|
492 |
} |
|
493 |
||
494 |
/** |
|
495 |
* A Spliterator for traversing the resources of a module linked into the |
|
496 |
* run-time image. |
|
497 |
*/ |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
44359
diff
changeset
|
498 |
private static class ModuleContentSpliterator implements Spliterator<String> { |
42703 | 499 |
final String moduleRoot; |
500 |
final Deque<ImageReader.Node> stack; |
|
501 |
Iterator<ImageReader.Node> iterator; |
|
502 |
||
503 |
ModuleContentSpliterator(String module) throws IOException { |
|
504 |
moduleRoot = "/modules/" + module; |
|
505 |
stack = new ArrayDeque<>(); |
|
506 |
||
507 |
// push the root node to the stack to get started |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42752
diff
changeset
|
508 |
ImageReader.Node dir = SystemImage.reader().findNode(moduleRoot); |
42703 | 509 |
if (dir == null || !dir.isDirectory()) |
510 |
throw new IOException(moduleRoot + " not a directory"); |
|
511 |
stack.push(dir); |
|
512 |
iterator = Collections.emptyIterator(); |
|
513 |
} |
|
514 |
||
515 |
/** |
|
516 |
* Returns the name of the next non-directory node or {@code null} if |
|
517 |
* there are no remaining nodes to visit. |
|
518 |
*/ |
|
519 |
private String next() throws IOException { |
|
520 |
for (;;) { |
|
521 |
while (iterator.hasNext()) { |
|
522 |
ImageReader.Node node = iterator.next(); |
|
523 |
String name = node.getName(); |
|
524 |
if (node.isDirectory()) { |
|
525 |
// build node |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42752
diff
changeset
|
526 |
ImageReader.Node dir = SystemImage.reader().findNode(name); |
42703 | 527 |
assert dir.isDirectory(); |
528 |
stack.push(dir); |
|
529 |
} else { |
|
530 |
// strip /modules/$MODULE/ prefix |
|
531 |
return name.substring(moduleRoot.length() + 1); |
|
532 |
} |
|
533 |
} |
|
534 |
||
535 |
if (stack.isEmpty()) { |
|
536 |
return null; |
|
537 |
} else { |
|
538 |
ImageReader.Node dir = stack.poll(); |
|
539 |
assert dir.isDirectory(); |
|
540 |
iterator = dir.getChildren().iterator(); |
|
541 |
} |
|
542 |
} |
|
543 |
} |
|
544 |
||
545 |
@Override |
|
546 |
public boolean tryAdvance(Consumer<? super String> action) { |
|
547 |
String next; |
|
548 |
try { |
|
549 |
next = next(); |
|
550 |
} catch (IOException ioe) { |
|
551 |
throw new UncheckedIOException(ioe); |
|
552 |
} |
|
553 |
if (next != null) { |
|
554 |
action.accept(next); |
|
555 |
return true; |
|
556 |
} else { |
|
557 |
return false; |
|
558 |
} |
|
559 |
} |
|
560 |
||
561 |
@Override |
|
562 |
public Spliterator<String> trySplit() { |
|
563 |
return null; |
|
564 |
} |
|
565 |
||
566 |
@Override |
|
567 |
public int characteristics() { |
|
568 |
return Spliterator.DISTINCT + Spliterator.NONNULL + Spliterator.IMMUTABLE; |
|
569 |
} |
|
570 |
||
571 |
@Override |
|
572 |
public long estimateSize() { |
|
573 |
return Long.MAX_VALUE; |
|
574 |
} |
|
575 |
} |
|
576 |
} |