An attempt to use system class loader when loading dll
This commit is contained in:
parent
53f0be52ba
commit
9f71f8eb1c
7
pom.xml
7
pom.xml
|
@ -195,5 +195,12 @@
|
|||
<type>jar</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.plexus</groupId>
|
||||
<artifactId>plexus-classworlds</artifactId>
|
||||
<version>2.4</version>
|
||||
<type>jar</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -48,8 +48,10 @@ public class LoadSnappy
|
|||
private static boolean isLoaded = false;
|
||||
|
||||
public static boolean load() {
|
||||
if (!isLoaded)
|
||||
if (!isLoaded) {
|
||||
ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
||||
loadSnappyNativeLibrary();
|
||||
}
|
||||
return isLoaded;
|
||||
}
|
||||
|
||||
|
|
|
@ -24,6 +24,9 @@
|
|||
//--------------------------------------
|
||||
package org.xerial.snappy;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
|
@ -35,6 +38,46 @@ import java.nio.ByteBuffer;
|
|||
public class Snappy
|
||||
{
|
||||
|
||||
static {
|
||||
URL resource = Snappy.class.getResource("/org/xerial/snappy/SnappyNative.class");
|
||||
// jar:file:/C:/.../snappy-java-1.0.1-rc4-SNAPSHOT.jar!/org/xerial/snappy/SnappyNative.class
|
||||
if (resource == null) {
|
||||
LoadSnappy.load();
|
||||
}
|
||||
else {
|
||||
String path = resource.toString();
|
||||
if (path.startsWith("jar:")) {
|
||||
int pos = path.indexOf(".jar!");
|
||||
if (pos >= 0) {
|
||||
path = path.substring(4, pos + 4);
|
||||
}
|
||||
}
|
||||
else {
|
||||
path = path.replace("SnappyNative", "LoadSnappy");
|
||||
}
|
||||
try {
|
||||
URL loaderPath = new URL(path);
|
||||
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
|
||||
if (URLClassLoader.class.isInstance(systemClassLoader)) {
|
||||
|
||||
URLClassLoader cl = URLClassLoader.class.cast(systemClassLoader);
|
||||
Method addURLmethod = URLClassLoader.class.getDeclaredMethod("addURL",
|
||||
new Class< ? >[] { URL.class });
|
||||
addURLmethod.setAccessible(true);
|
||||
|
||||
addURLmethod.invoke(cl, loaderPath);
|
||||
Class< ? > snappyLoader = cl.loadClass("org.xerial.snappy.LoadSnappy");
|
||||
Method loader = snappyLoader.getMethod("load");
|
||||
loader.invoke(null);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the native library version of the snappy
|
||||
*
|
||||
|
|
|
@ -34,9 +34,6 @@ import java.nio.ByteBuffer;
|
|||
*/
|
||||
public class SnappyNative
|
||||
{
|
||||
static {
|
||||
LoadSnappy.load();
|
||||
}
|
||||
|
||||
public native static String nativeLibraryVersion();
|
||||
|
||||
|
|
Binary file not shown.
|
@ -210,7 +210,12 @@ public class SnappyTest
|
|||
byte[] compressed = Snappy.compress(input.getBytes("UTF-8"));
|
||||
byte[] uncompressed = Snappy.uncompress(compressed);
|
||||
String result = new String(uncompressed, "UTF-8");
|
||||
System.out.println(result);
|
||||
_logger.debug(result);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadByDifferentClassLoader() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue