/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jitsi.util;

import com.sun.jna.*;
import java.io.*;
import java.util.regex.*;

/**
 * Implements Java Native Interface (JNI)-related facilities such as loading a
 * JNI library from a jar.
 *
 * @author Lyubomir Marinov
 */
public final class JNIUtils
{
    /**
     * The regular expression pattern which matches the file extension
     * &quot;dylib&quot; that is commonly used on Mac OS X for dynamic
     * libraries/shared objects.
     */
    private static final Pattern DYLIB_PATTERN = Pattern.compile("\\.dylib$");

    public static void loadLibrary(String libname, ClassLoader classLoader)
    {
        try
        {
            System.loadLibrary(libname);
        }
        catch (UnsatisfiedLinkError ulerr)
        {
            // Attempt to extract the library from the resources and load it that
            // way.
            libname = System.mapLibraryName(libname);
            if (Platform.isMac())
                libname = DYLIB_PATTERN.matcher(libname).replaceFirst(".jnilib");

            File embedded;

            try
            {
                embedded
                    = Native.extractFromResourcePath(
                            "/" + Platform.RESOURCE_PREFIX + "/" + libname,
                            classLoader);
            }
            catch (IOException ioex)
            {
                throw ulerr;
            }
            try
            {
                System.load(embedded.getAbsolutePath());
            }
            finally
            {
                // Native.isUnpacked(String) is (package) internal.
                if (embedded.getName().startsWith("jna"))
                {
                    // Native.deleteLibrary(String) is (package) internal.
                    if (!embedded.delete())
                        embedded.deleteOnExit();
                }
            }
        }
    }

    /**
     * Prevents the initialization of new <tt>JNIUtils</tt> instances.
     */
    private JNIUtils()
    {
    }
}

@@ -202,24 +202,109 @@
  java.io.*;%0A
+import java.lang.reflect.InvocationTargetException;%0Aimport java.lang.reflect.Method;%0A
 import java.
@@ -858,62 +858,1350 @@
     
-try%0A        %7B%0A            System.loadLibrary(libname);
+loadLibrary(libname, null, classLoader);%0A    %7D%0A%0A    public static void loadLibrary(String libname, Class clazz)%0A    %7B%0A        loadLibrary(libname, clazz, clazz.getClassLoader());%0A    %7D%0A%0A    private static void loadLibrary(String libname, Class clazz,%0A        ClassLoader classLoader)%0A    %7B%0A        try%0A        %7B%0A            if (clazz == null)%0A            %7B%0A                System.loadLibrary(libname);%0A                return;%0A            %7D%0A%0A            // Hack so that the native library is loaded into the ClassLoader%0A            // that called this method, and not into the ClassLoader where%0A            // this code resides. This is necessary for true OSGi environments.%0A            try%0A            %7B%0A                Method loadLibrary0 = Runtime%0A                    .getRuntime()%0A                    .getClass()%0A                    .getDeclaredMethod(%22loadLibrary0%22,%0A                        Class.class, String.class);%0A                loadLibrary0.setAccessible(true);%0A                loadLibrary0.invoke(Runtime.getRuntime(), clazz, libname);%0A            %7D%0A            catch (NoSuchMethodException%0A                %7C SecurityException%0A                %7C IllegalAccessException%0A                %7C IllegalArgumentException%0A                %7C InvocationTargetException e)%0A            %7B%0A                System.loadLibrary(libname);%0A            %7D
 %0A   

Add support for real OSGi in JNIUtils