jni_helper.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #ifndef LIBHDFS_JNI_HELPER_H
  19. #define LIBHDFS_JNI_HELPER_H
  20. #include <jni.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdarg.h>
  24. #include <errno.h>
  25. #define PATH_SEPARATOR ':'
  26. /** Denote the method we want to invoke as STATIC or INSTANCE */
  27. typedef enum {
  28. STATIC,
  29. INSTANCE
  30. } MethType;
  31. /**
  32. * Create a new malloc'ed C string from a Java string.
  33. *
  34. * @param env The JNI environment
  35. * @param jstr The Java string
  36. * @param out (out param) the malloc'ed C string
  37. *
  38. * @return NULL on success; the exception otherwise
  39. */
  40. jthrowable newCStr(JNIEnv *env, jstring jstr, char **out);
  41. /**
  42. * Create a new Java string from a C string.
  43. *
  44. * @param env The JNI environment
  45. * @param str The C string
  46. * @param out (out param) the java string
  47. *
  48. * @return NULL on success; the exception otherwise
  49. */
  50. jthrowable newJavaStr(JNIEnv *env, const char *str, jstring *out);
  51. /**
  52. * Helper function to destroy a local reference of java.lang.Object
  53. * @param env: The JNIEnv pointer.
  54. * @param jFile: The local reference of java.lang.Object object
  55. * @return None.
  56. */
  57. void destroyLocalReference(JNIEnv *env, jobject jObject);
  58. /** invokeMethod: Invoke a Static or Instance method.
  59. * className: Name of the class where the method can be found
  60. * methName: Name of the method
  61. * methSignature: the signature of the method "(arg-types)ret-type"
  62. * methType: The type of the method (STATIC or INSTANCE)
  63. * instObj: Required if the methType is INSTANCE. The object to invoke
  64. the method on.
  65. * env: The JNIEnv pointer
  66. * retval: The pointer to a union type which will contain the result of the
  67. method invocation, e.g. if the method returns an Object, retval will be
  68. set to that, if the method returns boolean, retval will be set to the
  69. value (JNI_TRUE or JNI_FALSE), etc.
  70. * exc: If the methods throws any exception, this will contain the reference
  71. * Arguments (the method arguments) must be passed after methSignature
  72. * RETURNS: -1 on error and 0 on success. If -1 is returned, exc will have
  73. a valid exception reference, and the result stored at retval is undefined.
  74. */
  75. jthrowable invokeMethod(JNIEnv *env, jvalue *retval, MethType methType,
  76. jobject instObj, const char *className, const char *methName,
  77. const char *methSignature, ...);
  78. jthrowable constructNewObjectOfClass(JNIEnv *env, jobject *out, const char *className,
  79. const char *ctorSignature, ...);
  80. jthrowable methodIdFromClass(const char *className, const char *methName,
  81. const char *methSignature, MethType methType,
  82. JNIEnv *env, jmethodID *out);
  83. jthrowable globalClassReference(const char *className, JNIEnv *env, jclass *out);
  84. /** classNameOfObject: Get an object's class name.
  85. * @param jobj: The object.
  86. * @param env: The JNIEnv pointer.
  87. * @param name: (out param) On success, will contain a string containing the
  88. * class name. This string must be freed by the caller.
  89. * @return NULL on success, or the exception
  90. */
  91. jthrowable classNameOfObject(jobject jobj, JNIEnv *env, char **name);
  92. /** getJNIEnv: A helper function to get the JNIEnv* for the given thread.
  93. * It gets this from the ThreadLocalState if it exists. If a ThreadLocalState
  94. * does not exist, one will be created.
  95. * If no JVM exists, then one will be created. JVM command line arguments
  96. * are obtained from the LIBHDFS_OPTS environment variable.
  97. * @param: None.
  98. * @return The JNIEnv* corresponding to the thread.
  99. * */
  100. JNIEnv* getJNIEnv(void);
  101. /**
  102. * Get the last exception root cause that happened in the context of the
  103. * current thread.
  104. *
  105. * The pointer returned by this function is guaranteed to be valid until
  106. * the next call to invokeMethod() by the current thread.
  107. * Users of this function should not free the pointer.
  108. *
  109. * @return The root cause as a C-string.
  110. */
  111. char* getLastTLSExceptionRootCause();
  112. /**
  113. * Get the last exception stack trace that happened in the context of the
  114. * current thread.
  115. *
  116. * The pointer returned by this function is guaranteed to be valid until
  117. * the next call to invokeMethod() by the current thread.
  118. * Users of this function should not free the pointer.
  119. *
  120. * @return The stack trace as a C-string.
  121. */
  122. char* getLastTLSExceptionStackTrace();
  123. /** setTLSExceptionStrings: Sets the 'rootCause' and 'stackTrace' in the
  124. * ThreadLocalState if one exists for the current thread.
  125. *
  126. * @param rootCause A string containing the root cause of an exception.
  127. * @param stackTrace A string containing the stack trace of an exception.
  128. * @return None.
  129. */
  130. void setTLSExceptionStrings(const char *rootCause, const char *stackTrace);
  131. /**
  132. * Figure out if a Java object is an instance of a particular class.
  133. *
  134. * @param env The Java environment.
  135. * @param obj The object to check.
  136. * @param name The class name to check.
  137. *
  138. * @return -1 if we failed to find the referenced class name.
  139. * 0 if the object is not of the given class.
  140. * 1 if the object is of the given class.
  141. */
  142. int javaObjectIsOfClass(JNIEnv *env, jobject obj, const char *name);
  143. /**
  144. * Set a value in a configuration object.
  145. *
  146. * @param env The JNI environment
  147. * @param jConfiguration The configuration object to modify
  148. * @param key The key to modify
  149. * @param value The value to set the key to
  150. *
  151. * @return NULL on success; exception otherwise
  152. */
  153. jthrowable hadoopConfSetStr(JNIEnv *env, jobject jConfiguration,
  154. const char *key, const char *value);
  155. /**
  156. * Fetch an instance of an Enum.
  157. *
  158. * @param env The JNI environment.
  159. * @param className The enum class name.
  160. * @param valueName The name of the enum value
  161. * @param out (out param) on success, a local reference to an
  162. * instance of the enum object. (Since Java enums are
  163. * singletones, this is also the only instance.)
  164. *
  165. * @return NULL on success; exception otherwise
  166. */
  167. jthrowable fetchEnumInstance(JNIEnv *env, const char *className,
  168. const char *valueName, jobject *out);
  169. #endif /*LIBHDFS_JNI_HELPER_H*/
  170. /**
  171. * vim: ts=4: sw=4: et:
  172. */