From 5d7c5332828af258912758ec60c2b7f8c961a44d Mon Sep 17 00:00:00 2001 From: Graeme Rocher Date: Tue, 6 Aug 2024 10:41:12 -0400 Subject: [PATCH] fix native image support --- .../core/io/service/ServiceScanner.java | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/io/micronaut/core/io/service/ServiceScanner.java b/core/src/main/java/io/micronaut/core/io/service/ServiceScanner.java index d7d9b1cf37b..93d5962800a 100644 --- a/core/src/main/java/io/micronaut/core/io/service/ServiceScanner.java +++ b/core/src/main/java/io/micronaut/core/io/service/ServiceScanner.java @@ -18,6 +18,9 @@ import io.micronaut.core.annotation.Internal; import io.micronaut.core.annotation.Nullable; import io.micronaut.core.io.IOUtils; +import java.nio.file.FileSystemNotFoundException; +import java.nio.file.ProviderNotFoundException; +import java.util.stream.Stream; import org.graalvm.nativeimage.ImageSingletons; import java.io.BufferedReader; @@ -190,17 +193,34 @@ private void findMicronautMetaServiceConfigs(BiConsumer consumer) t } if (uniqueURIs.isEmpty()) { - FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"), Map.of(), classLoader); - Path modulesPath = fs.getPath("modules"); - Files.list(modulesPath) - .filter(p -> !p.getFileName().toString().startsWith("jdk.")) // filter out JDK internal modules - .filter(p -> !p.getFileName().toString().startsWith("java.")) // filter out JDK public modules - .map(p -> p.resolve(path)) - .filter(Files::exists) - .map(modulesPath::resolve) - .map(Path::toUri) - .forEach(uniqueURIs::add); - // uri will be jrt:/modules//META-INF/micronaut/, so we can walk through its files as if it was a directory + FileSystem fs = null; + try { + fs = FileSystems.getFileSystem(URI.create("jrt:/")); + } catch (FileSystemNotFoundException | ProviderNotFoundException e) { + //no-op + } + if (fs == null || !fs.isOpen()) { + try { + fs = FileSystems.newFileSystem(URI.create("jrt:/"), Collections.emptyMap(), classLoader); + } catch (IOException | ProviderNotFoundException e) { + // not available, probably running in Native Image. + } + } + if (fs != null) { + Path modulesPath = fs.getPath("modules"); + try (Stream stream = Files.list(modulesPath)) { + stream + .filter(p -> !p.getFileName().toString().startsWith("jdk.")) // filter out JDK internal modules + .filter(p -> !p.getFileName().toString().startsWith("java.")) // filter out JDK public modules + .map(p -> p.resolve(path)) + .filter(Files::exists) + .map(modulesPath::resolve) + .map(Path::toUri) + .forEach(uniqueURIs::add); + } + + // uri will be jrt:/modules//META-INF/micronaut/, so we can walk through its files as if it was a directory + } } for (URI uri : uniqueURIs) {