Fix bugs to load a native binary in SnappyLoader

This commit is contained in:
Takeshi YAMAMURO 2016-04-02 15:45:55 +09:00
parent f94765b939
commit eeb5a811b4

View File

@ -79,7 +79,8 @@ public class SnappyLoader
public static final String KEY_SNAPPY_USE_SYSTEMLIB = "org.xerial.snappy.use.systemlib"; public static final String KEY_SNAPPY_USE_SYSTEMLIB = "org.xerial.snappy.use.systemlib";
public static final String KEY_SNAPPY_DISABLE_BUNDLED_LIBS = "org.xerial.snappy.disable.bundled.libs"; // Depreciated, but preserved for backward compatibility public static final String KEY_SNAPPY_DISABLE_BUNDLED_LIBS = "org.xerial.snappy.disable.bundled.libs"; // Depreciated, but preserved for backward compatibility
private static volatile boolean isLoaded = false; private static boolean isLoaded = false;
private static volatile SnappyNative snappyApi = null; private static volatile SnappyNative snappyApi = null;
private static volatile BitShuffleNative bitshuffleApi = null; private static volatile BitShuffleNative bitshuffleApi = null;
@ -92,6 +93,8 @@ public class SnappyLoader
if (!deleted) { if (!deleted) {
// Deleting native lib has failed, but it's not serious so simply ignore it here // Deleting native lib has failed, but it's not serious so simply ignore it here
} }
snappyApi = null;
bitshuffleApi = null;
} }
} }
@ -105,16 +108,6 @@ public class SnappyLoader
snappyApi = nativeCode; snappyApi = nativeCode;
} }
/**
* Set the `bitshuffleApi` instance.
*
* @param nativeCode
*/
static synchronized void setBitShuffleApi(BitShuffleNative nativeCode)
{
bitshuffleApi = nativeCode;
}
/** /**
* load system properties when configuration file of the name * load system properties when configuration file of the name
* {@link #SNAPPY_SYSTEM_PROPERTIES_FILE} is found * {@link #SNAPPY_SYSTEM_PROPERTIES_FILE} is found
@ -158,18 +151,8 @@ public class SnappyLoader
if (snappyApi != null) { if (snappyApi != null) {
return snappyApi; return snappyApi;
} }
loadNativeLibrary();
try { setSnappyApi(new SnappyNative());
loadNativeLibrary();
setSnappyApi(new SnappyNative());
isLoaded = true;
}
catch (Exception e) {
e.printStackTrace();
throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, e.getMessage());
}
return snappyApi; return snappyApi;
} }
@ -178,35 +161,32 @@ public class SnappyLoader
if (bitshuffleApi != null) { if (bitshuffleApi != null) {
return bitshuffleApi; return bitshuffleApi;
} }
loadNativeLibrary();
try { bitshuffleApi = new BitShuffleNative();
loadNativeLibrary();
setBitShuffleApi(new BitShuffleNative());
isLoaded = true;
}
catch (Exception e) {
e.printStackTrace();
throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, e.getMessage());
}
return bitshuffleApi; return bitshuffleApi;
} }
/** /**
* Load a native library of snappy-java * Load a native library of snappy-java
*/ */
private static void loadNativeLibrary() private synchronized static void loadNativeLibrary()
{ {
if (!isLoaded) {
nativeLibFile = findNativeLibrary(); try {
if (nativeLibFile != null) { nativeLibFile = findNativeLibrary();
// Load extracted or specified snappyjava native library. if (nativeLibFile != null) {
System.load(nativeLibFile.getAbsolutePath()); // Load extracted or specified snappyjava native library.
} System.load(nativeLibFile.getAbsolutePath());
else { } else {
// Load preinstalled snappyjava (in the path -Djava.library.path) // Load preinstalled snappyjava (in the path -Djava.library.path)
System.loadLibrary("snappyjava"); System.loadLibrary("snappyjava");
}
}
catch (Exception e) {
e.printStackTrace();
throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, e.getMessage());
}
isLoaded = true;
} }
} }