]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
java: search for JNI bits in common dirs
authorNoah Watkins <noahwatkins@gmail.com>
Sun, 9 Aug 2015 19:33:22 +0000 (13:33 -0600)
committerNoah Watkins <noahwatkins@gmail.com>
Sun, 16 Aug 2015 22:15:34 +0000 (16:15 -0600)
Signed-off-by: Noah Watkins <noahwatkins@gmail.com>
src/java/java/com/ceph/fs/CephMount.java
src/java/java/com/ceph/fs/CephNativeLoader.java

index 7c9c02c76e7b4b2337c08916a8e8d04d208736ff..a2867958710cccd48d5e74b1d16587298f98edc1 100644 (file)
@@ -94,7 +94,11 @@ public class CephMount {
    * finding or linking in the shared JNI library.
    */
   static {
-    CephNativeLoader.checkLoaded();
+    loadLibrary();
+  }
+
+  static synchronized void loadLibrary() {
+    CephNativeLoader.getInstance().loadLibrary();
   }
 
   /*
index 21a54c339fce947b01d2e5218a3914cf670cbaf2..358ca243e5c66fbf2a1d1ea10c20452b823ee213 100644 (file)
 package com.ceph.fs;
 
 class CephNativeLoader {
+  private static final CephNativeLoader instance = new CephNativeLoader();
+  private static boolean initialized = false;
 
-  private static boolean loaded = false;
+  private static final String JNI_PATH_ENV_VAR = "CEPH_JNI_PATH";
+  private static final String LIBRARY_NAME = "cephfs_jni";
+  private static final String LIBRARY_FILE = "libcephfs_jni.so";
 
-  static {
-    if (!loaded) {
-      System.loadLibrary("cephfs_jni");
-      CephMount.native_initialize();
-      loaded = true;
+  private CephNativeLoader() {}
+
+  public static CephNativeLoader getInstance() {
+    return instance;
+  }
+
+  public synchronized void loadLibrary() {
+    if (initialized)
+      return;
+
+    boolean success = false;
+
+    /*
+     * Allow a Ceph specific environment variable to force
+     * the loading path.
+     */
+    String path = System.getenv(JNI_PATH_ENV_VAR);
+    try {
+      if (path != null) {
+        System.out.println("Loading libcephfs-jni: " + path);
+        System.load(path);
+        success = true;
+      } else {
+        try {
+          /*
+           * Try default Java loading path(s)
+           */
+          System.out.println("Loading libcephfs-jni from default path: " +
+              System.getProperty("java.library.path"));
+          System.loadLibrary(LIBRARY_NAME);
+          success = true;
+        } catch (final UnsatisfiedLinkError ule1) {
+          try {
+            /*
+             * Try RHEL/CentOS default path
+             */
+            path = "/usr/lib64/" + LIBRARY_FILE;
+            System.out.println("Loading libcephfs-jni: " + path);
+            System.load(path);
+            success = true;
+          } catch (final UnsatisfiedLinkError ule2) {
+            /*
+             * Try Ubuntu default path
+             */
+            path = "/usr/lib/jni/" + LIBRARY_FILE;
+            System.out.println("Loading libcephfs-jni: " + path);
+            System.load(path);
+            success = true;
+          }
+        }
+      }
+    } finally {
+      System.out.println("Loading libcephfs-jni: " +
+          (success ? "Success!" : "Failure!"));
     }
+
+    /*
+     * Finish initialization
+     */
+    CephMount.native_initialize();
+    initialized = true;
   }
 
-  static void checkLoaded() { assert(loaded); }
 }