/* * Class: org.apache.activemq6_core_asyncio_impl_AsynchronousFileImpl * Method: openFile * Signature: (Ljava/lang/String;)I */ JNIEXPORT jint JNICALL Java_org_apache_activemq_core_libaio_Native_openFile (JNIEnv * env , jclass , jstring jstrFileName) { std::string fileName = convertJavaString(env, jstrFileName); return open(fileName.data(), O_RDWR | O_CREAT, 0666); }
/** * @return string containing the name of the exception (ie its class name). */ std::string JniException::retrieveExceptionName(JNIEnv * curEnv) { // then get its class jclass exceptionClass = curEnv->GetObjectClass(javaException); // get the Class class // we could also use curEnv->FindClass("Class"); jclass classClass = curEnv->GetObjectClass(exceptionClass); // get the getName method jmethodID getNameId = curEnv->GetMethodID(classClass, "getName", "()Ljava/lang/String;"); // call the getName function jstring javaName = (jstring) curEnv->CallObjectMethod(exceptionClass, getNameId); if (javaName == NULL) { return ""; } std::string res = convertJavaString(curEnv, javaName); // release java resources curEnv->DeleteLocalRef(exceptionClass); curEnv->DeleteLocalRef(classClass); curEnv->DeleteLocalRef(javaName); return res; }
/** * @return error message of the exception. */ std::string JniException::retrieveExceptionMessage(JNIEnv * curEnv) { // return the result of the getLocalizedMessage method // retrieve information from the exception. // get method id jmethodID getLocalizedMessageId = curEnv->GetMethodID(curEnv->GetObjectClass(javaException), "getLocalizedMessage", "()Ljava/lang/String;"); // call getLocalizedMessage jstring description = (jstring) curEnv->CallObjectMethod(javaException, getLocalizedMessageId); if (description == NULL) { return ""; } std::string res = convertJavaString(curEnv, description); // release java resources curEnv->DeleteLocalRef(description); return res; }
/** * @return full stack trace when the exception occurred. */ std::string JniException::retrieveStackTrace(JNIEnv * curEnv) { // return the result of the getStackTrace method // retrieve information from the exception. // get method id // getStackTrace returns an array of StackTraceElement jmethodID getStackTraceId = curEnv->GetMethodID(curEnv->GetObjectClass(javaException), "getStackTrace", "()[Ljava/lang/StackTraceElement;"); // call getStackTrace jobjectArray stackTrace = (jobjectArray) curEnv->CallObjectMethod(javaException, getStackTraceId); if (stackTrace == NULL) { return ""; } // get length of the array jsize stackTraceLength = curEnv->GetArrayLength(stackTrace); std::string res = ""; // get toString methodId of StackTraceElement class jclass stackTraceElementClass = curEnv->FindClass("java/lang/StackTraceElement"); jmethodID toStringId = curEnv->GetMethodID(stackTraceElementClass, "toString", "()Ljava/lang/String;"); for (jsize i = 0; i < stackTraceLength; i++) { // add the result of toString method of each element in the result jobject curStackTraceElement = curEnv->GetObjectArrayElement(stackTrace, i); // call to string on the object jstring stackElementString = (jstring) curEnv->CallObjectMethod(curStackTraceElement, toStringId); if (stackElementString == NULL) { curEnv->DeleteLocalRef(stackTraceElementClass); curEnv->DeleteLocalRef(stackTrace); curEnv->DeleteLocalRef(curStackTraceElement); return res; } // add a line to res res += " at " + convertJavaString(curEnv, stackElementString) + "\n"; curEnv->DeleteLocalRef(curStackTraceElement); curEnv->DeleteLocalRef(stackElementString); } // release java resources curEnv->DeleteLocalRef(stackTraceElementClass); curEnv->DeleteLocalRef(stackTrace); return res; }
/* * Class: org_jboss_jaio_libaioimpl_LibAIOController * Method: init * Signature: (Ljava/lang/String;Ljava/lang/Class;)J */ JNIEXPORT jobject JNICALL Java_org_apache_activemq_core_libaio_Native_init (JNIEnv * env, jclass, jclass controllerClazz, jstring jstrFileName, jint maxIO, jobject logger) { AIOController * controller = 0; try { std::string fileName = convertJavaString(env, jstrFileName); controller = new AIOController(fileName, (int) maxIO); controller->done = env->GetMethodID(controllerClazz,"callbackDone","(Lorg/apache/activemq/core/asyncio/AIOCallback;JLjava/nio/ByteBuffer;)V"); if (!controller->done) { throwException (env, -1, "can't get callbackDone method"); return 0; } controller->error = env->GetMethodID(controllerClazz, "callbackError", "(Lorg/apache/activemq/core/asyncio/AIOCallback;JLjava/nio/ByteBuffer;ILjava/lang/String;)V"); if (!controller->done) { throwException (env, -1, "can't get callbackError method"); return 0; } jclass loggerClass = env->GetObjectClass(logger); if (!(controller->loggerDebug = env->GetMethodID(loggerClass, "debug", "(Ljava/lang/Object;)V"))) return 0; if (!(controller->loggerWarn = env->GetMethodID(loggerClass, "warn", "(Ljava/lang/Object;)V"))) return 0; if (!(controller->loggerInfo = env->GetMethodID(loggerClass, "info", "(Ljava/lang/Object;)V"))) return 0; if (!(controller->loggerError = env->GetMethodID(loggerClass, "error", "(Ljava/lang/Object;)V"))) return 0; controller->logger = env->NewGlobalRef(logger); return env->NewDirectByteBuffer(controller, 0); } catch (AIOException& e){ if (controller != 0) { delete controller; } throwException(env, e.getErrorCode(), e.what()); return 0; } }