From e8d069794b743bb45c0ef0d9582674921ed3d88f Mon Sep 17 00:00:00 2001 From: linghengqian Date: Sun, 12 Nov 2023 20:13:46 +0800 Subject: [PATCH] Make JDBCRepositorySQLLoader avoid using `Path#toFile` in GraalVM Native Image --- .../jdbc/sql/JDBCRepositorySQLLoader.java | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) 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 efabbb5301d92..2d7ea1cb9a7d4 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 @@ -105,11 +105,46 @@ private static JDBCRepositorySQL loadFromDirectory(final URL url, final String t return loadFromDirectoryLegacy(url, type); } else { try (FileSystem ignored = FileSystems.newFileSystem(URI.create("resource:/"), Collections.emptyMap())) { - return loadFromDirectoryLegacy(url, type); + return loadFromDirectoryInNativeImage(url, type); } } } + /** + * Affected by oracle/graal#7804, ShardingSphere needs to + * avoid the use of `java.nio.file.Path#toFile` in GraalVM Native Image. + * + * @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 java.nio.file.Path + * @see java.io.File + */ + private static JDBCRepositorySQL loadFromDirectoryInNativeImage(final URL url, final String type) throws URISyntaxException, IOException { + final JDBCRepositorySQL[] result = new JDBCRepositorySQL[1]; + Files.walkFileTree(Paths.get(url.toURI()), new SimpleFileVisitor() { + + @SneakyThrows(JAXBException.class) + @Override + public FileVisitResult visitFile(final Path file, final BasicFileAttributes attributes) throws IOException { + if (file.toString().endsWith(FILE_EXTENSION)) { + JDBCRepositorySQL provider = (JDBCRepositorySQL) JAXBContext.newInstance(JDBCRepositorySQL.class).createUnmarshaller().unmarshal(Files.newInputStream(file.toAbsolutePath())); + if (provider.isDefault()) { + result[0] = provider; + } + if (Objects.equals(provider.getType(), type)) { + result[0] = provider; + return FileVisitResult.TERMINATE; + } + } + return FileVisitResult.CONTINUE; + } + }); + return result[0]; + } + 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() {