📄 jstaf.cpp
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2001 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************/#include "STAFOSTypes.h"#include <jni.h>#include <string.h>#include "com_ibm_staf_STAFHandle.h"#include "com_ibm_staf_STAFUtil.h"#include "STAF.h"// These variables reference strings which will be passed into the JVM. See// additional comments in the initJVMStrings() function below.const char *sJavaIntFieldType = 0;const char *sJavaConstructorMethod = 0;const char *sSTAFHandleHandleField = 0;const char *sSTAFResultClass = 0;const char *sSTAFResultConstructorMethodSig = 0;const char *sSTAFExceptionClass = 0;const char *sSTAFExceptionConstructorMethodSig = 0;const STAFString *sSTAFResultClassStringPtr = 0;#if defined(STAF_OS_NAME_HPUX) && !defined(__ia64)extern "C"{ void _main();}#endifint throwSTAFException(JNIEnv *env, unsigned int rc, const char *utfString = 0);jobject createResultObject(JNIEnv *env, unsigned int rc, const char *utfString = 0, const unsigned int resultLength = 0);static void initJVMStrings(){ // JVM string constants // // Note: All strings that are passed into the JVM via the JNI interfaces // must be sent in as "ASCII". The JNI will not accept strings in the // current encoding. They must use the UTF-8 encodings from 0x01 to // 0x7F. That is why we have all of the constants below. // // Note: This data must reside in this function. If it is placed at file // scope, then there are problems with the AIX runtime initializing // things in a non-desirable order. // JVM Field types static STAFString sJavaIntFieldTypeString = STAFString("I") + kUTF8_NULL; sJavaIntFieldType = sJavaIntFieldTypeString.buffer(); // Default Java Methods static STAFString sJavaConstructorMethodString = STAFString("<init>") + kUTF8_NULL; sJavaConstructorMethod = sJavaConstructorMethodString.buffer(); // STAFHandle class definitions static STAFString sSTAFHandleHandleFieldString = STAFString("handle") + kUTF8_NULL; sSTAFHandleHandleField = sSTAFHandleHandleFieldString.buffer(); // STAFResult class definitions static STAFString sSTAFResultClassString = STAFString("com/ibm/staf/STAFResult") + kUTF8_NULL; static STAFString sSTAFResultConstructorMethodSigString = STAFString("(ILjava/lang/String;)V") + kUTF8_NULL; sSTAFResultClass = sSTAFResultClassString.buffer(); sSTAFResultConstructorMethodSig = sSTAFResultConstructorMethodSigString.buffer(); sSTAFResultClassStringPtr = &sSTAFResultClassString; // STAFException class definitions static STAFString sSTAFExceptionClassString = STAFString("com/ibm/staf/STAFException") + kUTF8_NULL; static STAFString sSTAFExceptionConstructorMethodSigString = STAFString("(ILjava/lang/String;)V") + kUTF8_NULL; sSTAFExceptionClass = sSTAFExceptionClassString.buffer(); sSTAFExceptionConstructorMethodSig = sSTAFExceptionConstructorMethodSigString.buffer();}/* * Class: com_ibm_staf_STAFHandle * Method: initialize * Signature: ()V */JNIEXPORT void JNICALL Java_com_ibm_staf_STAFHandle_initialize (JNIEnv *, jclass){ #if defined(STAF_OS_NAME_HPUX) && !defined(__ia64) _main(); #endif initJVMStrings();}/* * Class: com_ibm_staf_STAFHandle * Method: STAFRegister * Signature: (Ljava/lang/String;)V */JNIEXPORT void JNICALL Java_com_ibm_staf_STAFHandle_STAFRegister (JNIEnv *env, jobject theObject, jstring theHandleName){ // Get the class for STAFHandle and the handle field ID jclass stafHandleClass = env->GetObjectClass(theObject); jfieldID handleFieldID = env->GetFieldID(stafHandleClass, sSTAFHandleHandleField, sJavaIntFieldType); if (handleFieldID == 0) { // We could not get the Field ID of STAFHandle.handle // The Java VM should already have thrown an exception, so we // just return return; } // Check if jstring is null because GetStringUTFChars core dumps if // pass it a null string. Throw an exception if the string is null. if (theHandleName == 0) { throwSTAFException(env, kSTAFInvalidValue, "Error - handleName string is null"); return; } const char *handleName = env->GetStringUTFChars(theHandleName, 0); if (handleName == 0) { // We could not get the UTF-8 version of handleName, so throw an // exception throwSTAFException(env, kSTAFInvalidValue, "Error getting UTF-8 handleName string"); return; } STAFHandle_t handle = 0; unsigned int rc = STAFRegisterUTF8((char *)handleName, &handle); env->ReleaseStringUTFChars(theHandleName, handleName); if (rc != 0) { // Error registering with STAF, so throw an exception throwSTAFException(env, rc); return; } env->SetIntField(theObject, handleFieldID, (jint)handle);}/* * Class: com_ibm_staf_STAFHandle * Method: STAFUnRegister * Signature: ()V */JNIEXPORT void JNICALL Java_com_ibm_staf_STAFHandle_STAFUnRegister (JNIEnv *env, jobject theObject){ // Get the class for STAFHandle and the handle field ID jclass stafHandleClass = env->GetObjectClass(theObject); jfieldID handleFieldID = env->GetFieldID(stafHandleClass, sSTAFHandleHandleField, sJavaIntFieldType); if (handleFieldID == 0) { // We could not get the Field ID of STAFHandle.handle // The Java VM should already have thrown an exception, so we // just return return; } unsigned int rc = STAFUnRegister(static_cast<STAFHandle_t>( env->GetIntField(theObject, handleFieldID))); if (rc != 0) { // Error unregistering with STAF, so throw an exception throwSTAFException(env, rc); }}/* * Class: com_ibm_staf_STAFHandle * Method: STAFSubmit * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_ibm_staf_STAFHandle_STAFSubmit (JNIEnv *env, jobject theObject, jint syncOpt, jstring where, jstring service, jstring request){ // Get the class for STAFHandle and the handle field ID jclass stafHandleClass = env->GetObjectClass(theObject); jfieldID handleFieldID = env->GetFieldID(stafHandleClass, sSTAFHandleHandleField, sJavaIntFieldType); if (handleFieldID == 0) { // We could not get the Field ID of STAFHandle.handle // The Java VM should already have thrown an exception, so we // just return return 0; } STAFHandle_t handle = static_cast<STAFHandle_t>(env->GetIntField(theObject, handleFieldID)); STAFSyncOption_t syncOption = static_cast<STAFSyncOption_t>(syncOpt); if (where == 0 || service == 0 || request == 0) { // Check if jstring is null because GetStringUTFChars core dumps if // pass it a null string. Throw an exception if the string is null. if (where == 0) { throwSTAFException(env, kSTAFInvalidValue, "Error - where string is null"); } else if (service == 0) { throwSTAFException(env, kSTAFInvalidValue, "Error - service string is null"); } else if (request == 0) { throwSTAFException(env, kSTAFInvalidValue, "Error - request string is null"); } return 0; } const char *whereUTF = env->GetStringUTFChars(where, 0); const char *serviceUTF = env->GetStringUTFChars(service, 0); const char *requestUTF = env->GetStringUTFChars(request, 0); if ((whereUTF == 0) || (serviceUTF == 0) || (requestUTF == 0)) { // First release any memory we got from the Java VM if (whereUTF != 0) env->ReleaseStringUTFChars(where, whereUTF); if (serviceUTF != 0) env->ReleaseStringUTFChars(service, serviceUTF); if (requestUTF != 0) env->ReleaseStringUTFChars(request, requestUTF); // Now throw an exception if (whereUTF != 0) { throwSTAFException(env, kSTAFInvalidValue, "Error getting UTF-8 where string"); } else if (serviceUTF != 0) { throwSTAFException(env, kSTAFInvalidValue, "Error getting UTF-8 service string"); } else { throwSTAFException(env, kSTAFInvalidValue, "Error getting UTF-8 request string"); } return 0; } char *result = 0; unsigned int resultLength = 0; unsigned int rc = STAFSubmit2UTF8(handle, syncOption, (char *)whereUTF, (char *)serviceUTF, (char *)requestUTF, strlen(requestUTF), &result, &resultLength); // Now free up the memory from the Java VM env->ReleaseStringUTFChars(where, whereUTF); env->ReleaseStringUTFChars(service, serviceUTF); env->ReleaseStringUTFChars(request, requestUTF); // Now, check results of submit call jstring javaResult = 0; if (rc != 0) throwSTAFException(env, rc, result); else { if (result != 0) { if (resultLength == 0) { javaResult = env->NewStringUTF(result); } else { // Convert the result string for Java to handle null chars STAFString newResult = STAFString(result, resultLength, STAFString::kUTF8); newResult = newResult.replace(kUTF8_NULL, kUTF8_NULL2); newResult += kUTF8_NULL; javaResult = env->NewStringUTF(const_cast<char *> (newResult.buffer())); } } else javaResult = env->NewStringUTF("\00"); if (javaResult == 0) { throwSTAFException(env, kSTAFInvalidValue, "Error creating result string"); } } if (result != 0) STAFFree(handle, result); return javaResult;}/* * Class: com_ibm_staf_STAFHandle * Method: STAFSubmit2 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)LCOM/IBM/ncsd/test/STAF/STAFResult; */JNIEXPORT jobject JNICALL Java_com_ibm_staf_STAFHandle_STAFSubmit2 (JNIEnv *env, jobject theObject, jint syncOpt, jstring where, jstring service, jstring request){ // Get the class for STAFHandle and the handle field ID jclass stafHandleClass = env->GetObjectClass(theObject); jfieldID handleFieldID = env->GetFieldID(stafHandleClass, sSTAFHandleHandleField, sJavaIntFieldType); if (handleFieldID == 0) { // We could not get the Field ID of STAFHandle.handle // The Java VM should already have thrown an exception, so we // just return return 0; } STAFHandle_t handle = static_cast<STAFHandle_t>(env->GetIntField(theObject, handleFieldID)); STAFSyncOption_t syncOption = static_cast<STAFSyncOption_t>(syncOpt); if (where == 0 || service == 0 || request == 0) { // Check if jstring is null because GetStringUTFChars core dumps if // pass it a null string. if (where == 0) { return createResultObject(env, kSTAFInvalidValue, "Error - where string is null"); } else if (service == 0) { return createResultObject(env, kSTAFInvalidValue, "Error - service string is null"); } else if (request == 0) { return createResultObject(env, kSTAFInvalidValue, "Error - request string is null"); } } const char *whereUTF = env->GetStringUTFChars(where, 0); const char *serviceUTF = env->GetStringUTFChars(service, 0); const char *requestUTF = env->GetStringUTFChars(request, 0); if ((whereUTF == 0) || (serviceUTF == 0) || (requestUTF == 0)) { // First release any memory we got from the Java VM if (whereUTF != 0) env->ReleaseStringUTFChars(where, whereUTF); if (serviceUTF != 0) env->ReleaseStringUTFChars(service, serviceUTF); if (requestUTF != 0) env->ReleaseStringUTFChars(request, requestUTF); // Now return the result object if (whereUTF != 0) { return createResultObject(env, kSTAFInvalidValue, "Error getting UTF-8 where string"); } else if (serviceUTF != 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -