summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2022-04-25 16:52:39 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2022-04-25 17:15:11 +0200
commitcad36df5132815879cc078c3faed8ec51f13a23d (patch)
tree345c16d73ab4c1773b629dcae9a6be189b8b6205
parent1098c95f952833cc725e9ede4bd78af90f6a625e (diff)
Separate C library interface in separate package private class.
-rw-r--r--vespajlib/src/main/java/com/yahoo/io/NativeC.java25
-rw-r--r--vespajlib/src/main/java/com/yahoo/io/NativeIO.java15
2 files changed, 28 insertions, 12 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/io/NativeC.java b/vespajlib/src/main/java/com/yahoo/io/NativeC.java
new file mode 100644
index 00000000000..7dbf4015bcf
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/io/NativeC.java
@@ -0,0 +1,25 @@
+package com.yahoo.io;
+
+import com.sun.jna.LastErrorException;
+import com.sun.jna.Native;
+import com.sun.jna.Platform;
+
+class NativeC {
+ private final static Throwable initException = loadLibrary();
+ private static Throwable loadLibrary() {
+ if (Platform.isLinux()) {
+ try {
+ Native.register(Platform.C_LIBRARY_NAME);
+ } catch (Throwable e) {
+ return e;
+ }
+ } else {
+ return new RuntimeException("Platform is unsúpported. Only supported on linux.");
+ }
+ return null;
+ }
+ static Throwable init() {
+ return initException;
+ }
+ static native int posix_fadvise(int fd, long offset, long len, int flag) throws LastErrorException;
+}
diff --git a/vespajlib/src/main/java/com/yahoo/io/NativeIO.java b/vespajlib/src/main/java/com/yahoo/io/NativeIO.java
index 28c3f21b24c..8d6fe65b8e2 100644
--- a/vespajlib/src/main/java/com/yahoo/io/NativeIO.java
+++ b/vespajlib/src/main/java/com/yahoo/io/NativeIO.java
@@ -10,10 +10,6 @@ import java.io.SyncFailedException;
import java.lang.reflect.Field;
import java.util.logging.Logger;
-import com.sun.jna.LastErrorException;
-import com.sun.jna.Native;
-import com.sun.jna.Platform;
-
/**
* Provides functionality only possible through native C library.
*/
@@ -31,17 +27,14 @@ public class NativeIO {
boolean initComplete = false;
boolean disabled = true;
Field field = null;
- Throwable exception = null;
+ Throwable exception = NativeC.init();
try {
- if (Platform.isLinux()) {
+ if (exception == null) {
disabled = System.getenv().containsKey(DISABLE_NATIVE_IO);
if (!disabled) {
- Native.register(Platform.C_LIBRARY_NAME);
field = getField(FileDescriptor.class, "fd");
initComplete = true;
}
- } else {
- exception = new RuntimeException("Platform is unsúpported. Only supported on linux.");
}
} catch (Throwable throwable) {
exception = throwable;
@@ -68,8 +61,6 @@ public class NativeIO {
}
}
- private static native int posix_fadvise(int fd, long offset, long len, int flag) throws LastErrorException;
-
public NativeIO() {
if ( ! fdField.isInitialized()) {
if (fdField.isEnabled()) {
@@ -96,7 +87,7 @@ public class NativeIO {
}
}
if (valid()) {
- posix_fadvise(fdField.getNativeFD(fd), offset, len, POSIX_FADV_DONTNEED);
+ NativeC.posix_fadvise(fdField.getNativeFD(fd), offset, len, POSIX_FADV_DONTNEED);
}
}
/**