Updates issue 20. Add org.xerial.snappy.use.systemlib property to lookup snappyjava dll under java.library.path

This commit is contained in:
Taro L. Saito 2011-08-16 13:04:57 +09:00
parent 006425b288
commit 3710c4e7dc
3 changed files with 43 additions and 33 deletions

View File

@ -56,9 +56,9 @@ $ make clean-native native
= Using system installed libsnappyjava (or snappyjava.dll) = Using system installed libsnappyjava (or snappyjava.dll)
Use org.xerial.snappy.enable.bundled.libs system property: Set org.xerial.snappy.use.systemlib system property to true:
java -Djava.library.path=(path to the installed snappyjava lib) -Dorg.xerial.snappy.enable.bundled.libs=false ... java -Djava.library.path=(path to the installed snappyjava lib) -Dorg.xerial.snappy.use.systemlib=true ...
With this setting snappy-java does not use bundled native libraries. Insted it tries to load native library installed at the path specified in java.library.path. With this setting snappy-java does not use bundled native libraries. Insted it tries to load native library installed at the path specified in java.library.path.

View File

@ -51,8 +51,8 @@ import java.util.Properties;
* In default, no configuration is required to use snappy-java, but you can load * In default, no configuration is required to use snappy-java, but you can load
* your own native library created by 'make native' command. * your own native library created by 'make native' command.
* *
* This SnappyLoader searches for native libraries (snappyjava.dll, libsnappy.so, etc.) * This SnappyLoader searches for native libraries (snappyjava.dll,
* in the following order: * libsnappy.so, etc.) in the following order:
* <ol> * <ol>
* <li>(System property: <i>org.xerial.snappy.lib.path</i>)/(System property: * <li>(System property: <i>org.xerial.snappy.lib.path</i>)/(System property:
* <i>org.xerial.lib.name</i>) * <i>org.xerial.lib.name</i>)
@ -82,13 +82,14 @@ import java.util.Properties;
*/ */
public class SnappyLoader public class SnappyLoader
{ {
public static final String KEY_SNAPPY_LIB_PATH = "org.xerial.snappy.lib.path"; public static final String KEY_SNAPPY_LIB_PATH = "org.xerial.snappy.lib.path";
public static final String KEY_SNAPPY_LIB_NAME = "org.xerial.snappy.lib.name"; public static final String KEY_SNAPPY_LIB_NAME = "org.xerial.snappy.lib.name";
public static final String KEY_SNAPPY_TEMPDIR = "org.xerial.snappy.tempdir"; public static final String KEY_SNAPPY_TEMPDIR = "org.xerial.snappy.tempdir";
public static final String KEY_SNAPPY_ENABLE_BUNDLED_LIBS = "org.xerial.snappy.enable.bundled.libs"; 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
private static boolean isLoaded = false; private static boolean isLoaded = false;
private static SnappyNativeAPI api = null; private static SnappyNativeAPI api = null;
private static ClassLoader getRootClassLoader() { private static ClassLoader getRootClassLoader() {
ClassLoader cl = Thread.currentThread().getContextClassLoader(); ClassLoader cl = Thread.currentThread().getContextClassLoader();
@ -132,8 +133,9 @@ public class SnappyLoader
/** /**
* Load SnappyNative and its JNI native implementation in the root class * Load SnappyNative and its JNI native implementation in the root class
* loader. This process is necessary to avoid the JNI multi-loading issue when the * loader. This process is necessary to avoid the JNI multi-loading issue
* same JNI library is loaded by different class loaders in the same JVM. * when the same JNI library is loaded by different class loaders in the
* same JVM.
* *
* In order to load native code in the root class loader, this method first * In order to load native code in the root class loader, this method first
* inject SnappyNativeLoader class into the root class loader, because * inject SnappyNativeLoader class into the root class loader, because
@ -361,33 +363,41 @@ public class SnappyLoader
} }
static File findNativeLibrary() { static File findNativeLibrary() {
// Try to load the library from org.xerial.snappy.lib.path library path */
boolean useSystemLib = Boolean.parseBoolean(System.getProperty(KEY_SNAPPY_USE_SYSTEMLIB, "false"));
if (useSystemLib)
return null;
boolean disabledBundledLibs = Boolean
.parseBoolean(System.getProperty(KEY_SNAPPY_DISABLE_BUNDLED_LIBS, "false"));
if (disabledBundledLibs)
return null;
// Try to load the library in org.xerial.snappy.lib.path */
String snappyNativeLibraryPath = System.getProperty(KEY_SNAPPY_LIB_PATH); String snappyNativeLibraryPath = System.getProperty(KEY_SNAPPY_LIB_PATH);
String snappyNativeLibraryName = System.getProperty(KEY_SNAPPY_LIB_NAME); String snappyNativeLibraryName = System.getProperty(KEY_SNAPPY_LIB_NAME);
if (System.getProperty(KEY_SNAPPY_ENABLE_BUNDLED_LIBS, "true").equals("true")) { // Resolve the library file name with a suffix (e.g., dll, .so, etc.)
// Resolve the library file name with a suffix (e.g., dll, .so, etc.) if (snappyNativeLibraryName == null)
if (snappyNativeLibraryName == null) snappyNativeLibraryName = System.mapLibraryName("snappyjava");
snappyNativeLibraryName = System.mapLibraryName("snappyjava");
if (snappyNativeLibraryPath != null) { if (snappyNativeLibraryPath != null) {
File nativeLib = new File(snappyNativeLibraryPath, snappyNativeLibraryName); File nativeLib = new File(snappyNativeLibraryPath, snappyNativeLibraryName);
if (nativeLib.exists()) if (nativeLib.exists())
return nativeLib; return nativeLib;
} }
{ {
// Load an OS-dependent native library inside a jar file // Load an OS-dependent native library inside a jar file
snappyNativeLibraryPath = "/org/xerial/snappy/native/" + OSInfo.getNativeLibFolderPathForCurrentOS(); snappyNativeLibraryPath = "/org/xerial/snappy/native/" + OSInfo.getNativeLibFolderPathForCurrentOS();
if (SnappyLoader.class.getResource(snappyNativeLibraryPath + "/" + snappyNativeLibraryName) != null) { if (SnappyLoader.class.getResource(snappyNativeLibraryPath + "/" + snappyNativeLibraryName) != null) {
// Temporary library folder. Use the value of org.xerial.snappy.tempdir or java.io.tmpdir // Temporary library folder. Use the value of org.xerial.snappy.tempdir or java.io.tmpdir
String tempFolder = new File(System.getProperty(KEY_SNAPPY_TEMPDIR, String tempFolder = new File(System.getProperty(KEY_SNAPPY_TEMPDIR,
System.getProperty("java.io.tmpdir"))).getAbsolutePath(); System.getProperty("java.io.tmpdir"))).getAbsolutePath();
// Extract and load a native library inside the jar file // Extract and load a native library inside the jar file
return extractLibraryFile(snappyNativeLibraryPath, snappyNativeLibraryName, tempFolder); return extractLibraryFile(snappyNativeLibraryPath, snappyNativeLibraryName, tempFolder);
}
} }
} }

View File

@ -117,7 +117,7 @@ public class SnappyLoaderTest
public static void main(String[] args) { public static void main(String[] args) {
// Test for loading native library specified in -Djava.library.path // Test for loading native library specified in -Djava.library.path
System.setProperty(SnappyLoader.KEY_SNAPPY_ENABLE_BUNDLED_LIBS, "false"); System.setProperty(SnappyLoader.KEY_SNAPPY_USE_SYSTEMLIB, "true");
_logger.debug(Snappy.getNativeLibraryVersion()); _logger.debug(Snappy.getNativeLibraryVersion());
} }
} }