hdfsJniHelper.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**
  2. * Copyright 2006 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <string.h>
  17. #include "hdfsJniHelper.h"
  18. static pthread_mutex_t hdfsHashMutex = PTHREAD_MUTEX_INITIALIZER;
  19. static volatile int hashTableInited = 0;
  20. #define LOCK_HASH_TABLE() pthread_mutex_lock(&hdfsHashMutex)
  21. #define UNLOCK_HASH_TABLE() pthread_mutex_unlock(&hdfsHashMutex)
  22. /** The Native return types that methods could return */
  23. #define VOID 'V'
  24. #define JOBJECT 'L'
  25. #define JARRAYOBJECT '['
  26. #define JBOOLEAN 'Z'
  27. #define JBYTE 'B'
  28. #define JCHAR 'C'
  29. #define JSHORT 'S'
  30. #define JINT 'I'
  31. #define JLONG 'J'
  32. #define JFLOAT 'F'
  33. #define JDOUBLE 'D'
  34. /**
  35. * Helpful macro to convert a pthread_t to a string
  36. */
  37. #define GET_threadID(threadID, key, keySize) \
  38. snprintf(key, keySize, "__hdfs_threadID__%u", (unsigned)(threadID));
  39. #define threadID_SIZE 32
  40. /**
  41. * MAX_HASH_TABLE_ELEM: The maximum no. of entries in the hashtable.
  42. * It's set to 4096 to account for (classNames + No. of threads)
  43. */
  44. #define MAX_HASH_TABLE_ELEM 4096
  45. #define CHECK_EXCEPTION_IN_METH_INVOC \
  46. if ((*env)->ExceptionCheck(env)) {\
  47. (*env)->ExceptionDescribe(env);\
  48. va_end(args);\
  49. return -1;\
  50. }\
  51. static void validateMethodType(MethType methType)
  52. {
  53. if (methType != STATIC && methType != INSTANCE) {
  54. fprintf(stderr, "Unimplemented method type\n");
  55. exit(1);
  56. }
  57. return;
  58. }
  59. static void hashTableInit(void)
  60. {
  61. if (!hashTableInited) {
  62. LOCK_HASH_TABLE();
  63. if (!hashTableInited) {
  64. if (hcreate(MAX_HASH_TABLE_ELEM) == 0) {
  65. fprintf(stderr, "error creating hashtable, <%d>: %s\n",
  66. errno, strerror(errno));
  67. exit(1);
  68. }
  69. hashTableInited = 1;
  70. }
  71. UNLOCK_HASH_TABLE();
  72. }
  73. }
  74. static void insertEntryIntoTable(const char *key, void *data)
  75. {
  76. ENTRY e, *ep;
  77. if (key == NULL || data == NULL) {
  78. return;
  79. }
  80. hashTableInit();
  81. e.data = data;
  82. e.key = (char*)key;
  83. LOCK_HASH_TABLE();
  84. ep = hsearch(e, ENTER);
  85. UNLOCK_HASH_TABLE();
  86. if (ep == NULL) {
  87. fprintf(stderr, "error adding key (%s) to hash table, <%d>: %s\n",
  88. key, errno, strerror(errno));
  89. exit(1);
  90. }
  91. }
  92. static void* searchEntryFromTable(const char *key)
  93. {
  94. ENTRY e,*ep;
  95. if (key == NULL) {
  96. return NULL;
  97. }
  98. hashTableInit();
  99. e.key = (char*)key;
  100. LOCK_HASH_TABLE();
  101. ep = hsearch(e, FIND);
  102. UNLOCK_HASH_TABLE();
  103. if (ep != NULL) {
  104. return ep->data;
  105. }
  106. return NULL;
  107. }
  108. int invokeMethod(JNIEnv *env, RetVal *retval, MethType methType,
  109. jobject instObj, const char *className,
  110. const char *methName, const char *methSignature, ...)
  111. {
  112. va_list args;
  113. jclass cls;
  114. jmethodID mid;
  115. const char *str;
  116. char returnType;
  117. validateMethodType(methType);
  118. cls = globalClassReference(className, env);
  119. mid = methodIdFromClass(className, methName, methSignature,
  120. methType, env);
  121. if (mid == NULL) {
  122. (*env)->ExceptionDescribe(env);
  123. exit(1);
  124. }
  125. str = methSignature;
  126. while (*str != ')') str++;
  127. str++;
  128. returnType = *str;
  129. va_start(args, methSignature);
  130. if (returnType == JOBJECT || returnType == JARRAYOBJECT) {
  131. jobject jobj = NULL;
  132. if (methType == STATIC) {
  133. jobj = (*env)->CallStaticObjectMethodV(env, cls, mid, args);
  134. }
  135. else if (methType == INSTANCE) {
  136. jobj = (*env)->CallObjectMethodV(env, instObj, mid, args);
  137. }
  138. CHECK_EXCEPTION_IN_METH_INVOC
  139. retval->l = jobj;
  140. }
  141. else if (returnType == VOID) {
  142. if (methType == STATIC) {
  143. (*env)->CallStaticVoidMethodV(env, cls, mid, args);
  144. }
  145. else if (methType == INSTANCE) {
  146. (*env)->CallVoidMethodV(env, instObj, mid, args);
  147. }
  148. CHECK_EXCEPTION_IN_METH_INVOC
  149. }
  150. else if (returnType == JBOOLEAN) {
  151. jboolean jbool = 0;
  152. if (methType == STATIC) {
  153. jbool = (*env)->CallStaticBooleanMethodV(env, cls, mid, args);
  154. }
  155. else if (methType == INSTANCE) {
  156. jbool = (*env)->CallBooleanMethodV(env, instObj, mid, args);
  157. }
  158. CHECK_EXCEPTION_IN_METH_INVOC
  159. retval->z = jbool;
  160. }
  161. else if (returnType == JLONG) {
  162. jlong jl = -1;
  163. if (methType == STATIC) {
  164. jl = (*env)->CallStaticLongMethodV(env, cls, mid, args);
  165. }
  166. else if (methType == INSTANCE) {
  167. jl = (*env)->CallLongMethodV(env, instObj, mid, args);
  168. }
  169. CHECK_EXCEPTION_IN_METH_INVOC
  170. retval->j = jl;
  171. }
  172. else if (returnType == JINT) {
  173. jint ji = -1;
  174. if (methType == STATIC) {
  175. ji = (*env)->CallStaticIntMethodV(env, cls, mid, args);
  176. }
  177. else if (methType == INSTANCE) {
  178. ji = (*env)->CallIntMethodV(env, instObj, mid, args);
  179. }
  180. CHECK_EXCEPTION_IN_METH_INVOC
  181. retval->i = ji;
  182. }
  183. va_end(args);
  184. return 0;
  185. }
  186. jobject constructNewObjectOfClass(JNIEnv *env, const char *className,
  187. const char *ctorSignature, ...)
  188. {
  189. va_list args;
  190. jclass cls;
  191. jmethodID mid;
  192. jobject jobj;
  193. cls = globalClassReference(className, env);
  194. mid = methodIdFromClass(className, "<init>", ctorSignature,
  195. INSTANCE, env);
  196. if (mid == NULL) {
  197. (*env)->ExceptionDescribe(env);
  198. exit(1);
  199. }
  200. va_start(args, ctorSignature);
  201. jobj = (*env)->NewObjectV(env, cls, mid, args);
  202. va_end(args);
  203. if ((*env)->ExceptionCheck(env)) {
  204. (*env)->ExceptionDescribe(env);
  205. }
  206. return jobj;
  207. }
  208. jmethodID methodIdFromClass(const char *className, const char *methName,
  209. const char *methSignature, MethType methType,
  210. JNIEnv *env)
  211. {
  212. jclass cls = globalClassReference(className, env);
  213. jmethodID mid = 0;
  214. validateMethodType(methType);
  215. if (methType == STATIC) {
  216. mid = (*env)->GetStaticMethodID(env, cls, methName, methSignature);
  217. }
  218. else if (methType == INSTANCE) {
  219. mid = (*env)->GetMethodID(env, cls, methName, methSignature);
  220. }
  221. return mid;
  222. }
  223. jclass globalClassReference(const char *className, JNIEnv *env)
  224. {
  225. jclass clsLocalRef;
  226. jclass cls = searchEntryFromTable(className);
  227. if (cls) {
  228. return cls;
  229. }
  230. clsLocalRef = (*env)->FindClass(env,className);
  231. if (clsLocalRef == NULL) {
  232. (*env)->ExceptionDescribe(env);
  233. exit(1);
  234. }
  235. cls = (*env)->NewGlobalRef(env, clsLocalRef);
  236. if (cls == NULL) {
  237. (*env)->ExceptionDescribe(env);
  238. exit(1);
  239. }
  240. (*env)->DeleteLocalRef(env, clsLocalRef);
  241. insertEntryIntoTable(className, cls);
  242. return cls;
  243. }
  244. /**
  245. * getJNIEnv: A helper function to get the JNIEnv* for the given thread.
  246. * @param: None.
  247. * @return The JNIEnv* corresponding to the thread.
  248. */
  249. JNIEnv* getJNIEnv(void)
  250. {
  251. char threadID[threadID_SIZE];
  252. const jsize vmBufLength = 1;
  253. JavaVM* vmBuf[vmBufLength];
  254. JNIEnv *env;
  255. jint rv = 0;
  256. jint noVMs = 0;
  257. //Get the threadID and stringize it
  258. GET_threadID(pthread_self(), threadID, sizeof(threadID));
  259. //See if you already have the JNIEnv* cached...
  260. env = (JNIEnv*)searchEntryFromTable(threadID);
  261. if (env != NULL) {
  262. return env;
  263. }
  264. //All right... some serious work required here!
  265. //1. Initialize the HashTable
  266. //2. LOCK!
  267. //3. Check if any JVMs have been created here
  268. // Yes: Use it (we should only have 1 VM)
  269. // No: Create the JVM
  270. //4. UNLOCK
  271. hashTableInit();
  272. LOCK_HASH_TABLE();
  273. rv = JNI_GetCreatedJavaVMs(&(vmBuf[0]), vmBufLength, &noVMs);
  274. if (rv != 0) {
  275. fprintf(stderr, "JNI_GetCreatedJavaVMs failed with error: %d\n", rv);
  276. exit(1);
  277. }
  278. if (noVMs == 0) {
  279. //Get the environment variables for initializing the JVM
  280. char *hadoopClassPath = getenv("CLASSPATH");
  281. if (hadoopClassPath == NULL) {
  282. fprintf(stderr, "Environment variable CLASSPATH not set!\n");
  283. exit(-1);
  284. }
  285. char *hadoopClassPathVMArg = "-Djava.class.path=";
  286. size_t optHadoopClassPathLen = strlen(hadoopClassPath) +
  287. strlen(hadoopClassPathVMArg) + 1;
  288. char *optHadoopClassPath = malloc(sizeof(char)*optHadoopClassPathLen);
  289. snprintf(optHadoopClassPath, optHadoopClassPathLen,
  290. "%s%s", hadoopClassPathVMArg, hadoopClassPath);
  291. //Create the VM
  292. JavaVMInitArgs vm_args;
  293. JavaVMOption options[1];
  294. JavaVM *vm;
  295. // User classes
  296. options[0].optionString = optHadoopClassPath;
  297. // Print JNI-related messages
  298. //options[2].optionString = "-verbose:jni";
  299. vm_args.version = JNI_VERSION_1_2;
  300. vm_args.options = options;
  301. vm_args.nOptions = 1;
  302. vm_args.ignoreUnrecognized = 1;
  303. rv = JNI_CreateJavaVM(&vm, (void*)&env, &vm_args);
  304. if (rv != 0) {
  305. fprintf(stderr, "Call to JNI_CreateJavaVM failed "
  306. "with error: %d\n", rv);
  307. exit(1);
  308. }
  309. free(optHadoopClassPath);
  310. }
  311. else {
  312. //Attach this thread to the VM
  313. JavaVM* vm = vmBuf[0];
  314. rv = (*vm)->AttachCurrentThread(vm, (void*)&env, 0);
  315. if (rv != 0) {
  316. fprintf(stderr, "Call to AttachCurrentThread "
  317. "failed with error: %d\n", rv);
  318. exit(1);
  319. }
  320. }
  321. //Save the threadID -> env mapping
  322. ENTRY e, *ep;
  323. e.key = threadID;
  324. e.data = (void*)(env);
  325. if ((ep = hsearch(e, ENTER)) == NULL) {
  326. fprintf(stderr, "Call to hsearch(ENTER) failed\n");
  327. exit(1);
  328. }
  329. UNLOCK_HASH_TABLE();
  330. return env;
  331. }