Skip to content

Commit

Permalink
fix native image support
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Aug 6, 2024
1 parent 09d4e70 commit 5d7c533
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions core/src/main/java/io/micronaut/core/io/service/ServiceScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -190,17 +193,34 @@ private void findMicronautMetaServiceConfigs(BiConsumer<URI, String> 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/<module>/META-INF/micronaut/<service>, 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<Path> 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/<module>/META-INF/micronaut/<service>, so we can walk through its files as if it was a directory
}
}

for (URI uri : uniqueURIs) {
Expand Down

0 comments on commit 5d7c533

Please sign in to comment.