Called by JVM when it loads our library.
| 112 | |
| 113 | // Called by JVM when it loads our library. |
| 114 | JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* jvm, void* reserved) |
| 115 | { |
| 116 | // Grab the context ClassLoader of the current thread, if any. |
| 117 | JNIEnv* env; |
| 118 | if (jvm->GetEnv(JNIENV_CAST(&env), JNI_VERSION_1_2) != JNI_OK) { |
| 119 | return JNI_ERR; // JNI version not supported. |
| 120 | } |
| 121 | |
| 122 | // Find thread's context class loader. |
| 123 | jclass javaLangThread = env->FindClass("java/lang/Thread"); |
| 124 | assert(javaLangThread != nullptr); |
| 125 | |
| 126 | jclass javaLangClassLoader = env->FindClass("java/lang/ClassLoader"); |
| 127 | assert(javaLangClassLoader != nullptr); |
| 128 | |
| 129 | jmethodID currentThread = env->GetStaticMethodID( |
| 130 | javaLangThread, "currentThread", "()Ljava/lang/Thread;"); |
| 131 | assert(currentThread != nullptr); |
| 132 | |
| 133 | jmethodID getContextClassLoader = env->GetMethodID( |
| 134 | javaLangThread, "getContextClassLoader", "()Ljava/lang/ClassLoader;"); |
| 135 | assert(getContextClassLoader != nullptr); |
| 136 | |
| 137 | jobject thread = env->CallStaticObjectMethod(javaLangThread, currentThread); |
| 138 | assert(thread != nullptr); |
| 139 | |
| 140 | jobject classLoader = env->CallObjectMethod(thread, getContextClassLoader); |
| 141 | |
| 142 | if (classLoader != nullptr) { |
| 143 | mesosClassLoader = env->NewWeakGlobalRef(classLoader); |
| 144 | } |
| 145 | |
| 146 | // Set the 'loaded' property so we don't try and load it again. This |
| 147 | // is necessary because a native library can be loaded either with |
| 148 | // 'System.load' or 'System.loadLibrary' and while redundant calls |
| 149 | // to 'System.loadLibrary' will be ignored one call of each could |
| 150 | // cause an error. |
| 151 | jclass clazz = FindMesosClass(env, "org/apache/mesos/MesosNativeLibrary"); |
| 152 | jfieldID loaded = env->GetStaticFieldID(clazz, "loaded", "Z"); |
| 153 | env->SetStaticBooleanField(clazz, loaded, (jboolean) true); |
| 154 | |
| 155 | return JNI_VERSION_1_2; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | // Called by JVM when it unloads our library. |
nothing calls this directly
no test coverage detected