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