hadoop/hadoop-dlopen-libjvm.patch
2014-05-27 13:12:24 -04:00

128 lines
4.9 KiB
Diff

diff --git a/hadoop-common-project/hadoop-common/src/CMakeLists.txt b/hadoop-common-project/hadoop-common/src/CMakeLists.txt
index dec63c4..de21bab 100644
--- a/hadoop-common-project/hadoop-common/src/CMakeLists.txt
+++ b/hadoop-common-project/hadoop-common/src/CMakeLists.txt
@@ -205,7 +205,6 @@ ENDIF()
target_link_dual_libraries(hadoop
${LIB_DL}
- ${JAVA_JVM_LIBRARY}
)
SET(LIBHADOOP_VERSION "1.0.0")
SET_TARGET_PROPERTIES(hadoop PROPERTIES
diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/CMakeLists.txt b/hadoop-hdfs-project/hadoop-hdfs/src/CMakeLists.txt
index 82d1a32..2151bb8 100644
--- a/hadoop-hdfs-project/hadoop-hdfs/src/CMakeLists.txt
+++ b/hadoop-hdfs-project/hadoop-hdfs/src/CMakeLists.txt
@@ -99,7 +99,6 @@ if (NEED_LINK_DL)
endif(NEED_LINK_DL)
target_link_dual_libraries(hdfs
- ${JAVA_JVM_LIBRARY}
${LIB_DL}
pthread
)
diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/native/fuse-dfs/CMakeLists.txt b/hadoop-hdfs-project/hadoop-hdfs/src/main/native/fuse-dfs/CMakeLists.txt
index dd3f1e6..68ba422 100644
--- a/hadoop-hdfs-project/hadoop-hdfs/src/main/native/fuse-dfs/CMakeLists.txt
+++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/native/fuse-dfs/CMakeLists.txt
@@ -68,7 +68,6 @@ IF(FUSE_FOUND)
)
target_link_libraries(fuse_dfs
${FUSE_LIBRARIES}
- ${JAVA_JVM_LIBRARY}
hdfs
m
pthread
diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/jni_helper.c b/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/jni_helper.c
index 878289f..62686b3 100644
--- a/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/jni_helper.c
+++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/jni_helper.c
@@ -20,6 +20,7 @@
#include "exception.h"
#include "jni_helper.h"
+#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
@@ -442,10 +443,44 @@ static JNIEnv* getGlobalJNIEnv(void)
jint rv = 0;
jint noVMs = 0;
jthrowable jthr;
+ void *jvmHandle = NULL;
+ jint JNICALL (*getCreatedJavaJVMsPtr)(JavaVM **, jsize, jsize *);
+ getCreatedJavaJVMsPtr = NULL;
+ jint JNICALL (*createJavaVMPtr)(JavaVM **, void **, void *);
+ createJavaVMPtr = NULL;
+ char *dlsym_error = NULL;
+
+ //Get JAVA_HOME to use appropriate libjvm
+ char *javaHome = getenv("JAVA_HOME");
+ if (javaHome == NULL) {
+ javaHome = "/usr/lib/jvm";
+ }
+
+ //Load the appropriate libjvm
+ char libjvmPath[strlen(javaHome)+35];
+ snprintf(libjvmPath, sizeof(libjvmPath), "%s/jre/lib/amd64/server/libjvm.so", javaHome);
+ jvmHandle = dlopen(libjvmPath, RTLD_NOW|RTLD_LOCAL);
+ if (jvmHandle == NULL) {
+ snprintf(libjvmPath, sizeof(libjvmPath), "%s/jre/lib/i386/server/libjvm.so", javaHome);
+ jvmHandle = dlopen(libjvmPath, RTLD_NOW|RTLD_LOCAL);
+ if (jvmHandle == NULL) {
+ fprintf(stderr, "Failed to load libjvm.so!\n");
+ return NULL;
+ }
+ }
- rv = JNI_GetCreatedJavaVMs(&(vmBuf[0]), vmBufLength, &noVMs);
+ //Load the JNI_GetCreatedJavaVMs function from the libjvm library
+ *(void **)(&getCreatedJavaJVMsPtr) = dlsym(jvmHandle, "JNI_GetCreatedJavaVMs");
+ dlsym_error = dlerror();
+ if (dlsym_error) {
+ fprintf(stderr, "Can not load symbol JNI_GetCreatedJavaVMs: %s\n", dlsym_error);
+ dlclose(jvmHandle);
+ return NULL;
+ }
+ rv = (*getCreatedJavaJVMsPtr)(&(vmBuf[0]), vmBufLength, &noVMs);
if (rv != 0) {
fprintf(stderr, "JNI_GetCreatedJavaVMs failed with error: %d\n", rv);
+ dlclose(jvmHandle);
return NULL;
}
@@ -454,6 +489,7 @@ static JNIEnv* getGlobalJNIEnv(void)
char *hadoopClassPath = getenv("CLASSPATH");
if (hadoopClassPath == NULL) {
fprintf(stderr, "Environment variable CLASSPATH not set!\n");
+ dlclose(jvmHandle);
return NULL;
}
char *hadoopClassPathVMArg = "-Djava.class.path=";
@@ -502,7 +538,15 @@ static JNIEnv* getGlobalJNIEnv(void)
vm_args.nOptions = noArgs;
vm_args.ignoreUnrecognized = 1;
- rv = JNI_CreateJavaVM(&vm, (void*)&env, &vm_args);
+ //Load the JNI_CreateJavaVM function from the libjvm library
+ *(void **)(&createJavaVMPtr) = dlsym(jvmHandle, "JNI_CreateJavaVM");
+ dlsym_error = dlerror();
+ if (dlsym_error) {
+ fprintf(stderr, "Can not load symbol JNI_CreateJavaVM: %s\n", dlsym_error);
+ dlclose(jvmHandle);
+ return NULL;
+ }
+ rv = (*createJavaVMPtr)(&vm, (void*)&env, &vm_args);
if (hadoopJvmArgs != NULL) {
free(hadoopJvmArgs);
@@ -512,6 +556,7 @@ static JNIEnv* getGlobalJNIEnv(void)
if (rv != 0) {
fprintf(stderr, "Call to JNI_CreateJavaVM failed "
"with error: %d\n", rv);
+ dlclose(jvmHandle);
return NULL;
}
jthr = invokeMethod(env, NULL, STATIC, NULL,