Skip to content

Commit

Permalink
Remove console-related service providers from ServiceCatalog
Browse files Browse the repository at this point in the history
  • Loading branch information
zakkak committed Nov 26, 2024
1 parent 9361824 commit e1823b2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,11 @@ public NativeImageInvokerInfo build() {
for (NativeImageFeatureBuildItem nativeImageFeature : nativeImageFeatures) {
featuresList.add(nativeImageFeature.getQualifiedName());
}
if (!nativeConfig.autoServiceLoaderRegistration()) {
featuresList.add("io.quarkus.runtime.graal.SkipConsoleServiceProvidersFeature");
// required by the feature
nativeImageArgs.add("-J--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk=ALL-UNNAMED");
}
nativeImageArgs.add("--features=" + String.join(",", featuresList));

if (nativeConfig.debug().enabled()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.quarkus.runtime.graal;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import org.graalvm.nativeimage.hosted.Feature;

/**
* Removes {@code jdk.internal.io.JdkConsoleProvider} service providers from the {@code ServiceCatalog} in a similar way to
* GraalVM's {@code ServiceLoaderFeature} which Quarkus disables by default.
*/
public class SkipConsoleServiceProvidersFeature implements Feature {
static final HashMap<String, Set<String>> omittedServiceProviders;

@Override
public String getDescription() {
return "Skip unsupported console service providers when quarkus.native.auto-service-loader-registration is false";
}

static {
omittedServiceProviders = new HashMap<>(1);
omittedServiceProviders.put("jdk.internal.io.JdkConsoleProvider",
new HashSet<>(Arrays.asList("jdk.jshell.execution.impl.ConsoleImpl$ConsoleProviderImpl",
"jdk.internal.org.jline.JdkConsoleProviderImpl")));
}

@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
Class<?> serviceCatalogSupport;
Method singleton;
Method removeServices;
try {
serviceCatalogSupport = Class.forName("com.oracle.svm.core.jdk.ServiceCatalogSupport");
singleton = serviceCatalogSupport.getDeclaredMethod("singleton");
removeServices = serviceCatalogSupport.getDeclaredMethod("removeServicesFromServicesCatalog", String.class,
Set.class);
var result = singleton.invoke(null);
omittedServiceProviders.forEach((key, value) -> {
try {
removeServices.invoke(result, key, value);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
});
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

}

0 comments on commit e1823b2

Please sign in to comment.