Skip to content

Commit

Permalink
Merge pull request #27690 from zakkak/drop-internal-resources-dependency
Browse files Browse the repository at this point in the history
Stop depending on com.oracle.svm.core.jdk.Resources#registerResource
  • Loading branch information
geoand authored Sep 5, 2022
2 parents e998d5a + 76ca22d commit b1e79ce
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions core/runtime/src/main/java/io/quarkus/runtime/ResourceHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.InputStream;
import java.lang.reflect.Method;

import org.graalvm.home.Version;

import io.quarkus.runtime.util.ClassPathUtils;

/**
Expand All @@ -11,18 +13,33 @@
public class ResourceHelper {

public static void registerResources(String resource) {
try {
Class<?> resourcesClass = Class.forName("com.oracle.svm.core.jdk.Resources");
Method register = resourcesClass.getDeclaredMethod("registerResource", String.class, InputStream.class);
ClassPathUtils.consumeAsStreams(ResourceHelper.class.getClassLoader(), resource, in -> {
try {
register.invoke(null, resource, in);
} catch (Exception e) {
throw new RuntimeException("Failed to register resource " + resource, e);
}
});
} catch (Exception e) {
throw new RuntimeException("Failed to load resource " + resource, e);
Version currentGraalVmVersion = Version.getCurrent();
if (currentGraalVmVersion.compareTo(22, 3) >= 0) {
// Use the public API RuntimeResourceAccess with GraalVM >= 22.3
// TODO: Remove reflective access once support for GraalVM < 22.3 gets dropped and directly invoke
// RuntimeResourceAccess.addResource(ClassLoader.getSystemClassLoader().getUnnamedModule(), resource);
try {
Class<?> runtimeResourceSupportClass = Class.forName("org.graalvm.nativeimage.hosted.RuntimeResourceAccess");
Method addResource = runtimeResourceSupportClass.getDeclaredMethod("addResource", Module.class, String.class);
addResource.invoke(null, ClassLoader.getSystemClassLoader().getUnnamedModule(), resource);
} catch (Exception e) {
throw new RuntimeException("Failed to load resource " + resource, e);
}
} else {
// Use internal API with GraalVM < 22.3
try {
Class<?> resourcesClass = Class.forName("com.oracle.svm.core.jdk.Resources");
Method register = resourcesClass.getDeclaredMethod("registerResource", String.class, InputStream.class);
ClassPathUtils.consumeAsStreams(ResourceHelper.class.getClassLoader(), resource, in -> {
try {
register.invoke(null, resource, in);
} catch (Exception e) {
throw new RuntimeException("Failed to register resource " + resource, e);
}
});
} catch (Exception e) {
throw new RuntimeException("Failed to load resource " + resource, e);
}
}
}

Expand Down

0 comments on commit b1e79ce

Please sign in to comment.