16 #ifndef IMPALA_UTIL_JNI_UTIL_H
17 #define IMPALA_UTIL_JNI_UTIL_H
24 #include "gen-cpp/Frontend_types.h"
26 #define THROW_IF_ERROR_WITH_LOGGING(stmt, env, adaptor) \
28 Status status = (stmt); \
30 (adaptor)->WriteErrorLog(); \
31 (adaptor)->WriteFileErrors(); \
32 (env)->ThrowNew((adaptor)->impala_exc_cl(), status.GetDetail().c_str()); \
37 #define THROW_IF_ERROR(stmt, env, impala_exc_cl) \
39 Status status = (stmt); \
41 (env)->ThrowNew((impala_exc_cl), status.GetDetail().c_str()); \
46 #define THROW_IF_ERROR_RET(stmt, env, impala_exc_cl, ret) \
48 Status status = (stmt); \
50 (env)->ThrowNew((impala_exc_cl), status.GetDetail().c_str()); \
55 #define THROW_IF_EXC(env, exc_class) \
57 jthrowable exc = (env)->ExceptionOccurred(); \
59 DCHECK((throwable_to_string_id_) != NULL); \
60 jstring stack = (jstring) env->CallStaticObjectMethod(JniUtil::jni_util_class(), \
61 (JniUtil::throwable_to_stack_trace_id()), exc); \
63 const char* c_stack = \
64 reinterpret_cast<const char*>((env)->GetStringUTFChars(stack, &is_copy)); \
65 (env)->ExceptionClear(); \
66 (env)->ThrowNew((exc_class), c_stack); \
71 #define RETURN_IF_EXC(env) \
73 jthrowable exc = (env)->ExceptionOccurred(); \
75 jstring stack = (jstring) env->CallStaticObjectMethod(JniUtil::jni_util_class(), \
76 (JniUtil::throwable_to_stack_trace_id()), exc); \
78 const char* c_stack = \
79 reinterpret_cast<const char*>((env)->GetStringUTFChars(stack, &is_copy)); \
80 VLOG(1) << string(c_stack); \
85 #define EXIT_IF_EXC(env) \
87 jthrowable exc = (env)->ExceptionOccurred(); \
89 jstring stack = (jstring) env->CallStaticObjectMethod(JniUtil::jni_util_class(), \
90 (JniUtil::throwable_to_stack_trace_id()), exc); \
92 const char* c_stack = \
93 reinterpret_cast<const char*>((env)->GetStringUTFChars(stack, &is_copy)); \
94 LOG(ERROR) << string(c_stack); \
99 #define RETURN_ERROR_IF_EXC(env) \
101 jthrowable exc = (env)->ExceptionOccurred(); \
102 if (exc != NULL) return JniUtil::GetJniExceptionMsg(env);\
105 #define EXIT_IF_JNIERROR(stmt) \
107 if ((stmt) != JNI_OK) { \
108 cerr << #stmt << " resulted in a jni error"; \
113 #define RETURN_IF_JNIERROR(stmt) \
115 if ((stmt) != JNI_OK) { \
117 out << #stmt << " resulted in a jni error"; \
118 return Status(out.str()); \
123 extern "C" {
extern JNIEnv*
getJNIEnv(
void); }
142 Status push(JNIEnv* env,
int max_local_ref=10);
185 static bool ClassExists(JNIEnv* env,
const char* class_str);
215 const std::string& prefix =
"");
220 TGetJvmMetricsResponse* result);
230 template <
typename T>
233 jbyteArray request_bytes;
237 jni_env->CallObjectMethod(obj, method, request_bytes);
242 template <
typename R>
247 jbyteArray result_bytes =
static_cast<jbyteArray
>(
248 jni_env->CallObjectMethod(obj, method));
254 template <
typename T,
typename R>
256 const T& arg, R* response) {
258 jbyteArray request_bytes;
262 jbyteArray result_bytes =
static_cast<jbyteArray
>(
263 jni_env->CallObjectMethod(obj, method, request_bytes));
269 template <
typename T>
271 const T& arg, std::string* response) {
273 jbyteArray request_bytes;
277 jstring java_response_string =
static_cast<jstring
>(
278 jni_env->CallObjectMethod(obj, method, request_bytes));
281 const char *str = jni_env->GetStringUTFChars(java_response_string, &is_copy);
284 jni_env->ReleaseStringUTFChars(java_response_string, str);
static jclass jni_util_cl_
static std::vector< jobject > global_refs_
const std::string signature
JNI-style method signature.
#define RETURN_IF_ERROR(stmt)
some generally useful macros
static Status LoadJniMethod(JNIEnv *jni_env, const jclass &jni_class, JniMethodDescriptor *descriptor)
static jclass internal_exc_class()
Global reference to InternalException class.
static jmethodID throwable_to_stack_trace_id()
static Status Init()
Find JniUtil class, and get JniUtil.throwableToString method id.
static Status CallJniMethod(const jobject &obj, const jmethodID &method, R *response)
static Status Cleanup()
Delete all global references: class members, and those stored in global_refs_.
static jmethodID throwable_to_stack_trace_id_
static Status LocalToGlobalRef(JNIEnv *env, jobject local_ref, jobject *global_ref)
static void InitLibhdfs()
Call this prior to any libhdfs calls.
static Status CallJniMethod(const jobject &obj, const jmethodID &method, const T &arg, std::string *response)
static jmethodID get_jvm_metrics_id_
static Status GetJniExceptionMsg(JNIEnv *env, bool log_stack=true, const std::string &prefix="")
Status push(JNIEnv *env, int max_local_ref=10)
Describes one method to look up in a Java object.
static Status CallJniMethod(const jobject &obj, const jmethodID &method, const T &arg, R *response)
const std::string name
Name of the method, case must match.
#define RETURN_ERROR_IF_EXC(env)
static jclass jni_util_class()
Global reference to java JniUtil class.
static bool ClassExists(JNIEnv *env, const char *class_str)
jmethodID * method_id
Handle to the method.
static Status GetGlobalClassRef(JNIEnv *env, const char *class_str, jclass *class_ref)
static jclass internal_exc_cl_
static jmethodID throwable_to_string_id()
Status DeserializeThriftMsg(JNIEnv *env, jbyteArray serialized_msg, T *deserialized_msg)
static Status GetJvmMetrics(const TGetJvmMetricsRequest &request, TGetJvmMetricsResponse *result)
static Status CallJniMethod(const jobject &obj, const jmethodID &method, const T &arg)
Status SerializeThriftMsg(JNIEnv *env, T *msg, jbyteArray *serialized_msg)
static jmethodID throwable_to_string_id_
JNIEnv * getJNIEnv(void)
C linkage for helper functions in hdfsJniHelper.h.