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 17, 2024
1 parent fa1ef90 commit cba4dad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.url.ShardingSphereURLLoader;

import java.io.File;
import java.io.IOException;
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.Paths;
import java.util.Collections;
import java.util.Objects;
import java.util.Properties;
import java.util.stream.Collectors;
Expand All @@ -34,14 +39,19 @@
public final class ClassPathURLLoader implements ShardingSphereURLLoader {

@Override
@SneakyThrows(IOException.class)
@SneakyThrows({IOException.class, URISyntaxException.class})
public String load(final String configurationSubject, final Properties queryProps) {
return Files.readAllLines(getResourceFile(configurationSubject).toPath()).stream().collect(Collectors.joining(System.lineSeparator()));
URL resourceURL = getResourceURL(configurationSubject);
if ("resource".equals(resourceURL.getProtocol())) {
try (FileSystem ignored = FileSystems.newFileSystem(URI.create("resource:/"), Collections.emptyMap())) {
return Files.readAllLines(Paths.get(resourceURL.toURI())).stream().collect(Collectors.joining(System.lineSeparator()));
}
}
return Files.readAllLines(Paths.get(resourceURL.toURI())).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 URL getResourceURL(final String configurationSubject) {
return Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource(configurationSubject));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,12 @@ public static JDBCRepositorySQL load(final String type) {
* @see sun.nio.fs.UnixFileSystemProvider
*/
private static JDBCRepositorySQL loadFromDirectory(final URL url, final String type) throws URISyntaxException, IOException {
if (null == System.getProperty("org.graalvm.nativeimage.imagecode") || !"runtime".equals(System.getProperty("org.graalvm.nativeimage.imagecode"))) {
return loadFromDirectoryLegacy(url, type);
} else {
if ("resource".equals(url.getProtocol())) {
try (FileSystem ignored = FileSystems.newFileSystem(URI.create("resource:/"), Collections.emptyMap())) {
return loadFromDirectoryInNativeImage(url, type);
}
}
return loadFromDirectoryLegacy(url, type);
}

/**
Expand Down

0 comments on commit cba4dad

Please sign in to comment.