Skip to content

Commit

Permalink
Fixes ClassPathURLLoader cannot find files under GraalVM Native Image
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian committed Feb 16, 2024
1 parent 0f4f997 commit 57bb9c0
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@
import lombok.SneakyThrows;
import org.apache.shardingsphere.driver.jdbc.core.driver.url.ShardingSphereURLLoader;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
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;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
Expand All @@ -36,12 +41,18 @@ public final class ClassPathURLLoader implements ShardingSphereURLLoader {
@Override
@SneakyThrows(IOException.class)
public String load(final String configurationSubject, final Map<String, String> parameters) {
return Files.readAllLines(getResourceFile(configurationSubject).toPath()).stream().collect(Collectors.joining(System.lineSeparator()));
if (null == System.getProperty("org.graalvm.nativeimage.imagecode") || !"runtime".equals(System.getProperty("org.graalvm.nativeimage.imagecode"))) {
return Files.readAllLines(getResourcePath(configurationSubject)).stream().collect(Collectors.joining(System.lineSeparator()));
} else {
try (FileSystem ignored = FileSystems.newFileSystem(URI.create("resource:/"), Collections.emptyMap())) {
return Files.readAllLines(getResourcePath(configurationSubject)).stream().collect(Collectors.joining(System.lineSeparator()));
}
}
}

@SneakyThrows(URISyntaxException.class)
private File getResourceFile(final String configurationSubject) {
return new File(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource(configurationSubject)).toURI().getPath());
private Path getResourcePath(final String configurationSubject) {
return Paths.get(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource(configurationSubject)).toURI());
}

@Override
Expand Down

0 comments on commit 57bb9c0

Please sign in to comment.