[120] | 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 | |
---|
| 19 | #ifndef LIBHDFS_JNI_HELPER_H |
---|
| 20 | #define LIBHDFS_JNI_HELPER_H |
---|
| 21 | |
---|
| 22 | #include <jni.h> |
---|
| 23 | #include <stdio.h> |
---|
| 24 | |
---|
| 25 | #include <stdlib.h> |
---|
| 26 | #include <stdarg.h> |
---|
| 27 | #include <search.h> |
---|
| 28 | #include <pthread.h> |
---|
| 29 | #include <errno.h> |
---|
| 30 | |
---|
| 31 | #define PATH_SEPARATOR ':' |
---|
| 32 | |
---|
| 33 | #define USER_CLASSPATH "/home/y/libexec/hadoop/conf:/home/y/libexec/hadoop/lib/hadoop-0.1.0.jar" |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | /** Denote the method we want to invoke as STATIC or INSTANCE */ |
---|
| 37 | typedef enum { |
---|
| 38 | STATIC, |
---|
| 39 | INSTANCE |
---|
| 40 | } MethType; |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | /** Used for returning an appropriate return value after invoking |
---|
| 44 | * a method |
---|
| 45 | */ |
---|
| 46 | typedef jvalue RetVal; |
---|
| 47 | |
---|
| 48 | /** Used for returning the exception after invoking a method */ |
---|
| 49 | typedef jthrowable Exc; |
---|
| 50 | |
---|
| 51 | /** invokeMethod: Invoke a Static or Instance method. |
---|
| 52 | * className: Name of the class where the method can be found |
---|
| 53 | * methName: Name of the method |
---|
| 54 | * methSignature: the signature of the method "(arg-types)ret-type" |
---|
| 55 | * methType: The type of the method (STATIC or INSTANCE) |
---|
| 56 | * instObj: Required if the methType is INSTANCE. The object to invoke |
---|
| 57 | the method on. |
---|
| 58 | * env: The JNIEnv pointer |
---|
| 59 | * retval: The pointer to a union type which will contain the result of the |
---|
| 60 | method invocation, e.g. if the method returns an Object, retval will be |
---|
| 61 | set to that, if the method returns boolean, retval will be set to the |
---|
| 62 | value (JNI_TRUE or JNI_FALSE), etc. |
---|
| 63 | * exc: If the methods throws any exception, this will contain the reference |
---|
| 64 | * Arguments (the method arguments) must be passed after methSignature |
---|
| 65 | * RETURNS: -1 on error and 0 on success. If -1 is returned, exc will have |
---|
| 66 | a valid exception reference, and the result stored at retval is undefined. |
---|
| 67 | */ |
---|
| 68 | int invokeMethod(JNIEnv *env, RetVal *retval, Exc *exc, MethType methType, |
---|
| 69 | jobject instObj, const char *className, const char *methName, |
---|
| 70 | const char *methSignature, ...); |
---|
| 71 | |
---|
| 72 | /** constructNewObjectOfClass: Invoke a constructor. |
---|
| 73 | * className: Name of the class |
---|
| 74 | * ctorSignature: the signature of the constructor "(arg-types)V" |
---|
| 75 | * env: The JNIEnv pointer |
---|
| 76 | * exc: If the ctor throws any exception, this will contain the reference |
---|
| 77 | * Arguments to the ctor must be passed after ctorSignature |
---|
| 78 | */ |
---|
| 79 | jobject constructNewObjectOfClass(JNIEnv *env, Exc *exc, const char *className, |
---|
| 80 | const char *ctorSignature, ...); |
---|
| 81 | |
---|
| 82 | jmethodID methodIdFromClass(const char *className, const char *methName, |
---|
| 83 | const char *methSignature, MethType methType, |
---|
| 84 | JNIEnv *env); |
---|
| 85 | |
---|
| 86 | jclass globalClassReference(const char *className, JNIEnv *env); |
---|
| 87 | |
---|
| 88 | /** classNameOfObject: Get an object's class name. |
---|
| 89 | * @param jobj: The object. |
---|
| 90 | * @param env: The JNIEnv pointer. |
---|
| 91 | * @return Returns a pointer to a string containing the class name. This string |
---|
| 92 | * must be freed by the caller. |
---|
| 93 | */ |
---|
| 94 | char *classNameOfObject(jobject jobj, JNIEnv *env); |
---|
| 95 | |
---|
| 96 | /** getJNIEnv: A helper function to get the JNIEnv* for the given thread. |
---|
| 97 | * If no JVM exists, then one will be created. JVM command line arguments |
---|
| 98 | * are obtained from the LIBHDFS_OPTS environment variable. |
---|
| 99 | * @param: None. |
---|
| 100 | * @return The JNIEnv* corresponding to the thread. |
---|
| 101 | * */ |
---|
| 102 | JNIEnv* getJNIEnv(void); |
---|
| 103 | |
---|
| 104 | jarray constructNewArrayString(JNIEnv *env, Exc *exc, const char **elements, int size) ; |
---|
| 105 | |
---|
| 106 | #endif /*LIBHDFS_JNI_HELPER_H*/ |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * vim: ts=4: sw=4: et: |
---|
| 110 | */ |
---|
| 111 | |
---|