Merge pull request #402 from adamretter/bugfix-native-library-loader

Use correct classloader in Java NativeLibraryLoader
main
Yueh-Hsuan Chiang 10 years ago
commit ec24bd4e6a
  1. 67
      java/org/rocksdb/NativeLibraryLoader.java
  2. 2
      java/org/rocksdb/RocksDB.java

@ -1,6 +1,9 @@
package org.rocksdb; package org.rocksdb;
import java.io.*; import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import org.rocksdb.util.Environment; import org.rocksdb.util.Environment;
/** /**
@ -8,42 +11,56 @@ import org.rocksdb.util.Environment;
* The shared library is extracted to a temp folder and loaded from there. * The shared library is extracted to a temp folder and loaded from there.
*/ */
public class NativeLibraryLoader { public class NativeLibraryLoader {
private static String sharedLibraryName = Environment.getJniLibraryName("rocksdb"); //singleton
private static String tempFileSuffix = "." + Environment.getJniLibraryExtension(); private static final NativeLibraryLoader instance = new NativeLibraryLoader();
private static final String sharedLibraryName = Environment.getJniLibraryName("rocksdb");
private static final String tempFilePrefix = "librocksdbjni";
private static final String tempFileSuffix = "." + Environment.getJniLibraryExtension();
/**
* Get a reference to the NativeLibraryLoader
*
* @return The NativeLibraryLoader
*/
public static NativeLibraryLoader getInstance() {
return instance;
}
public static void loadLibraryFromJar(String tmpDir) /**
* Attempts to extract the native RocksDB library
* from the classpath and load it
*
* @param tmpDir A temporary directory to use
* to copy the native library to. If null,
* or the empty string, we rely on Java's
* {@see java.io.File#createTempFile(String, String) }
* function to provide a temporary location.
* The temporary file will be registered for deletion
* on exit.
*/
public void loadLibraryFromJar(final String tmpDir)
throws IOException { throws IOException {
File temp; final File temp;
String tempFilePrefix = "librocksdbjni"; if(tmpDir == null || tmpDir.equals("")) {
if(tmpDir == null || tmpDir.equals(""))
temp = File.createTempFile(tempFilePrefix, tempFileSuffix); temp = File.createTempFile(tempFilePrefix, tempFileSuffix);
else } else {
temp = new File(tmpDir + "/" + sharedLibraryName); temp = new File(tmpDir, sharedLibraryName);
}
temp.deleteOnExit();
if (!temp.exists()) { if (!temp.exists()) {
throw new RuntimeException("File " + temp.getAbsolutePath() + " does not exist."); throw new RuntimeException("File " + temp.getAbsolutePath() + " does not exist.");
} else {
temp.deleteOnExit();
} }
byte[] buffer = new byte[102400]; // attempt to copy the library from the Jar file to the temp destination
int readBytes; try(final InputStream is = getClass().getClassLoader().getResourceAsStream(sharedLibraryName)) {
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(sharedLibraryName);
if (is == null) { if (is == null) {
throw new RuntimeException(sharedLibraryName + " was not found inside JAR."); throw new RuntimeException(sharedLibraryName + " was not found inside JAR.");
} else {
Files.copy(is, temp.toPath(), StandardCopyOption.REPLACE_EXISTING);
} }
OutputStream os = null;
try {
os = new FileOutputStream(temp);
while ((readBytes = is.read(buffer)) != -1) {
os.write(buffer, 0, readBytes);
}
} finally {
if(os != null)
os.close();
is.close();
} }
System.load(temp.getAbsolutePath()); System.load(temp.getAbsolutePath());

@ -44,7 +44,7 @@ public class RocksDB extends RocksObject {
} }
try try
{ {
NativeLibraryLoader.loadLibraryFromJar(tmpDir); NativeLibraryLoader.getInstance().loadLibraryFromJar(tmpDir);
} }
catch (IOException e) catch (IOException e)
{ {

Loading…
Cancel
Save