jthrowable newRuntimeError(JNIEnv *env, const char *fmt, ...) { char buf[512]; jobject out, exc; jstring jstr; va_list ap; va_start(ap, fmt); vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); jstr = (*env)->NewStringUTF(env, buf); if (!jstr) { // We got an out of memory exception rather than a RuntimeException. // Too bad... return getPendingExceptionAndClear(env); } exc = constructNewObjectOfClass(env, &out, "RuntimeException", "(java/lang/String;)V", jstr); (*env)->DeleteLocalRef(env, jstr); // Again, we'll either get an out of memory exception or the // RuntimeException we wanted. return (exc) ? exc : out; }
struct NativeMiniDfsCluster* nmdCreate(struct NativeMiniDfsConf *conf) { struct NativeMiniDfsCluster* cl = NULL; jobject bld = NULL, cobj = NULL, cluster = NULL; jvalue val; JNIEnv *env = getJNIEnv(); jthrowable jthr; jstring jconfStr = NULL; if (!env) { fprintf(stderr, "nmdCreate: unable to construct JNIEnv.\n"); return NULL; } cl = calloc(1, sizeof(struct NativeMiniDfsCluster)); if (!cl) { fprintf(stderr, "nmdCreate: OOM"); goto error; } jthr = constructNewObjectOfClass(env, &cobj, HADOOP_CONF, "()V"); if (jthr) { printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "nmdCreate: new Configuration"); goto error; } if (conf->webhdfsEnabled) { jthr = newJavaStr(env, DFS_WEBHDFS_ENABLED_KEY, &jconfStr); if (jthr) { printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "nmdCreate: new String"); goto error; } jthr = invokeMethod(env, NULL, INSTANCE, cobj, HADOOP_CONF, "setBoolean", "(Ljava/lang/String;Z)V", jconfStr, conf->webhdfsEnabled); if (jthr) { printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "nmdCreate: Configuration::setBoolean"); goto error; } } jthr = constructNewObjectOfClass(env, &bld, MINIDFS_CLUSTER_BUILDER, "(L"HADOOP_CONF";)V", cobj); if (jthr) { printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "nmdCreate: NativeMiniDfsCluster#Builder#Builder"); goto error; } jthr = invokeMethod(env, &val, INSTANCE, bld, MINIDFS_CLUSTER_BUILDER, "format", "(Z)L" MINIDFS_CLUSTER_BUILDER ";", conf->doFormat); if (jthr) { printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "nmdCreate: " "Builder::format"); goto error; } (*env)->DeleteLocalRef(env, val.l); if (conf->webhdfsEnabled) { jthr = invokeMethod(env, &val, INSTANCE, bld, MINIDFS_CLUSTER_BUILDER, "nameNodeHttpPort", "(I)L" MINIDFS_CLUSTER_BUILDER ";", conf->namenodeHttpPort); if (jthr) { printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "nmdCreate: " "Builder::nameNodeHttpPort"); goto error; } (*env)->DeleteLocalRef(env, val.l); } jthr = invokeMethod(env, &val, INSTANCE, bld, MINIDFS_CLUSTER_BUILDER, "build", "()L" MINIDFS_CLUSTER ";"); if (jthr) { printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "nmdCreate: Builder#build"); goto error; } cluster = val.l; cl->obj = (*env)->NewGlobalRef(env, val.l); if (!cl->obj) { printPendingExceptionAndFree(env, PRINT_EXC_ALL, "nmdCreate: NewGlobalRef"); goto error; } (*env)->DeleteLocalRef(env, cluster); (*env)->DeleteLocalRef(env, bld); (*env)->DeleteLocalRef(env, cobj); (*env)->DeleteLocalRef(env, jconfStr); return cl; error: (*env)->DeleteLocalRef(env, cluster); (*env)->DeleteLocalRef(env, bld); (*env)->DeleteLocalRef(env, cobj); (*env)->DeleteLocalRef(env, jconfStr); free(cl); return NULL; }
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; }