- * (root class loader) -> [SnappyNativeLoader (load JNI code), SnappyNative (has native methods), SnappyNativeAPI, SnappyErrorCode] (injected by this method) - * | - * | - * (child class loader) -> Sees the above classes loaded by the root class loader. - * Then creates SnappyNativeAPI implementation by instantiating SnappyNaitive class. - *- * - * - *
- * (root class loader) -> [SnappyNativeLoader, SnappyNative ...] -> native code is loaded by once in this class loader - * | \ - * | (child2 class loader) - * (child1 class loader) - * - * child1 and child2 share the same SnappyNative code loaded by the root class loader. - *- * - * Note that Java's class loader first delegates the class lookup to its - * parent class loader. So once SnappyNativeLoader is loaded by the root - * class loader, no child class loader initialize SnappyNativeLoader again. - * - * @return - */ - static synchronized Object load() + static synchronized SnappyNative load() { if (api != null) return api; try { - if (!hasInjectedNativeLoader()) { - // Inject SnappyNativeLoader (src/main/resources/org/xerial/snappy/SnappyNativeLoader.bytecode) to the root class loader - Class< ? > nativeLoader = injectSnappyNativeLoader(); - // Load the JNI code using the injected loader - loadNativeLibrary(nativeLoader); - } + loadNativeLibrary(); + setApi(new SnappyNative()); isLoaded = true; - // Look up SnappyNative, injected to the root classloder, using reflection in order to avoid the initialization of SnappyNative class in this context class loader. - Object nativeCode = Class.forName("org.xerial.snappy.SnappyNative").newInstance(); - setApi(nativeCode); } catch (Exception e) { e.printStackTrace(); @@ -243,119 +156,40 @@ public class SnappyLoader } /** - * Inject SnappyNativeLoader class to the root class loader - * - * @return native code loader class initialized in the root class loader + * Load a native library of snappy-java */ - private static Class< ? > injectSnappyNativeLoader() { + private static void loadNativeLibrary() { - try { - // 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"; - ClassLoader rootClassLoader = getRootClassLoader(); - // Load a byte code - byte[] byteCode = getByteCode("/org/xerial/snappy/SnappyNativeLoader.bytecode"); - // In addition, we need to load the other dependent classes (e.g., SnappyNative and SnappyException) using the system class loader - final String[] classesToPreload = new String[] { "org.xerial.snappy.SnappyNativeAPI", - "org.xerial.snappy.SnappyNative", "org.xerial.snappy.SnappyErrorCode" }; - List
@@ -40,7 +39,7 @@ import java.nio.ByteBuffer; * @author leo * */ -public class SnappyNative implements SnappyNativeAPI +public class SnappyNative { public native String nativeLibraryVersion(); diff --git a/src/main/java/org/xerial/snappy/SnappyNativeAPI.java b/src/main/java/org/xerial/snappy/SnappyNativeAPI.java deleted file mode 100755 index b6c92d0..0000000 --- a/src/main/java/org/xerial/snappy/SnappyNativeAPI.java +++ /dev/null @@ -1,82 +0,0 @@ -/*-------------------------------------------------------------------------- - * 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. - *--------------------------------------------------------------------------*/ -//-------------------------------------- -// snappy-java Project -// -// SnappyNative.java -// Since: 2011/03/30 -// -// $URL$ -// $Author$ -//-------------------------------------- -package org.xerial.snappy; - -import java.io.IOException; -import java.nio.ByteBuffer; - -/** - * Internal only - Do not use this class. - * - * Interface to access the native code of Snappy. Although this class members - * are public, do not use them directly. Use {@link Snappy} API instead. - * - * - * @author leo - * - */ -public interface SnappyNativeAPI -{ - - public String nativeLibraryVersion(); - - // ------------------------------------------------------------------------ - // Generic compression/decompression routines. - // ------------------------------------------------------------------------ - public long rawCompress(long inputAddr, long inputSize, long destAddr) throws IOException; - public long rawUncompress(long inputAddr, long inputSize, long destAddr) throws IOException; - - public int rawCompress(ByteBuffer input, int inputOffset, int inputLength, ByteBuffer compressed, int outputOffset) - throws IOException; - - public int rawCompress(Object input, int inputOffset, int inputByteLength, Object output, int outputOffset) throws IOException; - - public int rawUncompress(ByteBuffer compressed, int inputOffset, int inputLength, ByteBuffer uncompressed, - int outputOffset) throws IOException; - - public int rawUncompress(Object input, int inputOffset, int inputLength, Object output, int outputOffset) - throws IOException; - - // Returns the maximal size of the compressed representation of - // input data that is "source_bytes" bytes in length; - public int maxCompressedLength(int source_bytes); - - // This operation takes O(1) time. - public int uncompressedLength(ByteBuffer compressed, int offset, int len) throws IOException; - - public int uncompressedLength(Object input, int offset, int len) throws IOException; - - public long uncompressedLength(long inputAddr, long len) throws IOException; - - - public boolean isValidCompressedBuffer(ByteBuffer compressed, int offset, int len) throws IOException; - - public boolean isValidCompressedBuffer(Object input, int offset, int len) throws IOException; - - public void arrayCopy(Object src, int offset, int byteLength, Object dest, int dOffset) throws IOException; - - public void throw_error(int errorCode) throws IOException; - -} diff --git a/src/main/resources/org/xerial/snappy/native/Linux/i386/libsnappyjava.so b/src/main/resources/org/xerial/snappy/native/Linux/x86/libsnappyjava.so similarity index 100% rename from src/main/resources/org/xerial/snappy/native/Linux/i386/libsnappyjava.so rename to src/main/resources/org/xerial/snappy/native/Linux/x86/libsnappyjava.so diff --git a/src/main/resources/org/xerial/snappy/native/Linux/amd64/libsnappyjava.so b/src/main/resources/org/xerial/snappy/native/Linux/x86_64/libsnappyjava.so similarity index 100% rename from src/main/resources/org/xerial/snappy/native/Linux/amd64/libsnappyjava.so rename to src/main/resources/org/xerial/snappy/native/Linux/x86_64/libsnappyjava.so diff --git a/src/main/resources/org/xerial/snappy/native/Mac/i386/libsnappyjava.jnilib b/src/main/resources/org/xerial/snappy/native/Mac/x86/libsnappyjava.jnilib similarity index 100% rename from src/main/resources/org/xerial/snappy/native/Mac/i386/libsnappyjava.jnilib rename to src/main/resources/org/xerial/snappy/native/Mac/x86/libsnappyjava.jnilib diff --git a/src/main/resources/org/xerial/snappy/native/OpenBSD/i386/libsnappyjava.so b/src/main/resources/org/xerial/snappy/native/OpenBSD/x86/libsnappyjava.so similarity index 100% rename from src/main/resources/org/xerial/snappy/native/OpenBSD/i386/libsnappyjava.so rename to src/main/resources/org/xerial/snappy/native/OpenBSD/x86/libsnappyjava.so diff --git a/src/main/resources/org/xerial/snappy/native/OpenBSD/amd64/libsnappyjava.so b/src/main/resources/org/xerial/snappy/native/OpenBSD/x86_64/libsnappyjava.so similarity index 100% rename from src/main/resources/org/xerial/snappy/native/OpenBSD/amd64/libsnappyjava.so rename to src/main/resources/org/xerial/snappy/native/OpenBSD/x86_64/libsnappyjava.so diff --git a/src/main/resources/org/xerial/snappy/native/Windows/amd64/snappyjava.dll b/src/main/resources/org/xerial/snappy/native/Windows/x86_64/snappyjava.dll similarity index 100% rename from src/main/resources/org/xerial/snappy/native/Windows/amd64/snappyjava.dll rename to src/main/resources/org/xerial/snappy/native/Windows/x86_64/snappyjava.dll diff --git a/src/test/java/org/xerial/snappy/SnappyLoaderTest.java b/src/test/java/org/xerial/snappy/SnappyLoaderTest.java index 81ecd53..9bcc85f 100755 --- a/src/test/java/org/xerial/snappy/SnappyLoaderTest.java +++ b/src/test/java/org/xerial/snappy/SnappyLoaderTest.java @@ -24,23 +24,20 @@ //-------------------------------------- package org.xerial.snappy; -import static org.junit.Assert.*; - -import java.io.BufferedInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.lang.reflect.Method; -import java.net.URL; -import java.net.URLClassLoader; - import org.codehaus.plexus.classworlds.ClassWorld; import org.codehaus.plexus.classworlds.realm.ClassRealm; import org.junit.Test; import org.xerial.util.FileResource; import org.xerial.util.log.Logger; +import java.io.*; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.fail; + public class SnappyLoaderTest { private static Logger _logger = Logger.getLogger(SnappyLoaderTest.class);