36 DCHECK_GT(max_local_ref, 0);
37 if (env->PushLocalFrame(max_local_ref) < 0) {
38 env->ExceptionClear();
39 return Status(
"failed to push frame");
46 jclass local_cl = env->FindClass(class_str);
47 jthrowable exc = env->ExceptionOccurred();
49 env->ExceptionClear();
52 env->DeleteLocalRef(local_cl);
58 jclass local_cl = env->FindClass(class_str);
61 reinterpret_cast<jobject*>(class_ref)));
62 env->DeleteLocalRef(local_cl);
68 *global_ref = env->NewGlobalRef(local_ref);
77 if (env == NULL)
return Status(
"Failed to get/create JVM");
79 jclass local_jni_util_cl = env->FindClass(
"com/cloudera/impala/common/JniUtil");
80 if (local_jni_util_cl == NULL) {
81 if (env->ExceptionOccurred()) env->ExceptionDescribe();
82 return Status(
"Failed to find JniUtil class.");
84 jni_util_cl_ =
reinterpret_cast<jclass
>(env->NewGlobalRef(local_jni_util_cl));
86 if (env->ExceptionOccurred()) env->ExceptionDescribe();
87 return Status(
"Failed to create global reference to JniUtil class.");
89 env->DeleteLocalRef(local_jni_util_cl);
90 if (env->ExceptionOccurred()) {
91 return Status(
"Failed to delete local reference to JniUtil class.");
95 jclass local_internal_exc_cl =
96 env->FindClass(
"com/cloudera/impala/common/InternalException");
97 if (local_internal_exc_cl == NULL) {
98 if (env->ExceptionOccurred()) env->ExceptionDescribe();
99 return Status(
"Failed to find JniUtil class.");
101 internal_exc_cl_ =
reinterpret_cast<jclass
>(env->NewGlobalRef(local_internal_exc_cl));
103 if (env->ExceptionOccurred()) env->ExceptionDescribe();
104 return Status(
"Failed to create global reference to JniUtil class.");
106 env->DeleteLocalRef(local_internal_exc_cl);
107 if (env->ExceptionOccurred()) {
108 return Status(
"Failed to delete local reference to JniUtil class.");
113 env->GetStaticMethodID(
jni_util_cl_,
"throwableToString",
114 "(Ljava/lang/Throwable;)Ljava/lang/String;");
116 if (env->ExceptionOccurred()) env->ExceptionDescribe();
117 return Status(
"Failed to find JniUtil.throwableToString method.");
122 env->GetStaticMethodID(
jni_util_cl_,
"throwableToStackTrace",
123 "(Ljava/lang/Throwable;)Ljava/lang/String;");
125 if (env->ExceptionOccurred()) env->ExceptionDescribe();
126 return Status(
"Failed to find JniUtil.throwableToFullStackTrace method.");
130 env->GetStaticMethodID(
jni_util_cl_,
"getJvmMetrics",
"([B)[B");
132 if (env->ExceptionOccurred()) env->ExceptionDescribe();
133 return Status(
"Failed to find JniUtil.getJvmMetrics method.");
143 hdfsFS fs = hdfsConnect(
"default", 0);
151 return Status(
"Failed to get/create JVM");
153 vector<jobject>::iterator it;
155 env->DeleteGlobalRef(*it);
162 jthrowable exc = (env)->ExceptionOccurred();
164 env->ExceptionClear();
166 jstring msg = (jstring) env->CallStaticObjectMethod(
jni_util_class(),
170 (
reinterpret_cast<const char*
>(env->GetStringUTFChars(msg, &is_copy)));
173 jstring stack = (jstring) env->CallStaticObjectMethod(
jni_util_class(),
175 const char* c_stack =
176 reinterpret_cast<const char*
>(env->GetStringUTFChars(stack, &is_copy));
177 VLOG(1) << string(c_stack);
180 env->ExceptionClear();
181 env->DeleteLocalRef(exc);
184 ss << prefix << error_msg;
189 TGetJvmMetricsResponse* result) {
195 (*descriptor->
method_id) = env->GetMethodID(jni_class,
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 jmethodID throwable_to_stack_trace_id()
static Status Init()
Find JniUtil class, and get JniUtil.throwableToString method id.
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 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.
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()
static Status GetJvmMetrics(const TGetJvmMetricsRequest &request, TGetJvmMetricsResponse *result)
static Status CallJniMethod(const jobject &obj, const jmethodID &method, const T &arg)
static jmethodID throwable_to_string_id_
JNIEnv * getJNIEnv(void)
C linkage for helper functions in hdfsJniHelper.h.