1
0

jni_helper.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * If no JVM exists, then one will be created. JVM command line arguments
  94. * are obtained from the LIBHDFS_OPTS environment variable.
  95. * @param: None.
  96. * @return The JNIEnv* corresponding to the thread.
  97. * */
  98. JNIEnv* getJNIEnv(void);
  99. /**
  100. * Figure out if a Java object is an instance of a particular class.
  101. *
  102. * @param env The Java environment.
  103. * @param obj The object to check.
  104. * @param name The class name to check.
  105. *
  106. * @return -1 if we failed to find the referenced class name.
  107. * 0 if the object is not of the given class.
  108. * 1 if the object is of the given class.
  109. */
  110. int javaObjectIsOfClass(JNIEnv *env, jobject obj, const char *name);
  111. /**
  112. * Set a value in a configuration object.
  113. *
  114. * @param env The JNI environment
  115. * @param jConfiguration The configuration object to modify
  116. * @param key The key to modify
  117. * @param value The value to set the key to
  118. *
  119. * @return NULL on success; exception otherwise
  120. */
  121. jthrowable hadoopConfSetStr(JNIEnv *env, jobject jConfiguration,
  122. const char *key, const char *value);
  123. /**
  124. * Fetch an instance of an Enum.
  125. *
  126. * @param env The JNI environment.
  127. * @param className The enum class name.
  128. * @param valueName The name of the enum value
  129. * @param out (out param) on success, a local reference to an
  130. * instance of the enum object. (Since Java enums are
  131. * singletones, this is also the only instance.)
  132. *
  133. * @return NULL on success; exception otherwise
  134. */
  135. jthrowable fetchEnumInstance(JNIEnv *env, const char *className,
  136. const char *valueName, jobject *out);
  137. #endif /*LIBHDFS_JNI_HELPER_H*/
  138. /**
  139. * vim: ts=4: sw=4: et:
  140. */