Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / java / java / com / ceph / fs / CephNativeLoader.java
1 /*
2  * Permission is hereby granted, free of charge, to any person obtaining a
3  * copy of this software and associated documentation files (the "Software"),
4  * to deal in the Software without restriction, including without limitation
5  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
6  * and/or sell copies of the Software, and to permit persons to whom the
7  * Software is furnished to do so, subject to the following conditions:
8  *
9  * The above copyright notice and this permission notice shall be included in
10  * all copies or substantial portions of the Software.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18  * DEALINGS IN THE SOFTWARE.
19  */
20 package com.ceph.fs;
21
22 class CephNativeLoader {
23   private static final CephNativeLoader instance = new CephNativeLoader();
24   private static boolean initialized = false;
25
26   private static final String JNI_PATH_ENV_VAR = "CEPH_JNI_PATH";
27   private static final String LIBRARY_NAME = "cephfs_jni";
28   private static final String LIBRARY_FILE = "libcephfs_jni.so";
29
30   private CephNativeLoader() {}
31
32   public static CephNativeLoader getInstance() {
33     return instance;
34   }
35
36   public synchronized void loadLibrary() {
37     if (initialized)
38       return;
39
40     boolean success = false;
41
42     /*
43      * Allow a Ceph specific environment variable to force
44      * the loading path.
45      */
46     String path = System.getenv(JNI_PATH_ENV_VAR);
47     try {
48       if (path != null) {
49         System.out.println("Loading libcephfs-jni: " + path);
50         System.load(path);
51         success = true;
52       } else {
53         try {
54           /*
55            * Try default Java loading path(s)
56            */
57           System.out.println("Loading libcephfs-jni from default path: " +
58               System.getProperty("java.library.path"));
59           System.loadLibrary(LIBRARY_NAME);
60           success = true;
61         } catch (final UnsatisfiedLinkError ule1) {
62           try {
63             /*
64              * Try RHEL/CentOS default path
65              */
66             path = "/usr/lib64/" + LIBRARY_FILE;
67             System.out.println("Loading libcephfs-jni: " + path);
68             System.load(path);
69             success = true;
70           } catch (final UnsatisfiedLinkError ule2) {
71             /*
72              * Try Ubuntu default path
73              */
74             path = "/usr/lib/jni/" + LIBRARY_FILE;
75             System.out.println("Loading libcephfs-jni: " + path);
76             System.load(path);
77             success = true;
78           }
79         }
80       }
81     } finally {
82       System.out.println("Loading libcephfs-jni: " +
83           (success ? "Success!" : "Failure!"));
84     }
85
86     /*
87      * Finish initialization
88      */
89     CephMount.native_initialize();
90     initialized = true;
91   }
92
93 }