Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion substratevm/mx.substratevm/mx_substratevm.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def _run_graalvm_cmd(cmd_args, config, nonZeroIsFatal=True, out=None, err=None,
config_args += ['--components=' + ','.join(c.name for c in components)]
dynamic_imports = [x for x, _ in mx.get_dynamic_imports()]
if dynamic_imports:
config_args += ['--dynamicimports', ','.join(dynamic_imports)]
config_args += ['--dynamicimports=' + ','.join(dynamic_imports)]
primary_suite_dir = None

args = config_args + cmd_args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@
@TargetClass(value = java.lang.Module.class, onlyWith = JDK11OrLater.class)
public final class Target_java_lang_Module_JDK11OrLater {

@Alias private String name;

@SuppressWarnings("static-method")
@Substitute
public InputStream getResourceAsStream(String name) {
ResourceStorageEntry res = Resources.get(name);
public InputStream getResourceAsStream(String resourceName) {
ResourceStorageEntry res = Resources.get(name, resourceName);
return res == null ? null : new ByteArrayInputStream(res.getData().get(0));
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.module.ModuleReference;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;
Expand Down Expand Up @@ -57,7 +58,7 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE

@Substitute
public URL findResource(String mn, String name) {
return ResourcesHelper.nameToResourceURL(name);
return ResourcesHelper.nameToResourceURL(mn, name);
}

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

@Substitute
private URL findResource(Target_java_lang_module_ModuleReference mref, String name) {
return ResourcesHelper.nameToResourceURL(name);
private URL findResource(ModuleReference mref, String name) {
return ResourcesHelper.nameToResourceURL(mref.descriptor().name(), name);
}

@Substitute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package com.oracle.svm.core;

import java.io.InputStream;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
Expand All @@ -47,5 +48,16 @@ public boolean isNativeImageClassLoader(ClassLoader classLoader) {

protected abstract boolean isNativeImageClassLoaderImpl(ClassLoader classLoader);

public interface ResourceCollector {

boolean isIncluded(String moduleName, String resourceName);

void addResource(String moduleName, String resourceName, InputStream resourceStream);

void addDirectoryResource(String moduleName, String dir, String content);
}

public abstract void collectResources(ResourceCollector resourceCollector);

public abstract List<ResourceBundle> getResourceBundle(String bundleName, Locale locale);
}
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,9 @@ public Enum<?>[] getEnumConstantsShared() {

@Substitute
public InputStream getResourceAsStream(String resourceName) {
return Resources.createInputStream(resolveName(resourceName));
String moduleName = module == null ? null : SubstrateUtil.cast(module, Target_java_lang_Module.class).name;
String resolvedName = resolveName(resourceName);
return Resources.createInputStream(moduleName, resolvedName);
}

@KeepOriginal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.List;

import org.graalvm.collections.EconomicMap;
import org.graalvm.collections.Pair;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
Expand Down Expand Up @@ -65,17 +66,22 @@ public static Resources singleton() {
return ImageSingletons.lookup(Resources.class);
}

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

Resources() {
}

public EconomicMap<String, ResourceStorageEntry> resources() {
public EconomicMap<Pair<String, String>, ResourceStorageEntry> resources() {
return resources;
}

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

private static void addEntry(String resourceName, boolean isDirectory, byte[] data) {
private static void addEntry(String moduleName, String resourceName, boolean isDirectory, byte[] data) {
Resources support = singleton();
ResourceStorageEntry entry = support.resources.get(resourceName);
Pair<String, String> key = Pair.create(moduleName, resourceName);
ResourceStorageEntry entry = support.resources.get(key);
if (entry == null) {
entry = new ResourceStorageEntry(isDirectory);
support.resources.put(resourceName, entry);
support.resources.put(key, entry);
}
entry.getData().add(data);
}

@Platforms(Platform.HOSTED_ONLY.class)
public static void registerResource(String resourceName, InputStream is) {
addEntry(resourceName, false, inputStreamToByteArray(is));
registerResource(null, resourceName, is);
}

@Platforms(Platform.HOSTED_ONLY.class)
public static void registerResource(String moduleName, String resourceName, InputStream is) {
addEntry(moduleName, resourceName, false, inputStreamToByteArray(is));
}

@Platforms(Platform.HOSTED_ONLY.class)
public static void registerDirectoryResource(String resourceDirName, String content) {
registerDirectoryResource(null, resourceDirName, content);
}

@Platforms(Platform.HOSTED_ONLY.class)
public static void registerDirectoryResource(String moduleName, String resourceDirName, String content) {
/*
* A directory content represents the names of all files and subdirectories located in the
* specified directory, separated with new line delimiter and joined into one string which
* is later converted into a byte array and placed into the resources map.
*/
addEntry(resourceDirName, true, content.getBytes());
addEntry(moduleName, resourceDirName, true, content.getBytes());
}

/**
Expand All @@ -135,7 +152,11 @@ public static String toCanonicalForm(String resourceName) {
}

public static ResourceStorageEntry get(String name) {
return singleton().resources.get(name);
return singleton().resources.get(Pair.createRight(name));
}

public static ResourceStorageEntry get(String moduleName, String resourceName) {
return singleton().resources.get(Pair.create(moduleName, resourceName));
}

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

public static URL createURL(String resourceName) {
return createURL(null, resourceName);
}

public static URL createURL(String moduleName, String resourceName) {
if (resourceName == null) {
return null;
}

Enumeration<URL> urls = createURLs(toCanonicalForm(resourceName));
Enumeration<URL> urls = createURLs(moduleName, toCanonicalForm(resourceName));
return urls.hasMoreElements() ? urls.nextElement() : null;
}

/* Avoid pulling in the URL class when only an InputStream is needed. */
public static InputStream createInputStream(String resourceName) {
return createInputStream(null, resourceName);
}

/* Avoid pulling in the URL class when only an InputStream is needed. */
public static InputStream createInputStream(String moduleName, String resourceName) {
if (resourceName == null) {
return null;
}

ResourceStorageEntry entry = Resources.get(toCanonicalForm(resourceName));
ResourceStorageEntry entry = Resources.get(moduleName, toCanonicalForm(resourceName));
if (entry == null) {
return null;
}
Expand All @@ -177,12 +206,16 @@ public static InputStream createInputStream(String resourceName) {
}

public static Enumeration<URL> createURLs(String resourceName) {
return createURLs(null, resourceName);
}

public static Enumeration<URL> createURLs(String moduleName, String resourceName) {
if (resourceName == null) {
return null;
}

String canonicalResourceName = toCanonicalForm(resourceName);
ResourceStorageEntry entry = Resources.get(canonicalResourceName);
ResourceStorageEntry entry = Resources.get(moduleName, canonicalResourceName);
if (entry == null) {
return Collections.emptyEnumeration();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
*/
package com.oracle.svm.core.jdk;

import com.oracle.svm.core.util.VMError;
import org.graalvm.nativeimage.ImageSingletons;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
Expand All @@ -36,6 +33,10 @@
import java.util.Enumeration;
import java.util.List;

import org.graalvm.nativeimage.ImageSingletons;

import com.oracle.svm.core.util.VMError;

@SuppressWarnings("unchecked")
public class ResourcesHelper {

Expand Down Expand Up @@ -72,6 +73,10 @@ public static URL nameToResourceURL(String resourceName) {
return Resources.createURL(resourceName);
}

public static URL nameToResourceURL(String moduleName, String resourceName) {
return Resources.createURL(moduleName, resourceName);
}

public static InputStream nameToResourceInputStream(String resourceName) throws IOException {
URL url = nameToResourceURL(resourceName);
return url != null ? url.openStream() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
*/
package com.oracle.svm.core.jdk;

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.TargetClass;

@SuppressWarnings("unused")
@TargetClass(className = "java.lang.Module", onlyWith = JDK11OrLater.class)
public final class Target_java_lang_Module {
@Alias //
public String name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private Class<?> findClassInModuleOrNull(Target_jdk_internal_loader_Loader_Loade

@Substitute
protected URL findResource(String mn, String name) {
return ResourcesHelper.nameToResourceURL(name);
return ResourcesHelper.nameToResourceURL(mn, name);
}

@Substitute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
import java.util.regex.Pattern;

import org.graalvm.collections.MapCursor;
import org.graalvm.collections.Pair;

import com.oracle.svm.core.jdk.Resources;

/**
* <p>
Expand Down Expand Up @@ -647,9 +650,9 @@ private void update(Entry e) {
}

private void readAllEntries() {
MapCursor<String, ResourceStorageEntry> entries = NativeImageResourceFileSystemUtil.iterator();
MapCursor<Pair<String, String>, ResourceStorageEntry> entries = Resources.singleton().resources().getEntries();
while (entries.advance()) {
byte[] name = getBytes(entries.getKey());
byte[] name = getBytes(entries.getKey().getRight());
if (!entries.getValue().isDirectory()) {
IndexNode newIndexNode = new IndexNode(name, false);
inodes.put(newIndexNode, newIndexNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.io.InputStream;
import java.util.Arrays;

import org.graalvm.collections.MapCursor;
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;

import com.oracle.svm.core.jdk.Resources;
Expand All @@ -38,12 +37,8 @@ public final class NativeImageResourceFileSystemUtil {
private NativeImageResourceFileSystemUtil() {
}

public static MapCursor<String, ResourceStorageEntry> iterator() {
return Resources.singleton().resources().getEntries();
}

public static byte[] getBytes(String resourceName, boolean readOnly) {
ResourceStorageEntry entry = Resources.singleton().resources().get(resourceName);
ResourceStorageEntry entry = Resources.get(resourceName);
if (entry == null) {
return new byte[0];
}
Expand All @@ -56,7 +51,7 @@ public static byte[] getBytes(String resourceName, boolean readOnly) {
}

public static int getSize(String resourceName) {
ResourceStorageEntry entry = Resources.singleton().resources().get(resourceName);
ResourceStorageEntry entry = Resources.get(resourceName);
if (entry == null) {
return 0;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public boolean consume(ArgumentQueue args) {
}
if (nativeImage.config.useJavaModules()) {
nativeImage.addImageBuilderJavaArgs(addModulesOption, addModulesArgs);
nativeImage.addAddedModules(addModulesArgs);
} else {
NativeImage.showWarning("Ignoring unsupported module option: " + addModulesOption + " " + addModulesArgs);
}
Expand Down Expand Up @@ -337,6 +338,7 @@ public boolean consume(ArgumentQueue args) {
}
if (nativeImage.config.useJavaModules()) {
nativeImage.addImageBuilderJavaArgs(addModulesOption, addModulesArgs);
nativeImage.addAddedModules(addModulesArgs);
} else {
NativeImage.showWarning("Ignoring unsupported module option: " + addModulesOption + " " + addModulesArgs);
}
Expand Down
Loading