jni_helper.h 7.8 KB

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