Skip to content

Commit f9b94e8

Browse files
committed
[GR-34740] [GR-34739] Implement ResourcesFeature Module awareness.
PullRequest: graal/10289
2 parents d79ae59 + 54006e3 commit f9b94e8

File tree

27 files changed

+435
-298
lines changed

27 files changed

+435
-298
lines changed

substratevm/mx.substratevm/mx_substratevm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def _run_graalvm_cmd(cmd_args, config, nonZeroIsFatal=True, out=None, err=None,
177177
config_args += ['--components=' + ','.join(c.name for c in components)]
178178
dynamic_imports = [x for x, _ in mx.get_dynamic_imports()]
179179
if dynamic_imports:
180-
config_args += ['--dynamicimports', ','.join(dynamic_imports)]
180+
config_args += ['--dynamicimports=' + ','.join(dynamic_imports)]
181181
primary_suite_dir = None
182182

183183
args = config_args + cmd_args

substratevm/src/com.oracle.svm.core.jdk11/src/com/oracle/svm/core/jdk11/Target_java_lang_Module_JDK11OrLater.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@
4444
@TargetClass(value = java.lang.Module.class, onlyWith = JDK11OrLater.class)
4545
public final class Target_java_lang_Module_JDK11OrLater {
4646

47+
@Alias private String name;
48+
4749
@SuppressWarnings("static-method")
4850
@Substitute
49-
public InputStream getResourceAsStream(String name) {
50-
ResourceStorageEntry res = Resources.get(name);
51+
public InputStream getResourceAsStream(String resourceName) {
52+
ResourceStorageEntry res = Resources.get(name, resourceName);
5153
return res == null ? null : new ByteArrayInputStream(res.getData().get(0));
5254
}
5355

substratevm/src/com.oracle.svm.core.jdk11/src/com/oracle/svm/core/jdk11/Target_java_lang_module_ModuleReference.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

substratevm/src/com.oracle.svm.core.jdk11/src/com/oracle/svm/core/jdk11/Target_jdk_internal_loader_BuiltinClassLoader.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.io.IOException;
2828
import java.io.InputStream;
29+
import java.lang.module.ModuleReference;
2930
import java.net.URL;
3031
import java.util.Enumeration;
3132
import java.util.List;
@@ -57,7 +58,7 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
5758

5859
@Substitute
5960
public URL findResource(String mn, String name) {
60-
return ResourcesHelper.nameToResourceURL(name);
61+
return ResourcesHelper.nameToResourceURL(mn, name);
6162
}
6263

6364
@Substitute
@@ -81,8 +82,8 @@ private List<URL> findMiscResource(String name) {
8182
}
8283

8384
@Substitute
84-
private URL findResource(Target_java_lang_module_ModuleReference mref, String name) {
85-
return ResourcesHelper.nameToResourceURL(name);
85+
private URL findResource(ModuleReference mref, String name) {
86+
return ResourcesHelper.nameToResourceURL(mref.descriptor().name(), name);
8687
}
8788

8889
@Substitute

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/ClassLoaderSupport.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package com.oracle.svm.core;
2626

27+
import java.io.InputStream;
2728
import java.util.List;
2829
import java.util.Locale;
2930
import java.util.ResourceBundle;
@@ -47,5 +48,16 @@ public boolean isNativeImageClassLoader(ClassLoader classLoader) {
4748

4849
protected abstract boolean isNativeImageClassLoaderImpl(ClassLoader classLoader);
4950

51+
public interface ResourceCollector {
52+
53+
boolean isIncluded(String moduleName, String resourceName);
54+
55+
void addResource(String moduleName, String resourceName, InputStream resourceStream);
56+
57+
void addDirectoryResource(String moduleName, String dir, String content);
58+
}
59+
60+
public abstract void collectResources(ResourceCollector resourceCollector);
61+
5062
public abstract List<ResourceBundle> getResourceBundle(String bundleName, Locale locale);
5163
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,9 @@ public Enum<?>[] getEnumConstantsShared() {
688688

689689
@Substitute
690690
public InputStream getResourceAsStream(String resourceName) {
691-
return Resources.createInputStream(resolveName(resourceName));
691+
String moduleName = module == null ? null : SubstrateUtil.cast(module, Target_java_lang_Module.class).name;
692+
String resolvedName = resolveName(resourceName);
693+
return Resources.createInputStream(moduleName, resolvedName);
692694
}
693695

694696
@KeepOriginal

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Resources.java

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.List;
3939

4040
import org.graalvm.collections.EconomicMap;
41+
import org.graalvm.collections.Pair;
4142
import org.graalvm.nativeimage.ImageSingletons;
4243
import org.graalvm.nativeimage.Platform;
4344
import org.graalvm.nativeimage.Platforms;
@@ -65,17 +66,22 @@ public static Resources singleton() {
6566
return ImageSingletons.lookup(Resources.class);
6667
}
6768

68-
/** The hosted map used to collect registered resources. */
69-
private final EconomicMap<String, ResourceStorageEntry> resources = ImageHeapMap.create();
69+
/**
70+
* The hosted map used to collect registered resources. Using a {@link Pair} of (moduleName,
71+
* resourceName) provides implementations for {@code hashCode()} and {@code equals()} needed for
72+
* the map keys.
73+
*/
74+
private final EconomicMap<Pair<String, String>, ResourceStorageEntry> resources = ImageHeapMap.create();
7075

7176
Resources() {
7277
}
7378

74-
public EconomicMap<String, ResourceStorageEntry> resources() {
79+
public EconomicMap<Pair<String, String>, ResourceStorageEntry> resources() {
7580
return resources;
7681
}
7782

7883
public static byte[] inputStreamToByteArray(InputStream is) {
84+
// TODO: Replace this with is.readAllBytes() once Java 8 support is removed
7985
byte[] arr = new byte[4096];
8086
int pos = 0;
8187
try {
@@ -100,29 +106,40 @@ public static byte[] inputStreamToByteArray(InputStream is) {
100106
return data;
101107
}
102108

103-
private static void addEntry(String resourceName, boolean isDirectory, byte[] data) {
109+
private static void addEntry(String moduleName, String resourceName, boolean isDirectory, byte[] data) {
104110
Resources support = singleton();
105-
ResourceStorageEntry entry = support.resources.get(resourceName);
111+
Pair<String, String> key = Pair.create(moduleName, resourceName);
112+
ResourceStorageEntry entry = support.resources.get(key);
106113
if (entry == null) {
107114
entry = new ResourceStorageEntry(isDirectory);
108-
support.resources.put(resourceName, entry);
115+
support.resources.put(key, entry);
109116
}
110117
entry.getData().add(data);
111118
}
112119

113120
@Platforms(Platform.HOSTED_ONLY.class)
114121
public static void registerResource(String resourceName, InputStream is) {
115-
addEntry(resourceName, false, inputStreamToByteArray(is));
122+
registerResource(null, resourceName, is);
123+
}
124+
125+
@Platforms(Platform.HOSTED_ONLY.class)
126+
public static void registerResource(String moduleName, String resourceName, InputStream is) {
127+
addEntry(moduleName, resourceName, false, inputStreamToByteArray(is));
116128
}
117129

118130
@Platforms(Platform.HOSTED_ONLY.class)
119131
public static void registerDirectoryResource(String resourceDirName, String content) {
132+
registerDirectoryResource(null, resourceDirName, content);
133+
}
134+
135+
@Platforms(Platform.HOSTED_ONLY.class)
136+
public static void registerDirectoryResource(String moduleName, String resourceDirName, String content) {
120137
/*
121138
* A directory content represents the names of all files and subdirectories located in the
122139
* specified directory, separated with new line delimiter and joined into one string which
123140
* is later converted into a byte array and placed into the resources map.
124141
*/
125-
addEntry(resourceDirName, true, content.getBytes());
142+
addEntry(moduleName, resourceDirName, true, content.getBytes());
126143
}
127144

128145
/**
@@ -135,7 +152,11 @@ public static String toCanonicalForm(String resourceName) {
135152
}
136153

137154
public static ResourceStorageEntry get(String name) {
138-
return singleton().resources.get(name);
155+
return singleton().resources.get(Pair.createRight(name));
156+
}
157+
158+
public static ResourceStorageEntry get(String moduleName, String resourceName) {
159+
return singleton().resources.get(Pair.create(moduleName, resourceName));
139160
}
140161

141162
private static URL createURL(String resourceName, int index) {
@@ -154,21 +175,29 @@ protected URLConnection openConnection(URL url) {
154175
}
155176

156177
public static URL createURL(String resourceName) {
178+
return createURL(null, resourceName);
179+
}
180+
181+
public static URL createURL(String moduleName, String resourceName) {
157182
if (resourceName == null) {
158183
return null;
159184
}
160185

161-
Enumeration<URL> urls = createURLs(toCanonicalForm(resourceName));
186+
Enumeration<URL> urls = createURLs(moduleName, toCanonicalForm(resourceName));
162187
return urls.hasMoreElements() ? urls.nextElement() : null;
163188
}
164189

165-
/* Avoid pulling in the URL class when only an InputStream is needed. */
166190
public static InputStream createInputStream(String resourceName) {
191+
return createInputStream(null, resourceName);
192+
}
193+
194+
/* Avoid pulling in the URL class when only an InputStream is needed. */
195+
public static InputStream createInputStream(String moduleName, String resourceName) {
167196
if (resourceName == null) {
168197
return null;
169198
}
170199

171-
ResourceStorageEntry entry = Resources.get(toCanonicalForm(resourceName));
200+
ResourceStorageEntry entry = Resources.get(moduleName, toCanonicalForm(resourceName));
172201
if (entry == null) {
173202
return null;
174203
}
@@ -177,12 +206,16 @@ public static InputStream createInputStream(String resourceName) {
177206
}
178207

179208
public static Enumeration<URL> createURLs(String resourceName) {
209+
return createURLs(null, resourceName);
210+
}
211+
212+
public static Enumeration<URL> createURLs(String moduleName, String resourceName) {
180213
if (resourceName == null) {
181214
return null;
182215
}
183216

184217
String canonicalResourceName = toCanonicalForm(resourceName);
185-
ResourceStorageEntry entry = Resources.get(canonicalResourceName);
218+
ResourceStorageEntry entry = Resources.get(moduleName, canonicalResourceName);
186219
if (entry == null) {
187220
return Collections.emptyEnumeration();
188221
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/ResourcesHelper.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
*/
2525
package com.oracle.svm.core.jdk;
2626

27-
import com.oracle.svm.core.util.VMError;
28-
import org.graalvm.nativeimage.ImageSingletons;
29-
3027
import java.io.IOException;
3128
import java.io.InputStream;
3229
import java.net.URL;
@@ -36,6 +33,10 @@
3633
import java.util.Enumeration;
3734
import java.util.List;
3835

36+
import org.graalvm.nativeimage.ImageSingletons;
37+
38+
import com.oracle.svm.core.util.VMError;
39+
3940
@SuppressWarnings("unchecked")
4041
public class ResourcesHelper {
4142

@@ -72,6 +73,10 @@ public static URL nameToResourceURL(String resourceName) {
7273
return Resources.createURL(resourceName);
7374
}
7475

76+
public static URL nameToResourceURL(String moduleName, String resourceName) {
77+
return Resources.createURL(moduleName, resourceName);
78+
}
79+
7580
public static InputStream nameToResourceInputStream(String resourceName) throws IOException {
7681
URL url = nameToResourceURL(resourceName);
7782
return url != null ? url.openStream() : null;

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_java_lang_Module.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
*/
2525
package com.oracle.svm.core.jdk;
2626

27+
import com.oracle.svm.core.annotate.Alias;
2728
import com.oracle.svm.core.annotate.TargetClass;
2829

2930
@SuppressWarnings("unused")
3031
@TargetClass(className = "java.lang.Module", onlyWith = JDK11OrLater.class)
3132
public final class Target_java_lang_Module {
33+
@Alias //
34+
public String name;
3235
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_jdk_internal_loader_Loader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private Class<?> findClassInModuleOrNull(Target_jdk_internal_loader_Loader_Loade
5454

5555
@Substitute
5656
protected URL findResource(String mn, String name) {
57-
return ResourcesHelper.nameToResourceURL(name);
57+
return ResourcesHelper.nameToResourceURL(mn, name);
5858
}
5959

6060
@Substitute

0 commit comments

Comments
 (0)