diff --git a/src/main/java/org/xerial/snappy/LoadSnappy.java b/src/main/java/org/xerial/snappy/LoadSnappy.java index b09d73b..5c45726 100755 --- a/src/main/java/org/xerial/snappy/LoadSnappy.java +++ b/src/main/java/org/xerial/snappy/LoadSnappy.java @@ -38,8 +38,39 @@ import java.security.NoSuchAlgorithmException; import java.util.Properties; /** - * This class loads a native library of Snappy according to the platform of the - * user. + * This class loads a native library of snappy-java (snappyjava.dll, + * libsnappy.so, etc.) according to the user platform (os.name and + * os.arch). The natively compiled libraries bundled to snappy-java + * contain the codes of the original snappy and JNI programs to access Snappy. + * + * In default, no configuration is required to use snappy-java, but you can load + * your own native library created by 'make native' command. + * + * LoadSnappy searches for native libraries (snappyjava.dll, libsnappy.so, etc.) + * in the following order: + *
    + *
  1. (System property: org.xerial.snappy.lib.path)/(System property: + * org.xerial.lib.name) + *
  2. One of the libraries embedded in snappy-java-(version).jar extracted into + * (System property: java.io.tempdir or if + * org.xerial.snappy.tempdir is set, use this folder.) + *
  3. Folders in LD_PATH environment variable (This is the default path that + * JVM searches for native libraries) + *
+ * + *

+ * If you do not want to use folder java.io.tempdir, set the System + * property org.xerial.snappy.tempdir. For example, to use + * /tmp/leo as a temporary folder to copy native libraries, use -D option + * of JVM: + * + *

+ * 
+ * java -Dorg.xerial.snappy.tempdir="/tmp/leo" ...
+ * 
+ * 
+ * + *

* * @author leo * @@ -55,6 +86,10 @@ public class LoadSnappy return isLoaded; } + 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"; + /** * Computes the MD5 value of the input stream * @@ -118,7 +153,7 @@ public class LoadSnappy } } - // extract file into the current directory + // extract a native library file into the target directory InputStream reader = LoadSnappy.class.getResourceAsStream(nativeLibraryFilePath); FileOutputStream writer = new FileOutputStream(extractedLibFile); byte[] buffer = new byte[1024]; @@ -130,6 +165,7 @@ public class LoadSnappy writer.close(); reader.close(); + // Set executable (x) flag to enable Java to load the native library if (!System.getProperty("os.name").contains("Windows")) { try { Runtime.getRuntime().exec(new String[] { "chmod", "755", extractedLibFile.getAbsolutePath() }) @@ -183,12 +219,14 @@ public class LoadSnappy } } - // Load the os-dependent library from a jar file + // Load an os-dependent native library inside a jar file snappyNativeLibraryPath = "/org/xerial/snappy/native/" + OSInfo.getNativeLibFolderPathForCurrentOS(); if (LoadSnappy.class.getResource(snappyNativeLibraryPath + "/" + snappyNativeLibraryName) != null) { // Temporary library folder. Use the value of java.io.tmpdir - String tempFolder = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath(); + String tempFolder = new File(System.getProperty(System.getProperty(KEY_SNAPPY_TEMPDIR), "java.io.tmpdir")) + .getAbsolutePath(); + // Try extracting the library from jar if (extractAndLoadLibraryFile(snappyNativeLibraryPath, snappyNativeLibraryName, tempFolder)) { isLoaded = true;