You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2012/12/11 01:01:09 UTC

svn commit: r1419919 - in /incubator/mesos/branches/0.10.x/src/java: generated/org/apache/mesos/MesosNativeLibrary.java.in jni/convert.cpp

Author: benh
Date: Tue Dec 11 00:01:09 2012
New Revision: 1419919

URL: http://svn.apache.org/viewvc?rev=1419919&view=rev
Log:
Fixs a bug when using nested/sandboxed class loaders in Java and
attempting to load the native library (contributed by Ben Mahler,
https://reviews.apache.org/r/7455).

Modified:
    incubator/mesos/branches/0.10.x/src/java/generated/org/apache/mesos/MesosNativeLibrary.java.in
    incubator/mesos/branches/0.10.x/src/java/jni/convert.cpp

Modified: incubator/mesos/branches/0.10.x/src/java/generated/org/apache/mesos/MesosNativeLibrary.java.in
URL: http://svn.apache.org/viewvc/incubator/mesos/branches/0.10.x/src/java/generated/org/apache/mesos/MesosNativeLibrary.java.in?rev=1419919&r1=1419918&r2=1419919&view=diff
==============================================================================
--- incubator/mesos/branches/0.10.x/src/java/generated/org/apache/mesos/MesosNativeLibrary.java.in (original)
+++ incubator/mesos/branches/0.10.x/src/java/generated/org/apache/mesos/MesosNativeLibrary.java.in Tue Dec 11 00:01:09 2012
@@ -25,31 +25,46 @@ public class MesosNativeLibrary {
    * System.loadLibrary}).
    */
   public static void load() {
-    // Our JNI library will actually set 'loaded' to true once it is
-    // loaded, that way the library can get loaded by a user via
-    // 'System.load' in the event that they want to specify an
-    // absolute path and we won't try and reload the library ourselves
-    // (which would probably fail because 'java.library.path' might
-    // not be set).
-    if (!loaded) {
-      final String MESOS_NATIVE_LIBRARY = System.getenv("MESOS_NATIVE_LIBRARY");
-      if (MESOS_NATIVE_LIBRARY != null) {
-        try {
-          System.load(MESOS_NATIVE_LIBRARY);
-        } catch (UnsatisfiedLinkError error) {
-          System.err.println("Failed to load native Mesos library at " +
-                             MESOS_NATIVE_LIBRARY);
-          throw error;
-        }
-      } else {
-        try {
-          System.loadLibrary("mesos");
-        } catch (UnsatisfiedLinkError error) {
-          System.err.println("Failed to load native Mesos library from " +
-                             System.getProperty("java.library.path"));
-          throw error;
+    // In some circumstances, such as when sandboxed class loaders are used,
+    // the current thread's context class loader will not be able to see
+    // MesosNativeLibrary (even when executing this code!).
+    // We therefore, temporarily swap the thread's context class loader with
+    // the class loader that loaded this class, for the duration of the native
+    // library load.
+    ClassLoader contextClassLoader =
+        Thread.currentThread().getContextClassLoader();
+    Thread.currentThread().setContextClassLoader(
+        MesosNativeLibrary.class.getClassLoader());
+
+    try {
+      // Our JNI library will actually set 'loaded' to true once it is
+      // loaded, that way the library can get loaded by a user via
+      // 'System.load' in the event that they want to specify an
+      // absolute path and we won't try and reload the library ourselves
+      // (which would probably fail because 'java.library.path' might
+      // not be set).
+      if (!loaded) {
+        final String MESOS_NATIVE_LIBRARY = System.getenv("MESOS_NATIVE_LIBRARY");
+        if (MESOS_NATIVE_LIBRARY != null) {
+          try {
+            System.load(MESOS_NATIVE_LIBRARY);
+          } catch (UnsatisfiedLinkError error) {
+            System.err.println("Failed to load native Mesos library at " +
+                MESOS_NATIVE_LIBRARY);
+            throw error;
+          }
+        } else {
+          try {
+            System.loadLibrary("mesos");
+          } catch (UnsatisfiedLinkError error) {
+            System.err.println("Failed to load native Mesos library from " +
+                System.getProperty("java.library.path"));
+            throw error;
+          }
         }
       }
+    } finally {
+      Thread.currentThread().setContextClassLoader(contextClassLoader);
     }
   }
 

Modified: incubator/mesos/branches/0.10.x/src/java/jni/convert.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/branches/0.10.x/src/java/jni/convert.cpp?rev=1419919&r1=1419918&r2=1419919&view=diff
==============================================================================
--- incubator/mesos/branches/0.10.x/src/java/jni/convert.cpp (original)
+++ incubator/mesos/branches/0.10.x/src/java/jni/convert.cpp Tue Dec 11 00:01:09 2012
@@ -94,6 +94,7 @@ jclass FindMesosClass(JNIEnv* env, const
                                               strClassName);
 
   if (env->ExceptionCheck()) {
+    env->ExceptionDescribe();
     fprintf(stderr, "ERROR: unable to load class '%s' from %p\n",
             className, mesosClassLoader);
     return NULL;