From c3bea55caf09eb4cbdabda9693e9517ef5925d2f Mon Sep 17 00:00:00 2001 From: Ling Hengqian Date: Sat, 11 Nov 2023 18:45:03 +0800 Subject: [PATCH] Open the `resource:/` related FileSystem for JDBCRepositorySQLLoader in the GraalVM Native Image environment (#29009) --- .../jdbc/sql/JDBCRepositorySQLLoader.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoader.java b/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoader.java index 172b8cd8ba5e9..837d5974bdbb8 100644 --- a/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoader.java +++ b/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoader.java @@ -26,8 +26,11 @@ import java.io.IOException; import java.io.InputStream; import java.net.JarURLConnection; +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.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; @@ -36,6 +39,7 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Enumeration; import java.util.HashSet; import java.util.Objects; @@ -77,7 +81,36 @@ public static JDBCRepositorySQL load(final String type) { return result; } + /** + * Under the GraalVM Native Image corresponding to GraalVM CE 21.0.1, although there is + * `com.oracle.svm.core.jdk.resources.NativeImageResourceFileSystemProvider`, the corresponding + * `com.oracle.svm.core.jdk.resources.NativeImageResourceFileSystem` does not autoload. This is mainly to align the + * behavior of `ZipFileSystemProvider`, so ShardingSphere need to manually open and close the FileSystem + * corresponding to the `resource:/` scheme. For more background reference oracle/graal#7682. + *

+ * ShardingSphere use the System Property of `org.graalvm.nativeimage.imagecode` to identify whether this class is in the + * GraalVM Native Image environment. The background of this property comes from + * Annotation Interface DisabledInNativeImage. + * + * @param url url + * @param type type of JDBC repository SQL + * @return loaded JDBC repository SQL + * @throws URISyntaxException Checked exception thrown to indicate that a string could not be parsed as a URI reference + * @throws IOException Signals that an I/O exception to some sort has occurred + * @see jdk.nio.zipfs.ZipFileSystemProvider + * @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")) { + try (FileSystem ignored = FileSystems.newFileSystem(URI.create("resource:/"), Collections.singletonMap("create", "true"))) { + return loadFromDirectoryLegacy(url, type); + } + } else { + return loadFromDirectoryLegacy(url, type); + } + } + + private static JDBCRepositorySQL loadFromDirectoryLegacy(final URL url, final String type) throws URISyntaxException, IOException { final JDBCRepositorySQL[] result = new JDBCRepositorySQL[1]; Files.walkFileTree(Paths.get(url.toURI()), new SimpleFileVisitor() {