int printExceptionAndFreeV(JNIEnv *env, jthrowable exc, int noPrintFlags, const char *fmt, va_list ap) { int i, noPrint, excErrno; char *className = NULL; jstring jStr = NULL; jvalue jVal; jthrowable jthr; const char *stackTrace; jthr = classNameOfObject(exc, env, &className); if (jthr) { fprintf(stderr, "PrintExceptionAndFree: error determining class name " "of exception.\n"); className = strdup("(unknown)"); destroyLocalReference(env, jthr); } for (i = 0; i < EXCEPTION_INFO_LEN; i++) { if (!strcmp(gExceptionInfo[i].name, className)) { break; } } if (i < EXCEPTION_INFO_LEN) { noPrint = (gExceptionInfo[i].noPrintFlag & noPrintFlags); excErrno = gExceptionInfo[i].excErrno; } else { noPrint = 0; excErrno = EINTERNAL; } if (!noPrint) { vfprintf(stderr, fmt, ap); fprintf(stderr, " error:\n"); // We don't want to use ExceptionDescribe here, because that requires a // pending exception. Instead, use ExceptionUtils. jthr = invokeMethod(env, &jVal, STATIC, NULL, "org/apache/commons/lang/exception/ExceptionUtils", "getStackTrace", "(Ljava/lang/Throwable;)Ljava/lang/String;", exc); if (jthr) { fprintf(stderr, "(unable to get stack trace for %s exception: " "ExceptionUtils::getStackTrace error.)\n", className); destroyLocalReference(env, jthr); } else { jStr = jVal.l; stackTrace = (*env)->GetStringUTFChars(env, jStr, NULL); if (!stackTrace) { fprintf(stderr, "(unable to get stack trace for %s exception: " "GetStringUTFChars error.)\n", className); } else { fprintf(stderr, "%s", stackTrace); (*env)->ReleaseStringUTFChars(env, jStr, stackTrace); } } } destroyLocalReference(env, jStr); destroyLocalReference(env, exc); free(className); return excErrno; }
int hdfsGetConf(const char *&host, unsigned short &port, const char *&user, char *group[], int &group_size) { jobject jConfiguration = NULL; JNIEnv *env = 0; jthrowable jExc = NULL; int ret = 0; env = getJNIEnv(); if (env == NULL) { fprintf(stderr, "can't JNI Env\n"); return -1; } jConfiguration = constructNewObjectOfClass(env, &jExc, HADOOP_CONF, "()V"); if (jConfiguration == NULL) { fprintf(stderr, "Can't construct instance of class " "org.apache.hadoop.conf.Configuration\n"); if (jExc != NULL) { env->ExceptionDescribe(); } return -1; } jvalue jVal; jstring ugiStr = (env)->NewStringUTF(UGI_INFO); jstring fsStr = env->NewStringUTF(DEFAULT_FS); if (invokeMethod(env, &jVal, NULL, jConfiguration, HADOOP_CONF, "get", JMETHOD1(JPARAM(JAVA_STRING), JPARAM(JAVA_STRING)), ugiStr) != 0) { //set err return ret = -1; fprintf(stderr, "can't invoke method get ugi\n"); if (env->ExceptionOccurred()) { env->ExceptionDescribe(); } } //parse group and user info if (ret == 0) { if (jVal.l != NULL) { const char *ugi = (env)->GetStringUTFChars((jstring)jVal.l, NULL); if (ugi != NULL) { fprintf(stdout, "ugi string is %s\n", ugi); if (parseGroup(user, group, group_size, ugi)) { fprintf(stderr, "can't parse group info\n"); ret = -1; } env->ReleaseStringUTFChars((jstring)jVal.l, ugi); } } else { fprintf(stdout, "can't find gui=%s\n", UGI_INFO); } } if (ret == 0) { /* get host and port info */ if (invokeMethod(env, &jVal, NULL, jConfiguration, HADOOP_CONF, "get", JMETHOD1(JPARAM(JAVA_STRING), JPARAM(JAVA_STRING)), fsStr) != 0) { ret = -1; fprintf(stderr, "can't invoke method get default fs\n"); if (env->ExceptionOccurred()) { env->ExceptionDescribe(); } } else { if (jVal.l != NULL) { const char *str = (env)->GetStringUTFChars((jstring)jVal.l, NULL); if (str != NULL) { fprintf(stdout, "fs string is %s\n", str); if (parseHostAndPort(host, port, str)) { fprintf(stderr, "can't parse host and port info\n"); ret = -1; } env->ReleaseStringUTFChars((jstring)jVal.l, str); } } else { fprintf(stdout, "can't find gui=%s\n", UGI_INFO); } } } destroyLocalReference(env, ugiStr); destroyLocalReference(env, jConfiguration); return ret; }
/** * To retrieve the default configuration value for NameNode's hostName and port * TODO: This function currently is using JNI, * we need to do this without using JNI (HDFS-3917) * * @param bld The hdfsBuilder handle * @param port Used to get the default value for NameNode's port * @param nn Used to get the default value for NameNode's hostName * @return 0 for success and non-zero value for failure */ static int retrieveDefaults(const struct hdfsBuilder *bld, tPort *port, char **nn) { JNIEnv *env = 0; jobject jHDFSConf = NULL, jAddress = NULL; jstring jHostName = NULL; jvalue jVal; jthrowable jthr = NULL; int ret = 0; char buf[512]; env = getJNIEnv(); if (!env) { return EINTERNAL; } jthr = constructNewObjectOfClass(env, &jHDFSConf, HADOOP_HDFS_CONF, "()V"); if (jthr) { ret = printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "hdfsBuilderConnect(%s)", hdfsBuilderToStr(bld, buf, sizeof(buf))); goto done; } jthr = invokeMethod(env, &jVal, STATIC, NULL, HADOOP_NAMENODE, "getHttpAddress", "(Lorg/apache/hadoop/conf/Configuration;)Ljava/net/InetSocketAddress;", jHDFSConf); if (jthr) { ret = printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "hdfsBuilderConnect(%s)", hdfsBuilderToStr(bld, buf, sizeof(buf))); goto done; } jAddress = jVal.l; jthr = invokeMethod(env, &jVal, INSTANCE, jAddress, JAVA_INETSOCKETADDRESS, "getPort", "()I"); if (jthr) { ret = printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "hdfsBuilderConnect(%s)", hdfsBuilderToStr(bld, buf, sizeof(buf))); goto done; } *port = jVal.i; jthr = invokeMethod(env, &jVal, INSTANCE, jAddress, JAVA_INETSOCKETADDRESS, "getHostName", "()Ljava/lang/String;"); if (jthr) { ret = printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "hdfsBuilderConnect(%s)", hdfsBuilderToStr(bld, buf, sizeof(buf))); goto done; } jHostName = jVal.l; jthr = newCStr(env, jHostName, nn); if (jthr) { ret = printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "hdfsBuilderConnect(%s)", hdfsBuilderToStr(bld, buf, sizeof(buf))); goto done; } done: destroyLocalReference(env, jHDFSConf); destroyLocalReference(env, jAddress); destroyLocalReference(env, jHostName); return ret; }