Skip to content

Commit

Permalink
Open the resource:/ related FileSystem for JDBCRepositorySQLLoader …
Browse files Browse the repository at this point in the history
…in the GraalVM Native Image environment
  • Loading branch information
linghengqian committed Nov 11, 2023
1 parent 213166a commit 49e87ce
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 <a href="https://github.com/oracle/graal/issues/7682">oracle/graal#7682</a>.
* <p/>
* 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
* <a href="https://junit.org/junit5/docs/5.10.0/api/org.junit.jupiter.api/org/junit/jupiter/api/condition/DisabledInNativeImage.html">Annotation Interface DisabledInNativeImage</a>.
*
* @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<Path>() {

Expand Down

0 comments on commit 49e87ce

Please sign in to comment.