tisonkun commented on code in PR #7087:
URL: https://github.com/apache/opendal/pull/7087#discussion_r2639839013
##########
bindings/java/src/main/java/org/apache/opendal/NativeLibrary.java:
##########
@@ -103,22 +108,89 @@ private static void doLoadLibrary() throws IOException {
}
private static void doLoadBundledLibrary() throws IOException {
- final String libraryPath = bundledLibraryPath();
- try (final InputStream is =
NativeObject.class.getResourceAsStream(libraryPath)) {
- if (is == null) {
- throw new IOException("cannot find " + libraryPath);
+ final String libraryName = System.mapLibraryName("opendal_java");
+ final String[] libraryPaths = bundledLibraryPaths(libraryName);
+
+ final UnsatisfiedLinkError[] linkErrors = new
UnsatisfiedLinkError[libraryPaths.length];
+ for (int i = 0; i < libraryPaths.length; i++) {
+ final String libraryPath = libraryPaths[i];
+ try (final InputStream is =
NativeObject.class.getResourceAsStream(libraryPath)) {
+ if (is == null) {
+ continue;
+ }
+
+ final File tmpFile = createTempLibraryFile(libraryName);
+ tmpFile.deleteOnExit();
+ Files.copy(is, tmpFile.toPath(),
StandardCopyOption.REPLACE_EXISTING);
+ try {
+ System.load(tmpFile.getAbsolutePath());
+ return;
+ } catch (UnsatisfiedLinkError e) {
+ linkErrors[i] = e;
+ }
+ }
+ }
+
+ final StringBuilder attempted = new StringBuilder();
+ for (int i = 0; i < libraryPaths.length; i++) {
+ if (i > 0) {
+ attempted.append(", ");
+ }
+ attempted.append(libraryPaths[i]);
+ }
+
+ final UnsatisfiedLinkError last = lastNonNull(linkErrors);
+ if (last != null) {
+ final UnsatisfiedLinkError e = new UnsatisfiedLinkError(
+ "Unable to load the OpenDAL shared library from classpath.
Tried: " + attempted);
+ for (UnsatisfiedLinkError err : linkErrors) {
+ if (err != null) {
+ e.addSuppressed(err);
+ }
}
- final int dot = libraryPath.indexOf('.');
- final File tmpFile = File.createTempFile(libraryPath.substring(0,
dot), libraryPath.substring(dot));
- tmpFile.deleteOnExit();
- Files.copy(is, tmpFile.toPath(),
StandardCopyOption.REPLACE_EXISTING);
- System.load(tmpFile.getAbsolutePath());
+ throw e;
}
+ throw new IOException("cannot find bundled OpenDAL shared library in
classpath. Tried: " + attempted);
}
- private static String bundledLibraryPath() {
+ private static String[] bundledLibraryPaths(String libraryName) {
final String classifier = Environment.getClassifier();
- final String libraryName = System.mapLibraryName("opendal_java");
- return "/native/" + classifier + "/" + libraryName;
+ if (classifier.startsWith("linux-") && classifier.endsWith("-musl")) {
+ final String gnu = classifier.substring(0, classifier.length() -
"-musl".length());
+ return new String[] {
+ "/native/" + classifier + "/" + libraryName,
+ "/native/" + gnu + "/" + libraryName,
+ };
+ }
+ if (classifier.startsWith("linux-")) {
+ return new String[] {
+ "/native/" + classifier + "/" + libraryName,
+ "/native/" + classifier + "-musl/" + libraryName,
+ };
+ }
+ return new String[] {"/native/" + classifier + "/" + libraryName};
+ }
+
+ private static File createTempLibraryFile(String libraryName) throws
IOException {
+ final int dot = libraryName.lastIndexOf('.');
+ final String prefix;
+ final String suffix;
+ if (dot >= 0) {
+ prefix = libraryName.substring(0, dot);
+ suffix = libraryName.substring(dot);
+ } else {
+ prefix = libraryName;
+ suffix = null;
+ }
+ return File.createTempFile(prefix + "-", suffix);
+ }
+
+ private static UnsatisfiedLinkError lastNonNull(UnsatisfiedLinkError[]
errors) {
+ for (int i = errors.length - 1; i >= 0; i--) {
+ if (errors[i] != null) {
+ return errors[i];
+ }
+ }
+ return null;
}
}
Review Comment:
Weird refactoring. Why we change these?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]