From 6bbdaa97d62358bdacd94f65d3e3e011d18a2654 Mon Sep 17 00:00:00 2001 From: Juan Narvaez Date: Tue, 19 Mar 2013 08:45:16 -0500 Subject: [PATCH] Patch for OSGi compatibility. --- pom.xml | 55 ++++++++++++++++++- .../xerial/snappy/SnappyBundleActivator.java | 24 ++++++-- .../java/org/xerial/snappy/SnappyLoader.java | 26 +++++++-- 3 files changed, 92 insertions(+), 13 deletions(-) diff --git a/pom.xml b/pom.xml index 3653d82..9e10f35 100755 --- a/pom.xml +++ b/pom.xml @@ -82,7 +82,6 @@ - org.apache.maven.plugins @@ -171,9 +170,10 @@ org.apache.felix maven-bundle-plugin - 2.3.4 + 2.3.7 true + META-INF ${project.groupId}.${project.artifactId} ${pproject.name} @@ -181,10 +181,57 @@ org.xerial.snappy.SnappyBundleActivator org.xerial.snappy org.osgi.framework;version="[1.5,2)" + + org/xerial/snappy/native/Windows/amd64/snappyjava.dll;selection-filter="(&(osgi.arch=x86_64)(osgi.os=win32))", + org/xerial/snappy/native/Windows/x86/snappyjava.dll;selection-filter="(&(osgi.arch=x86)(osgi.os=win32))", + org/xerial/snappy/native/Mac/i386/libsnappyjava.jnilib;selection-filter="(&(osgi.arch=x86)(osgi.os=macosx))", + org/xerial/snappy/native/Mac/x86_64/libsnappyjava.jnilib;selection-filter="(&(osgi.arch=x86_64)(osgi.os=macosx))", + org/xerial/snappy/native/Linux/amd64/libsnappyjava.so;selection-filter="(&(osgi.arch=x86_64)(osgi.os=linux))", + org/xerial/snappy/native/Linux/i386/libsnappyjava.so;selection-filter="(&(osgi.arch=x86)(osgi.os=linux))", + org/xerial/snappy/native/Linux/arm/libsnappyjava.so;selection-filter="(&(osgi.arch=arm)(osgi.os=linux))" + + + + + bundle-manifest + process-classes + + manifest + + + + + org.apache.maven.plugins + maven-jar-plugin + + + true + META-INF/MANIFEST.MF + + + + + + org.apache.maven.plugins + maven-clean-plugin + 2.4.1 + + false + + + META-INF + + MANIFEST.MF + + + + + + maven-assembly-plugin 2.2.1 @@ -205,6 +252,7 @@ + @@ -254,7 +302,6 @@ - junit @@ -285,5 +332,7 @@ jar test + + diff --git a/src/main/java/org/xerial/snappy/SnappyBundleActivator.java b/src/main/java/org/xerial/snappy/SnappyBundleActivator.java index 5495160..f36a4f5 100755 --- a/src/main/java/org/xerial/snappy/SnappyBundleActivator.java +++ b/src/main/java/org/xerial/snappy/SnappyBundleActivator.java @@ -24,6 +24,9 @@ //-------------------------------------- package org.xerial.snappy; +import java.util.jar.Manifest; + +import org.osgi.framework.Bundle; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; @@ -35,12 +38,23 @@ import org.osgi.framework.BundleContext; */ public class SnappyBundleActivator implements BundleActivator { - public void start(BundleContext context) throws Exception { - + /** + * Name of the Snappy native library + */ + public static final String LIBRARY_NAME = "snappyjava"; + + /** + * Make a call to {@link System#loadLibrary(String)} to load the native library which assumes + * that the library is available on the path based on this {@link Bundle}'s {@link Manifest}. + */ + public void start(BundleContext context) throws Exception + { + System.loadLibrary(System.mapLibraryName(LIBRARY_NAME)); + SnappyLoader.setApi(new SnappyNative()); } - public void stop(BundleContext context) throws Exception { - + public void stop(BundleContext context) throws Exception + { + SnappyLoader.setApi(null); } - } diff --git a/src/main/java/org/xerial/snappy/SnappyLoader.java b/src/main/java/org/xerial/snappy/SnappyLoader.java index 9ec625f..96ee629 100755 --- a/src/main/java/org/xerial/snappy/SnappyLoader.java +++ b/src/main/java/org/xerial/snappy/SnappyLoader.java @@ -24,7 +24,13 @@ //-------------------------------------- package org.xerial.snappy; -import java.io.*; +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; @@ -89,7 +95,17 @@ public class SnappyLoader private static boolean isLoaded = false; private static Object api = null; - + + /** + * Set the api instance. + * + * @param nativeCode + */ + static synchronized void setApi(Object nativeCode) + { + api = nativeCode; + } + /** * load system properties when configuration file of the name * {@link #SNAPPY_SYSTEM_PROPERTIES_FILE} is found @@ -200,8 +216,8 @@ public class SnappyLoader * * @return */ - static synchronized Object load() { - + static synchronized Object load() + { if (api != null) return api; @@ -216,7 +232,7 @@ public class SnappyLoader 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(); - api = nativeCode; + setApi(nativeCode); } catch (Exception e) { e.printStackTrace();