|
24 | 24 | //-------------------------------------- |
25 | 25 | package org.xerial.snappy; |
26 | 26 |
|
| 27 | +import static org.junit.Assert.*; |
| 28 | + |
| 29 | +import java.io.BufferedInputStream; |
| 30 | +import java.io.ByteArrayOutputStream; |
| 31 | +import java.io.File; |
| 32 | +import java.io.FileNotFoundException; |
| 33 | +import java.io.FileOutputStream; |
| 34 | +import java.io.IOException; |
| 35 | +import java.net.URL; |
| 36 | +import java.security.ProtectionDomain; |
| 37 | + |
| 38 | +import javassist.ClassPool; |
| 39 | +import javassist.CtClass; |
| 40 | +import javassist.CtField; |
| 41 | +import javassist.CtNewMethod; |
| 42 | + |
| 43 | +import org.codehaus.plexus.classworlds.ClassWorld; |
| 44 | +import org.codehaus.plexus.classworlds.realm.ClassRealm; |
27 | 45 | import org.junit.Test; |
| 46 | +import org.xerial.util.FileResource; |
28 | 47 | import org.xerial.util.log.Logger; |
29 | 48 |
|
30 | 49 | public class SnappyLoaderTest |
31 | 50 | { |
32 | 51 | private static Logger _logger = Logger.getLogger(SnappyLoaderTest.class); |
33 | 52 |
|
34 | | - // @Ignore |
35 | | - // @Test |
36 | | - // public void loadFromSytemClassLoader() throws Exception { |
37 | | - // |
38 | | - // ClassLoader parent = this.getClass().getClassLoader().getParent(); |
39 | | - // ClassWorld cw = new ClassWorld(); |
40 | | - // ClassRealm L1 = cw.newRealm("l1", parent); |
41 | | - // ClassRealm L2 = cw.newRealm("l2", parent); |
42 | | - // |
43 | | - // File nativeLib = SnappyLoader.findNativeLibrary(); |
44 | | - // assertNotNull(nativeLib); |
45 | | - // |
46 | | - // ClassPool pool = ClassPool.getDefault(); |
47 | | - // CtClass cl = pool.makeClass("org.xerial.snappy.SnappyNativeLoader"); |
48 | | - // cl.addField(CtField.make("static boolean isLoaded = false;", cl)); |
49 | | - // String m1 = FileResource.loadIntoString(SnappyLoaderTest.class, "load.code"); |
50 | | - // String m2 = FileResource.loadIntoString(SnappyLoaderTest.class, "loadLibrary.code"); |
51 | | - // cl.addMethod(CtNewMethod.make(m1, cl)); |
52 | | - // cl.addMethod(CtNewMethod.make(m2, cl)); |
53 | | - // |
54 | | - // ProtectionDomain systemPD = System.class.getProtectionDomain(); |
55 | | - // byte[] bytecode = cl.toBytecode(); |
56 | | - // FileOutputStream f = new FileOutputStream("src/main/resources/org/xerial/snappy/SnappyNativeLoader.bytecode"); |
57 | | - // f.write(bytecode); |
58 | | - // f.close(); |
59 | | - // |
60 | | - // //Class< ? > loaderClass = cl.toClass(parent, System.class.getProtectionDomain()); |
61 | | - // //_logger.info(cl.getName()); |
62 | | - // //Class< ? > loaderClass = cl.toClass(); |
63 | | - // |
64 | | - // Class< ? > classLoader = Class.forName("java.lang.ClassLoader"); |
65 | | - // java.lang.reflect.Method defineClass = classLoader.getDeclaredMethod("defineClass", new Class[] { String.class, |
66 | | - // byte[].class, int.class, int.class, ProtectionDomain.class }); |
67 | | - // |
68 | | - // defineClass.setAccessible(true); |
69 | | - // defineClass.invoke(parent, cl.getName(), bytecode, 0, bytecode.length, System.class.getProtectionDomain()); |
70 | | - // |
71 | | - // Class< ? > forName = parent.loadClass("org.xerial.snappy.SnappyNativeLoader"); |
72 | | - // _logger.info(forName.toString()); |
73 | | - // |
74 | | - // //Class< ? > snappyClass = L1.loadClass("org.xerial.snappy.Snappy"); // not found |
75 | | - // //_logger.info(snappyClass.getName()); |
76 | | - // |
77 | | - // } |
| 53 | + public static BufferedInputStream openByteStream(Class< ? > referenceClass, String resourceFileName) |
| 54 | + throws IOException { |
| 55 | + URL url = FileResource.find(referenceClass, resourceFileName); |
| 56 | + if (url != null) { |
| 57 | + return new BufferedInputStream(url.openStream()); |
| 58 | + } |
| 59 | + else |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + public static <T> String loadIntoString(Class<T> referenceClass, String path) throws IOException { |
| 64 | + BufferedInputStream in = openByteStream(referenceClass, path); |
| 65 | + if (in == null) |
| 66 | + throw new FileNotFoundException( |
| 67 | + String.format("reference class:%s, path:%s", referenceClass.getName(), path)); |
| 68 | + |
| 69 | + ByteArrayOutputStream buf = new ByteArrayOutputStream(); |
| 70 | + try { |
| 71 | + byte[] tmp = new byte[4028]; |
| 72 | + for (int readBytes = 0; (readBytes = in.read(tmp)) != -1;) { |
| 73 | + buf.write(tmp, 0, readBytes); |
| 74 | + } |
| 75 | + buf.flush(); |
| 76 | + return buf.toString(); |
| 77 | + } |
| 78 | + finally { |
| 79 | + in.close(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void loadFromSytemClassLoader() throws Exception { |
| 85 | + |
| 86 | + ClassLoader parent = this.getClass().getClassLoader().getParent(); |
| 87 | + ClassWorld cw = new ClassWorld(); |
| 88 | + ClassRealm L1 = cw.newRealm("l1", parent); |
| 89 | + ClassRealm L2 = cw.newRealm("l2", parent); |
| 90 | + |
| 91 | + File nativeLib = SnappyLoader.findNativeLibrary(); |
| 92 | + assertNotNull(nativeLib); |
| 93 | + |
| 94 | + ClassPool pool = ClassPool.getDefault(); |
| 95 | + CtClass cl = pool.makeClass("org.xerial.snappy.SnappyNativeLoader"); |
| 96 | + cl.addField(CtField.make("static boolean isLoaded = false;", cl)); |
| 97 | + String m1 = loadIntoString(SnappyLoaderTest.class, "load.code"); |
| 98 | + String m2 = loadIntoString(SnappyLoaderTest.class, "loadLibrary.code"); |
| 99 | + cl.addMethod(CtNewMethod.make(m1, cl)); |
| 100 | + cl.addMethod(CtNewMethod.make(m2, cl)); |
| 101 | + |
| 102 | + ProtectionDomain systemPD = System.class.getProtectionDomain(); |
| 103 | + byte[] bytecode = cl.toBytecode(); |
| 104 | + FileOutputStream f = new FileOutputStream("target/SnappyNativeLoader.bytecode"); |
| 105 | + f.write(bytecode); |
| 106 | + f.close(); |
| 107 | + |
| 108 | + //Class< ? > loaderClass = cl.toClass(parent, System.class.getProtectionDomain()); |
| 109 | + //_logger.info(cl.getName()); |
| 110 | + //Class< ? > loaderClass = cl.toClass(); |
| 111 | + |
| 112 | + Class< ? > classLoader = Class.forName("java.lang.ClassLoader"); |
| 113 | + java.lang.reflect.Method defineClass = classLoader.getDeclaredMethod("defineClass", new Class[] { String.class, |
| 114 | + byte[].class, int.class, int.class, ProtectionDomain.class }); |
| 115 | + |
| 116 | + defineClass.setAccessible(true); |
| 117 | + defineClass.invoke(parent, cl.getName(), bytecode, 0, bytecode.length, System.class.getProtectionDomain()); |
| 118 | + |
| 119 | + Class< ? > forName = parent.loadClass("org.xerial.snappy.SnappyNativeLoader"); |
| 120 | + |
| 121 | + //Class< ? > snappyClass = L1.loadClass("org.xerial.snappy.Snappy"); // not found |
| 122 | + //_logger.info(snappyClass.getName()); |
| 123 | + |
| 124 | + } |
78 | 125 |
|
79 | 126 | @Test |
80 | 127 | public void load() throws Exception { |
|
0 commit comments