Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make JDBCRepositorySQLLoader avoid using Path#toFile in GraalVM Native Image #29014

Merged
merged 1 commit into from
Nov 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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