exception.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "exception.h"
  19. #include "hdfs.h"
  20. #include "jni_helper.h"
  21. #include <inttypes.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #define EXCEPTION_INFO_LEN (sizeof(gExceptionInfo)/sizeof(gExceptionInfo[0]))
  26. struct ExceptionInfo {
  27. const char * const name;
  28. int noPrintFlag;
  29. int excErrno;
  30. };
  31. static const struct ExceptionInfo gExceptionInfo[] = {
  32. {
  33. .name = "java/io/FileNotFoundException",
  34. .noPrintFlag = NOPRINT_EXC_FILE_NOT_FOUND,
  35. .excErrno = ENOENT,
  36. },
  37. {
  38. .name = "org/apache/hadoop/security/AccessControlException",
  39. .noPrintFlag = NOPRINT_EXC_ACCESS_CONTROL,
  40. .excErrno = EACCES,
  41. },
  42. {
  43. .name = "org/apache/hadoop/fs/UnresolvedLinkException",
  44. .noPrintFlag = NOPRINT_EXC_UNRESOLVED_LINK,
  45. .excErrno = ENOLINK,
  46. },
  47. {
  48. .name = "org/apache/hadoop/fs/ParentNotDirectoryException",
  49. .noPrintFlag = NOPRINT_EXC_PARENT_NOT_DIRECTORY,
  50. .excErrno = ENOTDIR,
  51. },
  52. {
  53. .name = "java/lang/IllegalArgumentException",
  54. .noPrintFlag = NOPRINT_EXC_ILLEGAL_ARGUMENT,
  55. .excErrno = EINVAL,
  56. },
  57. {
  58. .name = "java/lang/OutOfMemoryError",
  59. .noPrintFlag = 0,
  60. .excErrno = ENOMEM,
  61. },
  62. };
  63. void getExceptionInfo(const char *excName, int noPrintFlags,
  64. int *excErrno, int *shouldPrint)
  65. {
  66. int i;
  67. for (i = 0; i < EXCEPTION_INFO_LEN; i++) {
  68. if (strstr(gExceptionInfo[i].name, excName)) {
  69. break;
  70. }
  71. }
  72. if (i < EXCEPTION_INFO_LEN) {
  73. *shouldPrint = !(gExceptionInfo[i].noPrintFlag & noPrintFlags);
  74. *excErrno = gExceptionInfo[i].excErrno;
  75. } else {
  76. *shouldPrint = 1;
  77. *excErrno = EINTERNAL;
  78. }
  79. }
  80. int printExceptionAndFreeV(JNIEnv *env, jthrowable exc, int noPrintFlags,
  81. const char *fmt, va_list ap)
  82. {
  83. int i, noPrint, excErrno;
  84. char *className = NULL;
  85. jstring jStr = NULL;
  86. jvalue jVal;
  87. jthrowable jthr;
  88. jthr = classNameOfObject(exc, env, &className);
  89. if (jthr) {
  90. fprintf(stderr, "PrintExceptionAndFree: error determining class name "
  91. "of exception.\n");
  92. className = strdup("(unknown)");
  93. destroyLocalReference(env, jthr);
  94. }
  95. for (i = 0; i < EXCEPTION_INFO_LEN; i++) {
  96. if (!strcmp(gExceptionInfo[i].name, className)) {
  97. break;
  98. }
  99. }
  100. if (i < EXCEPTION_INFO_LEN) {
  101. noPrint = (gExceptionInfo[i].noPrintFlag & noPrintFlags);
  102. excErrno = gExceptionInfo[i].excErrno;
  103. } else {
  104. noPrint = 0;
  105. excErrno = EINTERNAL;
  106. }
  107. if (!noPrint) {
  108. vfprintf(stderr, fmt, ap);
  109. fprintf(stderr, " error:\n");
  110. // We don't want to use ExceptionDescribe here, because that requires a
  111. // pending exception. Instead, use ExceptionUtils.
  112. jthr = invokeMethod(env, &jVal, STATIC, NULL,
  113. "org/apache/commons/lang/exception/ExceptionUtils",
  114. "getStackTrace", "(Ljava/lang/Throwable;)Ljava/lang/String;", exc);
  115. if (jthr) {
  116. fprintf(stderr, "(unable to get stack trace for %s exception: "
  117. "ExceptionUtils::getStackTrace error.)\n", className);
  118. destroyLocalReference(env, jthr);
  119. } else {
  120. jStr = jVal.l;
  121. const char *stackTrace = (*env)->GetStringUTFChars(env, jStr, NULL);
  122. if (!stackTrace) {
  123. fprintf(stderr, "(unable to get stack trace for %s exception: "
  124. "GetStringUTFChars error.)\n", className);
  125. } else {
  126. fprintf(stderr, "%s", stackTrace);
  127. (*env)->ReleaseStringUTFChars(env, jStr, stackTrace);
  128. }
  129. }
  130. }
  131. destroyLocalReference(env, jStr);
  132. destroyLocalReference(env, exc);
  133. free(className);
  134. return excErrno;
  135. }
  136. int printExceptionAndFree(JNIEnv *env, jthrowable exc, int noPrintFlags,
  137. const char *fmt, ...)
  138. {
  139. va_list ap;
  140. int ret;
  141. va_start(ap, fmt);
  142. ret = printExceptionAndFreeV(env, exc, noPrintFlags, fmt, ap);
  143. va_end(ap);
  144. return ret;
  145. }
  146. int printPendingExceptionAndFree(JNIEnv *env, int noPrintFlags,
  147. const char *fmt, ...)
  148. {
  149. va_list ap;
  150. int ret;
  151. jthrowable exc;
  152. exc = (*env)->ExceptionOccurred(env);
  153. if (!exc) {
  154. va_start(ap, fmt);
  155. vfprintf(stderr, fmt, ap);
  156. va_end(ap);
  157. fprintf(stderr, " error: (no exception)");
  158. ret = 0;
  159. } else {
  160. (*env)->ExceptionClear(env);
  161. va_start(ap, fmt);
  162. ret = printExceptionAndFreeV(env, exc, noPrintFlags, fmt, ap);
  163. va_end(ap);
  164. }
  165. return ret;
  166. }
  167. jthrowable getPendingExceptionAndClear(JNIEnv *env)
  168. {
  169. jthrowable jthr = (*env)->ExceptionOccurred(env);
  170. if (!jthr)
  171. return NULL;
  172. (*env)->ExceptionClear(env);
  173. return jthr;
  174. }
  175. jthrowable newRuntimeError(JNIEnv *env, const char *fmt, ...)
  176. {
  177. char buf[512];
  178. jobject out, exc;
  179. jstring jstr;
  180. va_list ap;
  181. va_start(ap, fmt);
  182. vsnprintf(buf, sizeof(buf), fmt, ap);
  183. va_end(ap);
  184. jstr = (*env)->NewStringUTF(env, buf);
  185. if (!jstr) {
  186. // We got an out of memory exception rather than a RuntimeException.
  187. // Too bad...
  188. return getPendingExceptionAndClear(env);
  189. }
  190. exc = constructNewObjectOfClass(env, &out, "RuntimeException",
  191. "(java/lang/String;)V", jstr);
  192. (*env)->DeleteLocalRef(env, jstr);
  193. // Again, we'll either get an out of memory exception or the
  194. // RuntimeException we wanted.
  195. return (exc) ? exc : out;
  196. }