changeset 2300:3ac6591eded8

Netx: allow jnlp classloaders to share native libraries 2010-06-29 Omair Majid <omajid@redhat.com> * netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java nativeDirectories: New variable. Contains a list of directories that contain native libraries. (getInstance): Tell other classloaders about the native directory. (getNativeDir): Add the new native directory to nativeDirectories. (addNativeDirectory): New function. (getNativeDirectories): New function. (findLibrary): Look in all the native directories for the native library.
author Omair Majid <omajid@redhat.com>
date Tue, 29 Jun 2010 15:48:10 -0400
parents a8117b0af51d
children aca7e8363025
files ChangeLog netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java
diffstat 2 files changed, 52 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue Jun 29 14:26:57 2010 -0400
+++ b/ChangeLog	Tue Jun 29 15:48:10 2010 -0400
@@ -1,3 +1,14 @@
+2010-06-29  Omair Majid <omajid@redhat.com>
+
+	* netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java
+	nativeDirectories: New variable. Contains a list of directories that
+	contain native libraries.
+	(getInstance): Tell other classloaders about the native directory.
+	(getNativeDir): Add the new native directory to nativeDirectories.
+	(addNativeDirectory): New function.
+	(getNativeDirectories): New function.
+	(findLibrary): Look in all the native directories for the native library.
+
 2010-06-29  Omair Majid <omajid@redhat.com>
 
 	* netx/net/sourceforge/jnlp/services/XSingleInstanceService.java
--- a/netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java	Tue Jun 29 14:26:57 2010 -0400
+++ b/netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java	Tue Jun 29 15:48:10 2010 -0400
@@ -32,6 +32,7 @@
 import java.security.Permissions;
 import java.security.PrivilegedAction;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.LinkedList;
@@ -83,6 +84,9 @@
     /** the directory for native code */
     private File nativeDir = null; // if set, some native code exists
 
+    /** a list of directories that contain native libraries */
+    private List<File> nativeDirectories = Collections.synchronizedList(new LinkedList<File>());
+
     /** security context */
     private AccessControlContext acc = AccessController.getContext();
 
@@ -237,18 +241,22 @@
 		        // loader for this unique key. Check.
 		        JNLPClassLoader extLoader = (JNLPClassLoader) urlToLoader.get(uniqueKey);
 
-		        if (extLoader != null) {
+		        if (extLoader != null && extLoader != loader) {
 		            for (URL u : loader.getURLs())
 		                extLoader.addURL(u);
+		            for (File nativeDirectory: loader.getNativeDirectories())
+		                extLoader.addNativeDirectory(nativeDirectory);
 
 		            loader = extLoader;
 		        }
 
                 // loader is now current + ext. But we also need to think of 
                 // the baseLoader
-		        if (baseLoader != null) {
+		        if (baseLoader != null && baseLoader != loader) {
                     for (URL u : loader.getURLs())
                         baseLoader.addURL(u);
+                    for (File nativeDirectory: loader.getNativeDirectories())
+                        baseLoader.addNativeDirectory(nativeDirectory);
 
                     loader = baseLoader;
                 } 
@@ -716,29 +724,47 @@
 
         if (!nativeDir.mkdirs()) 
             return null;
-        else
+        else {
+            // add this new native directory to the search path
+            addNativeDirectory(nativeDir);
             return nativeDir;
+        }
+    }
+
+    /**
+     * Adds the {@link File} to the search path of this {@link JNLPClassLoader}
+     * when trying to find a native library
+     */
+    protected void addNativeDirectory(File nativeDirectory) {
+        nativeDirectories.add(nativeDirectory);
+    }
+
+    /**
+     * Returns a list of all directories in the search path of the current classloader
+     * when it tires to find a native library.
+     * @return a list of directories in the search path for native libraries
+     */
+    protected List<File> getNativeDirectories() {
+        return nativeDirectories;
     }
 
     /**
      * Return the absolute path to the native library.
      */
     protected String findLibrary(String lib) {
-        if (nativeDir == null)
-            return null;
-
         String syslib = System.mapLibraryName(lib);
 
-        File target = new File(nativeDir, syslib);
-        if (target.exists())
-            return target.toString();
-        else {
-            String result = super.findLibrary(lib);
-            if (result != null)
-                return result;
+        for (File dir: getNativeDirectories()) {
+            File target = new File(dir, syslib);
+            if (target.exists())
+                return target.toString();
+        }
 
-            return findLibraryExt(lib);
-        }
+        String result = super.findLibrary(lib);
+        if (result != null)
+            return result;
+
+        return findLibraryExt(lib);
     }
 
     /**