Add a target for generationg SnappyNativeLoader bytecode

This commit is contained in:
Taro L. Saito 2011-07-06 10:46:47 +09:00
parent c675bf4280
commit 1c46977e67
6 changed files with 160 additions and 146 deletions

179
Makefile
View File

@ -1,86 +1,93 @@
include Makefile.common include Makefile.common
MVN:=mvn MVN:=mvn
all: snappy all: snappy
SNAPPY_OUT:=$(TARGET)/$(snappy)-$(os_arch) SNAPPY_OUT:=$(TARGET)/$(snappy)-$(os_arch)
SNAPPY_ARCHIVE:=$(TARGET)/snappy-$(VERSION).tar.gz SNAPPY_ARCHIVE:=$(TARGET)/snappy-$(VERSION).tar.gz
SNAPPY_CC:=snappy-sinksource.cc snappy-stubs-internal.cc snappy.cc SNAPPY_CC:=snappy-sinksource.cc snappy-stubs-internal.cc snappy.cc
SNAPPY_SRC_DIR:=$(TARGET)/snappy-$(VERSION) SNAPPY_SRC_DIR:=$(TARGET)/snappy-$(VERSION)
SNAPPY_SRC:=$(addprefix $(SNAPPY_SRC_DIR)/,$(SNAPPY_CC)) SNAPPY_SRC:=$(addprefix $(SNAPPY_SRC_DIR)/,$(SNAPPY_CC))
SNAPPY_OBJ:=$(addprefix $(SNAPPY_OUT)/,$(patsubst %.cc,%.o,$(SNAPPY_CC)) SnappyNative.o) SNAPPY_OBJ:=$(addprefix $(SNAPPY_OUT)/,$(patsubst %.cc,%.o,$(SNAPPY_CC)) SnappyNative.o)
SNAPPY_UNPACKED:=$(TARGET)/snappy-extracted.log SNAPPY_UNPACKED:=$(TARGET)/snappy-extracted.log
CXXFLAGS:=$(CXXFLAGS) -I$(SNAPPY_SRC_DIR) CXXFLAGS:=$(CXXFLAGS) -I$(SNAPPY_SRC_DIR)
$(SNAPPY_ARCHIVE): $(SNAPPY_ARCHIVE):
@mkdir -p $(@D) @mkdir -p $(@D)
curl -o$@ http://snappy.googlecode.com/files/snappy-$(VERSION).tar.gz curl -o$@ http://snappy.googlecode.com/files/snappy-$(VERSION).tar.gz
$(SNAPPY_UNPACKED): $(SNAPPY_ARCHIVE) $(SNAPPY_UNPACKED): $(SNAPPY_ARCHIVE)
tar xvfz $< -C $(TARGET) tar xvfz $< -C $(TARGET)
touch $@ touch $@
jni-header: $(SRC)/org/xerial/snappy/SnappyNative.h 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
$(SNAPPY_SRC): $(SNAPPY_UNPACKED) bytecode: src/main/resources/org/xerial/snappy/SnappyNativeLoader.bytecode
$(SNAPPY_OUT)/%.o : $(SNAPPY_SRC_DIR)/%.cc src/main/resources/org/xerial/snappy/SnappyNativeLoader.bytecode: src/main/resources/org/xerial/snappy/SnappyNativeLoader.java
@mkdir -p $(@D) @mkdir -p $(TARGET)/temp
$(CXX) $(CXXFLAGS) -c $< -o $@ $(JAVAC) -source 1.5 -target 1.5 -d $(TARGET)/temp $<
cp $(TARGET)/temp/org/xerial/snappy/SnappyNativeLoader.class $@
$(SNAPPY_OUT)/SnappyNative.o : $(SRC)/org/xerial/snappy/SnappyNative.cpp $(SRC)/org/xerial/snappy/SnappyNative.h
@mkdir -p $(@D) $(SNAPPY_SRC): $(SNAPPY_UNPACKED)
$(CXX) $(CXXFLAGS) -c $< -o $@
$(SNAPPY_OUT)/%.o : $(SNAPPY_SRC_DIR)/%.cc
@mkdir -p $(@D)
$(SNAPPY_OUT)/$(LIBNAME): $(SNAPPY_OBJ) $(CXX) $(CXXFLAGS) -c $< -o $@
$(CXX) $(CXXFLAGS) -o $@ $+ $(LINKFLAGS)
$(STRIP) $@ $(SNAPPY_OUT)/SnappyNative.o : $(SRC)/org/xerial/snappy/SnappyNative.cpp $(SRC)/org/xerial/snappy/SnappyNative.h
@mkdir -p $(@D)
clean-native: $(CXX) $(CXXFLAGS) -c $< -o $@
rm -rf $(SNAPPY_OUT)
clean: $(SNAPPY_OUT)/$(LIBNAME): $(SNAPPY_OBJ)
rm -rf $(TARGET) $(CXX) $(CXXFLAGS) -o $@ $+ $(LINKFLAGS)
$(STRIP) $@
NATIVE_DIR:=src/main/resources/org/xerial/snappy/native/$(OS_NAME)/$(OS_ARCH)
NATIVE_TARGET_DIR:=$(TARGET)/classes/org/xerial/snappy/native/$(OS_NAME)/$(OS_ARCH) clean-native:
NATIVE_DLL:=$(NATIVE_DIR)/$(LIBNAME) rm -rf $(SNAPPY_OUT)
snappy-jar-version:=snappy-java-$(shell $(JAVA) -jar lib/silk-weaver.jar find 'project(artifactId, version)' pom.xml | grep snappy-java | awk '{ print $$2; }') clean:
rm -rf $(TARGET)
native: $(SNAPPY_UNPACKED) $(NATIVE_DLL)
snappy: native $(TARGET)/$(snappy-jar-version).jar NATIVE_DIR:=src/main/resources/org/xerial/snappy/native/$(OS_NAME)/$(OS_ARCH)
NATIVE_TARGET_DIR:=$(TARGET)/classes/org/xerial/snappy/native/$(OS_NAME)/$(OS_ARCH)
$(NATIVE_DLL): $(SNAPPY_OUT)/$(LIBNAME) NATIVE_DLL:=$(NATIVE_DIR)/$(LIBNAME)
@mkdir -p $(@D)
cp $< $@ snappy-jar-version:=snappy-java-$(shell $(JAVA) -jar lib/silk-weaver.jar find 'project(artifactId, version)' pom.xml | grep snappy-java | awk '{ print $$2; }')
@mkdir -p $(NATIVE_TARGET_DIR)
cp $< $(NATIVE_TARGET_DIR)/$(LIBNAME) native: $(SNAPPY_UNPACKED) $(NATIVE_DLL)
snappy: native $(TARGET)/$(snappy-jar-version).jar
$(TARGET)/$(snappy-jar-version).jar: native $(NATIVE_DLL) $(NATIVE_DLL): $(SNAPPY_OUT)/$(LIBNAME)
$(MVN) package -Dmaven.test.skip=true @mkdir -p $(@D)
cp $< $@
test: $(NATIVE_DLL) @mkdir -p $(NATIVE_TARGET_DIR)
$(MVN) test cp $< $(NATIVE_TARGET_DIR)/$(LIBNAME)
win32:
$(MAKE) native CXX=mingw32-g++ OS_NAME=Windows OS_ARCH=x86 $(TARGET)/$(snappy-jar-version).jar: native $(NATIVE_DLL)
$(MVN) package -Dmaven.test.skip=true
mac32:
$(MAKE) native OS_NAME=Mac OS_ARCH=i386 test: $(NATIVE_DLL)
$(MVN) test
linux32:
$(MAKE) native OS_NAME=Linux OS_ARCH=i386 win32:
$(MAKE) native CXX=mingw32-g++ OS_NAME=Windows OS_ARCH=x86
clean-native-linux32:
$(MAKE) clean-native OS_NAME=Linux OS_ARCH=i386 mac32:
$(MAKE) native OS_NAME=Mac OS_ARCH=i386
linux32:
$(MAKE) native OS_NAME=Linux OS_ARCH=i386
clean-native-linux32:
$(MAKE) clean-native OS_NAME=Linux OS_ARCH=i386

View File

@ -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>

View File

@ -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;

View File

@ -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();
}
}
}

View File

@ -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();