Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't find micronautserviceconfigs when running as jlink-ed application #10982

Merged
merged 7 commits into from
Aug 6, 2024
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
4 changes: 4 additions & 0 deletions core/src/main/java/io/micronaut/core/io/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -165,6 +166,9 @@ static Path resolvePath(@NonNull URI uri,
return loadNestedJarUriFunction.apply(toClose, jarUri).resolve(path);
} else if ("file".equals(scheme)) {
return Paths.get(uri).resolve(path);
} else if ("jrt".equals(scheme)) {
FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"), Map.of());
return fs.getPath(uri.getPath());
} else {
// graal resource: case
return Paths.get(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class DefaultClassPathResourceLoader implements ClassPathResourceLoader {
private final String basePath;
private final URL baseURL;
private final Map<String, Boolean> isDirectoryCache = new ConcurrentLinkedHashMap.Builder<String, Boolean>()
.maximumWeightedCapacity(50).build();
.maximumWeightedCapacity(50).build();
private final boolean missingPath;
private final boolean checkBase;

Expand Down Expand Up @@ -100,7 +100,7 @@ public DefaultClassPathResourceLoader(ClassLoader classLoader, String basePath,
* @param classLoader The class loader for loading resources
* @param basePath The path to look for resources under
* @param checkBase If set to {@code true} an extended check for the base path is performed otherwise paths with relative URLs like {@code ../} are prohibited.
* @param logEnabled flag to enable or disable logger
* @param logEnabled flag to enable or disable logger
*/
public DefaultClassPathResourceLoader(ClassLoader classLoader, String basePath, boolean checkBase, boolean logEnabled) {

Expand Down Expand Up @@ -290,9 +290,8 @@ public ResourceLoader forBase(String basePath) {
/**
* Need this method to ability disable Slf4J initizalization.
*
* @param basePath The path to load resources
* @param basePath The path to load resources
* @param logEnabled flag to enable or disable logger
*
* @return The resource loader
*/
public ResourceLoader forBase(String basePath, boolean logEnabled) {
Expand Down Expand Up @@ -348,6 +347,19 @@ private boolean isDirectory(String path) {
}
}
}
} else if ("jrt".equals(uri.getScheme())) {
FileSystem fileSystem = null;
try {
fileSystem = FileSystems.getFileSystem(URI.create("jrt:/"));
} catch (FileSystemNotFoundException e) {
//no-op
}
if (fileSystem == null || !fileSystem.isOpen()) {
fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap(), classLoader);
}

pathObject = fileSystem.getPath(path);
return pathObject == null || Files.isDirectory(pathObject);
} else if (uri.getScheme().equals("file")) {
pathObject = Paths.get(uri);
return pathObject == null || Files.isDirectory(pathObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ protected void registerRuntimeReflection(Field... fields) {
}

/**
* Initialize a class at build time
* Initialize a class at build time.
* @param buildInitClass The class
*/
protected void initializeAtBuildTime(@Nullable Class<?> buildInitClass) {
Expand Down
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 All @@ -27,6 +30,8 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -187,6 +192,37 @@ private void findMicronautMetaServiceConfigs(BiConsumer<URI, String> consumer) t
uniqueURIs.add(uri);
}

if (uniqueURIs.isEmpty()) {
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) {
String scheme = uri.getScheme();
if ("file".equals(scheme)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.micronaut.core.io.service;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.lang.module.Configuration;
import java.lang.module.ModuleFinder;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Function;

import org.junit.jupiter.api.Test;

import io.micronaut.core.io.service.SoftServiceLoader.ServiceCollector;

public class SoftServiceLoaderTest {

@Test
void findServicesUsingJrtScheme() {
String modulename = "io.micronaut.core.test";
ModuleFinder finder = ModuleFinder.of(Path.of("build/resources/test/test.jar"));
ModuleLayer parent = ModuleLayer.boot();
Configuration cf = parent.configuration().resolve(finder, ModuleFinder.of(), Set.of(modulename));
ClassLoader scl = ClassLoader.getSystemClassLoader();
ModuleLayer layer = parent.defineModulesWithOneLoader(cf, scl);

ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(layer.findLoader(modulename));

ServiceCollector<String> collector = SoftServiceLoader.newCollector("io.micronaut.inject.BeanDefinitionReference", null, layer.findLoader(modulename), Function.identity());
List<String> services = new ArrayList<>();
collector.collect(services::add);

assertEquals(1, services.size());
assertEquals("io.micronaut.logging.$PropertiesLoggingLevelsConfigurer$Definition", services.get(0));
}
finally {
Thread.currentThread().setContextClassLoader(oldLoader);
}
}
}
Binary file added core/src/test/resources/test.jar
Binary file not shown.
Loading