Add a target for generationg SnappyNativeLoader bytecode
This commit is contained in:
parent
c675bf4280
commit
1c46977e67
7
Makefile
7
Makefile
|
@ -30,6 +30,13 @@ jni-header: $(SRC)/org/xerial/snappy/SnappyNative.h
|
||||||
$(SRC)/org/xerial/snappy/SnappyNative.h: $(SRC)/org/xerial/snappy/SnappyNative.java
|
$(SRC)/org/xerial/snappy/SnappyNative.h: $(SRC)/org/xerial/snappy/SnappyNative.java
|
||||||
$(JAVAH) -classpath $(TARGET)/classes -o $@ org.xerial.snappy.SnappyNative
|
$(JAVAH) -classpath $(TARGET)/classes -o $@ org.xerial.snappy.SnappyNative
|
||||||
|
|
||||||
|
bytecode: src/main/resources/org/xerial/snappy/SnappyNativeLoader.bytecode
|
||||||
|
|
||||||
|
src/main/resources/org/xerial/snappy/SnappyNativeLoader.bytecode: src/main/resources/org/xerial/snappy/SnappyNativeLoader.java
|
||||||
|
@mkdir -p $(TARGET)/temp
|
||||||
|
$(JAVAC) -source 1.5 -target 1.5 -d $(TARGET)/temp $<
|
||||||
|
cp $(TARGET)/temp/org/xerial/snappy/SnappyNativeLoader.class $@
|
||||||
|
|
||||||
$(SNAPPY_SRC): $(SNAPPY_UNPACKED)
|
$(SNAPPY_SRC): $(SNAPPY_UNPACKED)
|
||||||
|
|
||||||
$(SNAPPY_OUT)/%.o : $(SNAPPY_SRC_DIR)/%.cc
|
$(SNAPPY_OUT)/%.o : $(SNAPPY_SRC_DIR)/%.cc
|
||||||
|
|
7
pom.xml
7
pom.xml
|
@ -232,12 +232,5 @@
|
||||||
<type>jar</type>
|
<type>jar</type>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.javassist</groupId>
|
|
||||||
<artifactId>javassist</artifactId>
|
|
||||||
<version>3.14.0-GA</version>
|
|
||||||
<type>jar</type>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -61,10 +61,16 @@ public class SnappyInputStream extends InputStream
|
||||||
|
|
||||||
protected void readHeader() throws IOException {
|
protected void readHeader() throws IOException {
|
||||||
byte[] header = new byte[SnappyCodec.headerSize()];
|
byte[] header = new byte[SnappyCodec.headerSize()];
|
||||||
int readBytes = in.read(header, 0, header.length);
|
int readBytes = 0;
|
||||||
|
while (readBytes < header.length) {
|
||||||
|
int ret = in.read(header, readBytes, header.length - readBytes);
|
||||||
|
if (ret == -1)
|
||||||
|
break;
|
||||||
|
readBytes += ret;
|
||||||
|
}
|
||||||
|
|
||||||
// Quick test of the header
|
// Quick test of the header
|
||||||
if (header[0] != SnappyCodec.MAGIC_HEADER[0]) {
|
if (readBytes < header.length || header[0] != SnappyCodec.MAGIC_HEADER[0]) {
|
||||||
// do the default uncompression
|
// do the default uncompression
|
||||||
readFully(header, readBytes);
|
readFully(header, readBytes);
|
||||||
return;
|
return;
|
||||||
|
|
Binary file not shown.
|
@ -0,0 +1,59 @@
|
||||||
|
/*--------------------------------------------------------------------------
|
||||||
|
* Copyright 2011 Taro L. Saito
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*--------------------------------------------------------------------------*/
|
||||||
|
//--------------------------------------
|
||||||
|
// XerialJ
|
||||||
|
//
|
||||||
|
// SnappyNativeLoader.java
|
||||||
|
// Since: 2011/07/04 12:10:28
|
||||||
|
//
|
||||||
|
// $URL$
|
||||||
|
// $Author$
|
||||||
|
//--------------------------------------
|
||||||
|
package org.xerial.snappy;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class SnappyNativeLoader
|
||||||
|
{
|
||||||
|
private static HashMap<String, Boolean> loadedLibFiles = new HashMap<String, Boolean>();
|
||||||
|
private static HashMap<String, Boolean> loadedLib = new HashMap<String, Boolean>();
|
||||||
|
|
||||||
|
public static void load(String lib) {
|
||||||
|
if (loadedLibFiles.containsKey(lib) && loadedLibFiles.get(lib) == true)
|
||||||
|
return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
System.load(lib);
|
||||||
|
loadedLibFiles.put(lib, true);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void loadLibrary(String libname) {
|
||||||
|
if (loadedLib.containsKey(libname) && loadedLib.get(libname) == true)
|
||||||
|
return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
System.loadLibrary(libname);
|
||||||
|
loadedLib.put(libname, true);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -30,21 +30,13 @@ import java.io.BufferedInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
import java.security.ProtectionDomain;
|
|
||||||
|
|
||||||
import javassist.ClassPool;
|
|
||||||
import javassist.CtClass;
|
|
||||||
import javassist.CtField;
|
|
||||||
import javassist.CtNewMethod;
|
|
||||||
|
|
||||||
import org.codehaus.plexus.classworlds.ClassWorld;
|
import org.codehaus.plexus.classworlds.ClassWorld;
|
||||||
import org.codehaus.plexus.classworlds.realm.ClassRealm;
|
import org.codehaus.plexus.classworlds.realm.ClassRealm;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.xerial.util.FileResource;
|
import org.xerial.util.FileResource;
|
||||||
import org.xerial.util.log.Logger;
|
import org.xerial.util.log.Logger;
|
||||||
|
@ -110,49 +102,6 @@ public class SnappyLoaderTest
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore
|
|
||||||
@Test
|
|
||||||
public void createByteCodeOfSnappyLoader() throws Exception {
|
|
||||||
|
|
||||||
ClassLoader parent = this.getClass().getClassLoader().getParent();
|
|
||||||
ClassWorld cw = new ClassWorld();
|
|
||||||
ClassRealm L1 = cw.newRealm("l1", parent);
|
|
||||||
ClassRealm L2 = cw.newRealm("l2", parent);
|
|
||||||
|
|
||||||
File nativeLib = SnappyLoader.findNativeLibrary();
|
|
||||||
assertNotNull(nativeLib);
|
|
||||||
|
|
||||||
ClassPool pool = ClassPool.getDefault();
|
|
||||||
CtClass cl = pool.makeClass("org.xerial.snappy.SnappyNativeLoader");
|
|
||||||
cl.addField(CtField.make("static boolean isLoaded = false;", cl));
|
|
||||||
String m1 = loadIntoString(SnappyLoaderTest.class, "load.code");
|
|
||||||
String m2 = loadIntoString(SnappyLoaderTest.class, "loadLibrary.code");
|
|
||||||
cl.addMethod(CtNewMethod.make(m1, cl));
|
|
||||||
cl.addMethod(CtNewMethod.make(m2, cl));
|
|
||||||
|
|
||||||
ProtectionDomain systemPD = System.class.getProtectionDomain();
|
|
||||||
byte[] bytecode = cl.toBytecode();
|
|
||||||
FileOutputStream f = new FileOutputStream("target/SnappyNativeLoader.bytecode");
|
|
||||||
f.write(bytecode);
|
|
||||||
f.close();
|
|
||||||
|
|
||||||
//Class< ? > loaderClass = cl.toClass(parent, System.class.getProtectionDomain());
|
|
||||||
//_logger.info(cl.getName());
|
|
||||||
//Class< ? > loaderClass = cl.toClass();
|
|
||||||
|
|
||||||
// Class< ? > classLoader = Class.forName("java.lang.ClassLoader");
|
|
||||||
// java.lang.reflect.Method defineClass = classLoader.getDeclaredMethod("defineClass", new Class[] { String.class,
|
|
||||||
// byte[].class, int.class, int.class, ProtectionDomain.class });
|
|
||||||
//
|
|
||||||
// defineClass.setAccessible(true);
|
|
||||||
// defineClass.invoke(parent, cl.getName(), bytecode, 0, bytecode.length, System.class.getProtectionDomain());
|
|
||||||
//
|
|
||||||
// Class< ? > forName = parent.loadClass("org.xerial.snappy.SnappyNativeLoader");
|
|
||||||
|
|
||||||
//Class< ? > snappyClass = L1.loadClass("org.xerial.snappy.Snappy"); // not found
|
|
||||||
//_logger.info(snappyClass.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void load() throws Exception {
|
public void load() throws Exception {
|
||||||
SnappyLoader.load();
|
SnappyLoader.load();
|
||||||
|
|
Loading…
Reference in New Issue