diff --git a/src/main/java/org/xerial/snappy/SnappyLoader.java b/src/main/java/org/xerial/snappy/SnappyLoader.java index 7e29779..91961bd 100755 --- a/src/main/java/org/xerial/snappy/SnappyLoader.java +++ b/src/main/java/org/xerial/snappy/SnappyLoader.java @@ -82,13 +82,13 @@ import java.util.Properties; */ public class SnappyLoader { - 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_TEMPDIR = "org.xerial.snappy.tempdir"; - public static final String KEY_SNAPPY_DISABLE_BUNDLED_LIBS = "org.xerial.snappy.disable.bundled.libs"; + 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_TEMPDIR = "org.xerial.snappy.tempdir"; + public static final String KEY_SNAPPY_ENABLE_BUNDLED_LIBS = "org.xerial.snappy.enable.bundled.libs"; - private static boolean isLoaded = false; - private static SnappyNativeAPI api = null; + private static boolean isLoaded = false; + private static SnappyNativeAPI api = null; private static ClassLoader getRootClassLoader() { ClassLoader cl = Thread.currentThread().getContextClassLoader(); @@ -364,7 +364,7 @@ public class SnappyLoader String snappyNativeLibraryPath = System.getProperty(KEY_SNAPPY_LIB_PATH); String snappyNativeLibraryName = System.getProperty(KEY_SNAPPY_LIB_NAME); - if (System.getProperty(KEY_SNAPPY_DISABLE_BUNDLED_LIBS, "false").equals("false")) { + if (System.getProperty(KEY_SNAPPY_ENABLE_BUNDLED_LIBS, "true").equals("true")) { // Resolve the library file name with a suffix (e.g., dll, .so, etc.) if (snappyNativeLibraryName == null) snappyNativeLibraryName = System.mapLibraryName("snappyjava"); @@ -374,23 +374,23 @@ public class SnappyLoader if (nativeLib.exists()) return nativeLib; } - } - { - // Load an OS-dependent native library inside a jar file - snappyNativeLibraryPath = "/org/xerial/snappy/native/" + OSInfo.getNativeLibFolderPathForCurrentOS(); + { + // Load an OS-dependent native library inside a jar file + snappyNativeLibraryPath = "/org/xerial/snappy/native/" + OSInfo.getNativeLibFolderPathForCurrentOS(); - if (SnappyLoader.class.getResource(snappyNativeLibraryPath + "/" + snappyNativeLibraryName) != null) { - // Temporary library folder. Use the value of org.xerial.snappy.tempdir or java.io.tmpdir - String tempFolder = new File(System.getProperty(KEY_SNAPPY_TEMPDIR, - System.getProperty("java.io.tmpdir"))).getAbsolutePath(); + if (SnappyLoader.class.getResource(snappyNativeLibraryPath + "/" + snappyNativeLibraryName) != null) { + // Temporary library folder. Use the value of org.xerial.snappy.tempdir or java.io.tmpdir + String tempFolder = new File(System.getProperty(KEY_SNAPPY_TEMPDIR, + System.getProperty("java.io.tmpdir"))).getAbsolutePath(); - // Extract and load a native library inside the jar file - return extractLibraryFile(snappyNativeLibraryPath, snappyNativeLibraryName, tempFolder); + // Extract and load a native library inside the jar file + return extractLibraryFile(snappyNativeLibraryPath, snappyNativeLibraryName, tempFolder); + } } } - return null; + return null; // Use a pre-installed snappyjava } public static String getVersion() { diff --git a/src/test/java/org/xerial/snappy/SnappyLoaderTest.java b/src/test/java/org/xerial/snappy/SnappyLoaderTest.java index d93d8ff..ced1778 100755 --- a/src/test/java/org/xerial/snappy/SnappyLoaderTest.java +++ b/src/test/java/org/xerial/snappy/SnappyLoaderTest.java @@ -95,24 +95,18 @@ public class SnappyLoaderTest ClassRealm L1 = cw.newRealm("l1", URLClassLoader.newInstance(new URL[] { classPath }, parent)); ClassRealm L2 = cw.newRealm("l2", URLClassLoader.newInstance(new URL[] { classPath }, parent)); - // Actually load Snappy.class in a class loader - try { - Class< ? > S1 = L1.loadClass("org.xerial.snappy.Snappy"); - Method m = S1.getMethod("getNativeLibraryVersion"); - String v = (String) m.invoke(null); + // Actually load Snappy.class in a child class loader - // Load Snappy.class from another class loader - Class< ? > S2 = L2.loadClass("org.xerial.snappy.Snappy"); - Method m2 = S2.getMethod("getNativeLibraryVersion"); - String v2 = (String) m2.invoke(null); + Class< ? > S1 = L1.loadClass("org.xerial.snappy.Snappy"); + Method m = S1.getMethod("getNativeLibraryVersion"); + String v = (String) m.invoke(null); - assertEquals(v, v2); - } - catch (Exception e) { - e.printStackTrace(); - throw e; - } + // Load Snappy.class from another child class loader + Class< ? > S2 = L2.loadClass("org.xerial.snappy.Snappy"); + Method m2 = S2.getMethod("getNativeLibraryVersion"); + String v2 = (String) m2.invoke(null); + assertEquals(v, v2); } @Test @@ -120,4 +114,10 @@ public class SnappyLoaderTest SnappyLoader.load(); _logger.debug(Snappy.getNativeLibraryVersion()); } + + public static void main(String[] args) { + // Test for loading native library specified in -Djava.library.path + System.setProperty(SnappyLoader.KEY_SNAPPY_ENABLE_BUNDLED_LIBS, "false"); + _logger.debug(Snappy.getNativeLibraryVersion()); + } }