Skip to content

Commit

Permalink
Make JDBCRepositorySQLLoader avoid using Path#toFile in GraalVM Nat…
Browse files Browse the repository at this point in the history
…ive Image
  • Loading branch information
linghengqian committed Nov 12, 2023
1 parent 0f0ccfc commit e8d0697
Showing 1 changed file with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://github.com/oracle/graal/issues/7804">oracle/graal#7804</a>, 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<Path>() {

@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<Path>() {
Expand Down

0 comments on commit e8d0697

Please sign in to comment.