-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathjni_helper.h
More file actions
254 lines (228 loc) · 8.46 KB
/
jni_helper.h
File metadata and controls
254 lines (228 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LIBHDFS_JNI_HELPER_H
#define LIBHDFS_JNI_HELPER_H
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#ifdef WIN32
#define PATH_SEPARATOR ';'
#define PATH_SEPARATOR_STR ";"
#else
#define PATH_SEPARATOR ':'
#define PATH_SEPARATOR_STR ":"
#endif
// #define _LIBHDFS_JNI_HELPER_DEBUGGING_ON_
#ifdef WIN32
#ifdef LIBHDFS_DLL_EXPORT
#define LIBHDFS_EXTERNAL __declspec(dllexport)
#elif LIBHDFS_DLL_IMPORT
#define LIBHDFS_EXTERNAL __declspec(dllimport)
#else
#define LIBHDFS_EXTERNAL
#endif
#else
#ifdef LIBHDFS_DLL_EXPORT
#define LIBHDFS_EXTERNAL __attribute__((visibility("default")))
#elif LIBHDFS_DLL_IMPORT
#define LIBHDFS_EXTERNAL __attribute__((visibility("default")))
#else
#define LIBHDFS_EXTERNAL
#endif
#endif
/** Denote the method we want to invoke as STATIC or INSTANCE */
typedef enum {
STATIC,
INSTANCE
} MethType;
/**
* Create a new malloc'ed C string from a Java string.
*
* @param env The JNI environment
* @param jstr The Java string
* @param out (out param) the malloc'ed C string
*
* @return NULL on success; the exception otherwise
*/
LIBHDFS_EXTERNAL
jthrowable newCStr(JNIEnv *env, jstring jstr, char **out);
/**
* Create a new Java string from a C string.
*
* @param env The JNI environment
* @param str The C string
* @param out (out param) the java string
*
* @return NULL on success; the exception otherwise
*/
LIBHDFS_EXTERNAL
jthrowable newJavaStr(JNIEnv *env, const char *str, jstring *out);
/**
* Helper function to destroy a local reference of java.lang.Object
* @param env: The JNIEnv pointer.
* @param jFile: The local reference of java.lang.Object object
* @return None.
*/
LIBHDFS_EXTERNAL
void destroyLocalReference(JNIEnv *env, jobject jObject);
/** invokeMethod: Invoke a Static or Instance method.
* className: Name of the class where the method can be found
* methName: Name of the method
* methSignature: the signature of the method "(arg-types)ret-type"
* methType: The type of the method (STATIC or INSTANCE)
* instObj: Required if the methType is INSTANCE. The object to invoke
the method on.
* env: The JNIEnv pointer
* retval: The pointer to a union type which will contain the result of the
method invocation, e.g. if the method returns an Object, retval will be
set to that, if the method returns boolean, retval will be set to the
value (JNI_TRUE or JNI_FALSE), etc.
* exc: If the methods throws any exception, this will contain the reference
* Arguments (the method arguments) must be passed after methSignature
* RETURNS: -1 on error and 0 on success. If -1 is returned, exc will have
a valid exception reference, and the result stored at retval is undefined.
*/
LIBHDFS_EXTERNAL
jthrowable invokeMethod(JNIEnv *env, jvalue *retval, MethType methType,
jobject instObj, const char *className, const char *methName,
const char *methSignature, ...);
LIBHDFS_EXTERNAL
jthrowable constructNewObjectOfClass(JNIEnv *env, jobject *out, const char *className,
const char *ctorSignature, ...);
LIBHDFS_EXTERNAL
jthrowable methodIdFromClass(const char *className, const char *methName,
const char *methSignature, MethType methType,
JNIEnv *env, jmethodID *out);
LIBHDFS_EXTERNAL
jthrowable globalClassReference(const char *className, JNIEnv *env, jclass *out);
/** classNameOfObject: Get an object's class name.
* @param jobj: The object.
* @param env: The JNIEnv pointer.
* @param name: (out param) On success, will contain a string containing the
* class name. This string must be freed by the caller.
* @return NULL on success, or the exception
*/
LIBHDFS_EXTERNAL
jthrowable classNameOfObject(jobject jobj, JNIEnv *env, char **name);
/** getJNIEnv: A helper function to get the JNIEnv* for the given thread.
*
* In regular mode: Gets JNIEnv from ThreadLocalState if it exists, otherwise
* creates one. If no JVM exists, creates one using LIBHDFS_OPTS environment variable.
*
* In no_jvm_invocation mode: Uses JavaVM set via setJavaVM(). If the current thread
* is already attached by the caller, returns JNIEnv directly. If not attached,
* attaches the thread and stores JNIEnv in ThreadLocalState for proper cleanup.
*
* @param: None.
* @return The JNIEnv* corresponding to the thread.
* */
LIBHDFS_EXTERNAL
JNIEnv* getJNIEnv(void);
/**
* Get the last exception root cause that happened in the context of the
* current thread.
*
* The pointer returned by this function is guaranteed to be valid until
* the next call to invokeMethod() by the current thread.
* Users of this function should not free the pointer.
*
* @return The root cause as a C-string.
*/
LIBHDFS_EXTERNAL
char* getLastTLSExceptionRootCause();
/**
* Get the last exception stack trace that happened in the context of the
* current thread.
*
* The pointer returned by this function is guaranteed to be valid until
* the next call to invokeMethod() by the current thread.
* Users of this function should not free the pointer.
*
* @return The stack trace as a C-string.
*/
LIBHDFS_EXTERNAL
char* getLastTLSExceptionStackTrace();
/** setTLSExceptionStrings: Sets the 'rootCause' and 'stackTrace' in the
* ThreadLocalState if one exists for the current thread.
*
* @param rootCause A string containing the root cause of an exception.
* @param stackTrace A string containing the stack trace of an exception.
* @return None.
*/
LIBHDFS_EXTERNAL
void setTLSExceptionStrings(const char *rootCause, const char *stackTrace);
#ifdef LIBHDFS_NO_JVM_INVOCATION
/** setJavaVM: Set the JavaVM for use in JNI context.
* This function should be called once at the beginning of the application
* to provide the JavaVM that was passed to the JNI library.
* The JavaVM will be cached and used to obtain JNIEnv for each thread.
*
* @param vm The JavaVM pointer from JNI context.
* @return 0 on success, -1 on error.
*/
LIBHDFS_EXTERNAL
int setJavaVM(void *vm);
#endif /* LIBHDFS_NO_JVM_INVOCATION */
/**
* Figure out if a Java object is an instance of a particular class.
*
* @param env The Java environment.
* @param obj The object to check.
* @param name The class name to check.
*
* @return -1 if we failed to find the referenced class name.
* 0 if the object is not of the given class.
* 1 if the object is of the given class.
*/
LIBHDFS_EXTERNAL
int javaObjectIsOfClass(JNIEnv *env, jobject obj, const char *name);
/**
* Set a value in a configuration object.
*
* @param env The JNI environment
* @param jConfiguration The configuration object to modify
* @param key The key to modify
* @param value The value to set the key to
*
* @return NULL on success; exception otherwise
*/
LIBHDFS_EXTERNAL
jthrowable hadoopConfSetStr(JNIEnv *env, jobject jConfiguration,
const char *key, const char *value);
/**
* Fetch an instance of an Enum.
*
* @param env The JNI environment.
* @param className The enum class name.
* @param valueName The name of the enum value
* @param out (out param) on success, a local reference to an
* instance of the enum object. (Since Java enums are
* singletones, this is also the only instance.)
*
* @return NULL on success; exception otherwise
*/
LIBHDFS_EXTERNAL
jthrowable fetchEnumInstance(JNIEnv *env, const char *className,
const char *valueName, jobject *out);
#undef LIBHDFS_EXTERNAL
#endif /*LIBHDFS_JNI_HELPER_H*/
/**
* vim: ts=4: sw=4: et:
*/