more robust native loader
This commit is contained in:
parent
a9966f4e58
commit
6d0db1f53f
|
@ -19,10 +19,9 @@ import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.net.URL;
|
|
||||||
import java.rmi.server.ExportException;
|
import java.rmi.server.ExportException;
|
||||||
|
import java.security.AccessController;
|
||||||
import io.netty.util.internal.PlatformDependent;
|
import java.security.PrivilegedAction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the specified library, extracting it from the jar, if necessary
|
* Loads the specified library, extracting it from the jar, if necessary
|
||||||
|
@ -31,7 +30,7 @@ public
|
||||||
class NativeLoader {
|
class NativeLoader {
|
||||||
|
|
||||||
public static
|
public static
|
||||||
void loadLibrary(final String sourceFileName, final String destinationPrefix, final Class<?> classLoaderClass, String version)
|
File extractLibrary(final String sourceFileName, final String destinationDirectory, final String destinationName, String version)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
try {
|
try {
|
||||||
String suffix;
|
String suffix;
|
||||||
|
@ -45,15 +44,18 @@ class NativeLoader {
|
||||||
suffix = ".dylib";
|
suffix = ".dylib";
|
||||||
}
|
}
|
||||||
|
|
||||||
final String outputFileName = destinationPrefix + "." + version + suffix;
|
final String outputFileName;
|
||||||
|
if (version == null) {
|
||||||
final File file = new File(OS.TEMP_DIR, outputFileName);
|
outputFileName = destinationName + suffix;
|
||||||
if (!file.canRead()) {
|
}
|
||||||
ClassLoader loader = PlatformDependent.getClassLoader(classLoaderClass);
|
else {
|
||||||
URL url = loader.getResource(sourceFileName);
|
outputFileName = destinationName + "." + version + suffix;
|
||||||
|
}
|
||||||
|
|
||||||
|
final File file = new File(destinationDirectory, outputFileName);
|
||||||
|
if (!file.canRead() || file.length() == 0 || !file.canExecute()) {
|
||||||
// now we copy it out
|
// now we copy it out
|
||||||
final InputStream inputStream = url.openStream();
|
final InputStream inputStream = LocationResolver.getResourceAsStream(sourceFileName);
|
||||||
|
|
||||||
OutputStream outStream = null;
|
OutputStream outStream = null;
|
||||||
try {
|
try {
|
||||||
|
@ -82,12 +84,25 @@ class NativeLoader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.load(file.getAbsolutePath());
|
return file;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ExportException("Error extracting library: " + sourceFileName, e);
|
throw new ExportException("Error extracting library: " + sourceFileName, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static
|
||||||
|
void loadLibrary(final File file) {
|
||||||
|
// inject into the correct classloader
|
||||||
|
AccessController.doPrivileged(new PrivilegedAction<Object>() {
|
||||||
|
@Override
|
||||||
|
public
|
||||||
|
Object run() {
|
||||||
|
System.load(file.getAbsolutePath());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private
|
private
|
||||||
NativeLoader() {
|
NativeLoader() {
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue