mirror of
https://github.com/xerial/snappy-java.git
synced 2025-07-26 07:24:54 +02:00
use static call instead of call by reflection
This commit is contained in:
parent
94b879fe12
commit
8d49dddbba
@ -170,25 +170,42 @@ public class SnappyLoader
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, e.getMessage());
|
throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Prepare SnappyNativeLoader or LocalSnappyNativeLoader
|
// Prepare SnappyNativeLoader or LocalSnappyNativeLoader
|
||||||
Class< ? > nativeLoader = prepareNativeLoader();
|
Class< ? > nativeLoader = prepareNativeLoader();
|
||||||
// Load the code
|
// Load the code
|
||||||
loadNativeLibrary(nativeLoader);
|
loadNativeLibrary(nativeLoader);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (!isLoaded) {
|
||||||
|
// load locally
|
||||||
|
File nativeLib = findNativeLibrary();
|
||||||
|
if (nativeLib != null) {
|
||||||
|
System.load(nativeLib.getAbsolutePath());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Load preinstalled snappyjava (in the path -Djava.library.path)
|
||||||
|
System.loadLibrary("snappyjava");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
api = (SnappyNativeAPI) Class.forName("org.xerial.snappy.SnappyNative").newInstance();
|
||||||
|
isLoaded = true;
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
e.printStackTrace(System.err);
|
||||||
|
throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return api;
|
return api;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Class< ? > prepareNativeLoader() {
|
private static Class< ? > prepareNativeLoader() {
|
||||||
boolean useNativeCodeInjection = !Boolean.parseBoolean(System.getProperty(KEY_SNAPPY_DISABLE_NATIVE_INJECTION,
|
|
||||||
"false"));
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!useNativeCodeInjection) {
|
|
||||||
// Use the local loader
|
|
||||||
return LocalSnappyNativeLoader.class;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Use parent class loader to load SnappyNative, since Tomcat, which uses different class loaders for each webapps, cannot load JNI interface twice
|
// Use parent class loader to load SnappyNative, since Tomcat, which uses different class loaders for each webapps, cannot load JNI interface twice
|
||||||
|
|
||||||
final String nativeLoaderClassName = "org.xerial.snappy.SnappyNativeLoader";
|
final String nativeLoaderClassName = "org.xerial.snappy.SnappyNativeLoader";
|
||||||
@ -205,8 +222,8 @@ public class SnappyLoader
|
|||||||
|
|
||||||
// Create SnappyNative class from a byte code
|
// Create SnappyNative class from a byte code
|
||||||
Class< ? > classLoader = Class.forName("java.lang.ClassLoader");
|
Class< ? > classLoader = Class.forName("java.lang.ClassLoader");
|
||||||
Method defineClass = classLoader.getDeclaredMethod("defineClass", new Class[] { String.class,
|
Method defineClass = classLoader.getDeclaredMethod("defineClass", new Class[] { String.class, byte[].class,
|
||||||
byte[].class, int.class, int.class });
|
int.class, int.class });
|
||||||
|
|
||||||
//ProtectionDomain pd = System.class.getProtectionDomain();
|
//ProtectionDomain pd = System.class.getProtectionDomain();
|
||||||
|
|
||||||
@ -228,7 +245,7 @@ public class SnappyLoader
|
|||||||
|
|
||||||
// Load the SnappyNativeLoader class
|
// Load the SnappyNativeLoader class
|
||||||
return rootClassLoader.loadClass(nativeLoaderClassName);
|
return rootClassLoader.loadClass(nativeLoaderClassName);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
e.printStackTrace(System.err);
|
e.printStackTrace(System.err);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user