jni_helper.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <search.h>
  25. #include <pthread.h>
  26. #include <errno.h>
  27. #define PATH_SEPARATOR ':'
  28. /** Denote the method we want to invoke as STATIC or INSTANCE */
  29. typedef enum {
  30. STATIC,
  31. INSTANCE
  32. } MethType;
  33. /**
  34. * Create a new malloc'ed C string from a Java string.
  35. *
  36. * @param env The JNI environment
  37. * @param jstr The Java string
  38. * @param out (out param) the malloc'ed C string
  39. *
  40. * @return NULL on success; the exception otherwise
  41. */
  42. jthrowable newCStr(JNIEnv *env, jstring jstr, char **out);
  43. /**
  44. * Create a new Java string from a C string.
  45. *
  46. * @param env The JNI environment
  47. * @param str The C string
  48. * @param out (out param) the java string
  49. *
  50. * @return NULL on success; the exception otherwise
  51. */
  52. jthrowable newJavaStr(JNIEnv *env, const char *str, jstring *out);
  53. /**
  54. * Helper function to destroy a local reference of java.lang.Object
  55. * @param env: The JNIEnv pointer.
  56. * @param jFile: The local reference of java.lang.Object object
  57. * @return None.
  58. */
  59. void destroyLocalReference(JNIEnv *env, jobject jObject);
  60. /** invokeMethod: Invoke a Static or Instance method.
  61. * className: Name of the class where the method can be found
  62. * methName: Name of the method
  63. * methSignature: the signature of the method "(arg-types)ret-type"
  64. * methType: The type of the method (STATIC or INSTANCE)
  65. * instObj: Required if the methType is INSTANCE. The object to invoke
  66. the method on.
  67. * env: The JNIEnv pointer
  68. * retval: The pointer to a union type which will contain the result of the
  69. method invocation, e.g. if the method returns an Object, retval will be
  70. set to that, if the method returns boolean, retval will be set to the
  71. value (JNI_TRUE or JNI_FALSE), etc.
  72. * exc: If the methods throws any exception, this will contain the reference
  73. * Arguments (the method arguments) must be passed after methSignature
  74. * RETURNS: -1 on error and 0 on success. If -1 is returned, exc will have
  75. a valid exception reference, and the result stored at retval is undefined.
  76. */
  77. jthrowable invokeMethod(JNIEnv *env, jvalue *retval, MethType methType,
  78. jobject instObj, const char *className, const char *methName,
  79. const char *methSignature, ...);
  80. jthrowable constructNewObjectOfClass(JNIEnv *env, jobject *out, const char *className,
  81. const char *ctorSignature, ...);
  82. jthrowable methodIdFromClass(const char *className, const char *methName,
  83. const char *methSignature, MethType methType,
  84. JNIEnv *env, jmethodID *out);
  85. jthrowable globalClassReference(const char *className, JNIEnv *env, jclass *out);
  86. /** classNameOfObject: Get an object's class name.
  87. * @param jobj: The object.
  88. * @param env: The JNIEnv pointer.
  89. * @param name: (out param) On success, will contain a string containing the
  90. * class name. This string must be freed by the caller.
  91. * @return NULL on success, or the exception
  92. */
  93. jthrowable classNameOfObject(jobject jobj, JNIEnv *env, char **name);
  94. /** getJNIEnv: A helper function to get the JNIEnv* for the given thread.
  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. * Figure out if a Java object is an instance of a particular class.
  103. *
  104. * @param env The Java environment.
  105. * @param obj The object to check.
  106. * @param name The class name to check.
  107. *
  108. * @return -1 if we failed to find the referenced class name.
  109. * 0 if the object is not of the given class.
  110. * 1 if the object is of the given class.
  111. */
  112. int javaObjectIsOfClass(JNIEnv *env, jobject obj, const char *name);
  113. /**
  114. * Set a value in a configuration object.
  115. *
  116. * @param env The JNI environment
  117. * @param jConfiguration The configuration object to modify
  118. * @param key The key to modify
  119. * @param value The value to set the key to
  120. *
  121. * @return NULL on success; exception otherwise
  122. */
  123. jthrowable hadoopConfSetStr(JNIEnv *env, jobject jConfiguration,
  124. const char *key, const char *value);
  125. /**
  126. * Fetch an instance of an Enum.
  127. *
  128. * @param env The JNI environment.
  129. * @param className The enum class name.
  130. * @param valueName The name of the enum value
  131. * @param out (out param) on success, a local reference to an
  132. * instance of the enum object. (Since Java enums are
  133. * singletones, this is also the only instance.)
  134. *
  135. * @return NULL on success; exception otherwise
  136. */
  137. jthrowable fetchEnumInstance(JNIEnv *env, const char *className,
  138. const char *valueName, jobject *out);
  139. #endif /*LIBHDFS_JNI_HELPER_H*/
  140. /**
  141. * vim: ts=4: sw=4: et:
  142. */