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/10/10 01:59:39 UTC
svn commit: r1396421 - in /incubator/mesos/trunk/src/java:
generated/org/apache/mesos/MesosNativeLibrary.java.in jni/convert.cpp
Author: benh
Date: Tue Oct 9 23:59:39 2012
New Revision: 1396421
URL: http://svn.apache.org/viewvc?rev=1396421&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/trunk/src/java/generated/org/apache/mesos/MesosNativeLibrary.java.in
incubator/mesos/trunk/src/java/jni/convert.cpp
Modified: incubator/mesos/trunk/src/java/generated/org/apache/mesos/MesosNativeLibrary.java.in
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/java/generated/org/apache/mesos/MesosNativeLibrary.java.in?rev=1396421&r1=1396420&r2=1396421&view=diff
==============================================================================
--- incubator/mesos/trunk/src/java/generated/org/apache/mesos/MesosNativeLibrary.java.in (original)
+++ incubator/mesos/trunk/src/java/generated/org/apache/mesos/MesosNativeLibrary.java.in Tue Oct 9 23:59:39 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/trunk/src/java/jni/convert.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/java/jni/convert.cpp?rev=1396421&r1=1396420&r2=1396421&view=diff
==============================================================================
--- incubator/mesos/trunk/src/java/jni/convert.cpp (original)
+++ incubator/mesos/trunk/src/java/jni/convert.cpp Tue Oct 9 23:59:39 2012
@@ -96,6 +96,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;