📄 stafjavaservicehelper.cpp
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2001, 2004 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************/#include "STAF.h"#include <jni.h>#include <vector>#include <map>#include "STAFJavaService.h"#include "com_ibm_staf_service_STAFServiceHelper.h"#include "STAFConnectionProvider.h"#include "STAFEventSem.h"#include "STAFServiceInterface.h"#include "STAFUtil.h"#include "STAFTrace.h"struct JavaServiceData{ STAFString fName; jmethodID fInitMethodID; jmethodID fAcceptRequestMethodID; jmethodID fTermMethodID; jobject fServiceObject; unsigned int fInterfaceLevel;};// Function prototypesSTAFRC_t HandleRequest(const STAFConnectionProvider *provider, STAFConnectionPtr &connection);void HandleServiceConstruct(STAFConnectionPtr &connection);void HandleServiceInit(STAFConnectionPtr &connection);void HandleServiceRequest(STAFConnectionPtr &connection);void HandleServiceTerm(STAFConnectionPtr &connection);#if defined(STAF_OS_NAME_HPUX) && !defined(__ia64)extern "C"{ void _main();}#endif// Global variablesstatic jobject gHelperObj;static JavaVM *gJVM;// XXX: static bool gExitJVM = false;static STAFEventSem gExitJVMSem;static STAFString sIPCName("IPCNAME");// These variables reference strings which will be passed into the JVM. See// additional comments in the initJVMStrings() function below.static const char *sJavaStringFieldType = 0;static const char *sJavaIntFieldType = 0;static const char *sJavaConstructorMethod = 0;static const char *sSTAFResultClass = 0;static const char *sSTAFResultRCField = 0;static const char *sSTAFResultResultField = 0;static const char *sServiceInitClass = 0;static const char *sServiceInitConstructorMethodSig = 0;static const char *sServiceRequestClass = 0;static const char *sServiceRequestConstructorSig = 0;static const char *sJVMNameField = 0;static const char *sLoadServiceMethod = 0;static const char *sLoadServiceMethodSig = 0;static const char *sInitServiceMethod = 0;static const char *sInitServiceMethodSig = 0;static const char *sCallServiceMethod = 0;static const char *sCallServiceMethodSig = 0;static const char *sTermServiceMethod = 0;static const char *sTermServiceMethodSig = 0;static const STAFString *sSTAFResultClassStringPtr = 0;static const STAFString *sServiceInitClassStringPtr = 0;static const STAFString *sServiceRequestClassStringPtr = 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 sJavaStringFieldTypeString = STAFString("Ljava/lang/String;") + kUTF8_NULL; static STAFString sJavaIntFieldTypeString = STAFString("I") + kUTF8_NULL; sJavaStringFieldType = sJavaStringFieldTypeString.buffer(); sJavaIntFieldType = sJavaIntFieldTypeString.buffer(); // Default Java Methods static STAFString sJavaConstructorMethodString = STAFString("<init>") + kUTF8_NULL; sJavaConstructorMethod = sJavaConstructorMethodString.buffer(); // STAFResult class definitions static STAFString sSTAFResultClassString = STAFString("com/ibm/staf/STAFResult") + kUTF8_NULL; static STAFString sSTAFResultRCFieldString = STAFString("rc") + kUTF8_NULL; static STAFString sSTAFResultResultFieldString = STAFString("result") + kUTF8_NULL; sSTAFResultClass = sSTAFResultClassString.buffer(); sSTAFResultRCField = sSTAFResultRCFieldString.buffer(); sSTAFResultResultField = sSTAFResultResultFieldString.buffer(); sSTAFResultClassStringPtr = &sSTAFResultClassString; // STAFServiceHelper.ServiceInit class definitions static STAFString sServiceInitClassString = STAFString("com/ibm/staf/service/STAFServiceHelper$ServiceInit") + kUTF8_NULL; static STAFString sServiceInitConstructorMethodSigString = STAFString("(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V") + kUTF8_NULL; sServiceInitClass = sServiceInitClassString.buffer(); sServiceInitConstructorMethodSig = sServiceInitConstructorMethodSigString.buffer(); sServiceInitClassStringPtr = &sServiceInitClassString; // STAFServiceHelper.ServiceRequest class definitions static STAFString sServiceRequestClassString = STAFString("com/ibm/staf/service/STAFServiceHelper$ServiceRequest") + kUTF8_NULL; static STAFString sServiceRequestConstructorSigString = STAFString("(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;" "Ljava/lang/String;IIZILjava/lang/String;I" "Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V") + kUTF8_NULL; sServiceRequestClass = sServiceRequestClassString.buffer(); sServiceRequestConstructorSig = sServiceRequestConstructorSigString.buffer(); sServiceRequestClassStringPtr = &sServiceRequestClassString; // STAFServiceHelper fields static STAFString sJVMNameFieldString = STAFString("fJVMName") + kUTF8_NULL; sJVMNameField = sJVMNameFieldString.buffer(); // STAFServiceHelper methods static STAFString sLoadServiceMethodString = STAFString("loadService") + kUTF8_NULL; static STAFString sLoadServiceMethodSigString = STAFString("(Ljava/lang/String;Ljava/lang/String;" "Ljava/lang/String;I)Lcom/ibm/staf/STAFResult;") + kUTF8_NULL; static STAFString sInitServiceMethodString = STAFString("initService") + kUTF8_NULL; static STAFString sInitServiceMethodSigString = STAFString("(Ljava/lang/String;" "Lcom/ibm/staf/service/STAFServiceHelper$ServiceInit;)" "Lcom/ibm/staf/STAFResult;") + kUTF8_NULL; static STAFString sCallServiceMethodString = STAFString("callService") + kUTF8_NULL; static STAFString sCallServiceMethodSigString = STAFString("(Ljava/lang/String;" "Lcom/ibm/staf/service/STAFServiceHelper$" "ServiceRequest;)Lcom/ibm/staf/STAFResult;") + kUTF8_NULL; static STAFString sTermServiceMethodString = STAFString("termService") + kUTF8_NULL; static STAFString sTermServiceMethodSigString = STAFString("(Ljava/lang/String;)Lcom/ibm/staf/STAFResult;") + kUTF8_NULL; sLoadServiceMethod = sLoadServiceMethodString.buffer(); sLoadServiceMethodSig = sLoadServiceMethodSigString.buffer(); sInitServiceMethod = sInitServiceMethodString.buffer(); sInitServiceMethodSig = sInitServiceMethodSigString.buffer(); sCallServiceMethod = sCallServiceMethodString.buffer(); sCallServiceMethodSig = sCallServiceMethodSigString.buffer(); sTermServiceMethod = sTermServiceMethodString.buffer(); sTermServiceMethodSig = sTermServiceMethodSigString.buffer();} /* * Class: com_ibm_staf_service_STAFServiceHelper * Method: initialize * Signature: ()V */JNIEXPORT void JNICALL Java_com_ibm_staf_service_STAFServiceHelper_initialize (JNIEnv *env, jclass){ #if defined(STAF_OS_NAME_HPUX) && !defined(__ia64) _main(); #endif initJVMStrings();}/* * Class: com_ibm_staf_service_STAFServiceHelper * Method: listen * Signature: ()V */JNIEXPORT void JNICALL Java_com_ibm_staf_service_STAFServiceHelper_listen (JNIEnv *env, jobject theHelperObject){ gHelperObj = env->NewGlobalRef(theHelperObject); env->GetJavaVM(&gJVM); // Write the PID for the JVM to the JVM log (and a separater line) cout << "*** JVM PID : " << STAFUtilGetPID() << endl; cout << "***************************************" << "***************************************" << endl; // Get the class for STAFServiceHelper and the fJVMName field ID jclass helperClass = env->GetObjectClass(theHelperObject); jfieldID jvmNameFieldID = env->GetFieldID(helperClass, sJVMNameField, sJavaStringFieldType); env->DeleteLocalRef(helperClass); if (jvmNameFieldID == 0) { // We could not get the Field ID of STAFServiceHelper.fJVMName // The Java VM should already have thrown an exception, so we // just return return; } jstring jvmNameObj = reinterpret_cast<jstring>( env->GetObjectField(theHelperObject, jvmNameFieldID)); const char *jvmNameUTF = env->GetStringUTFChars(jvmNameObj, 0); if (jvmNameUTF == 0) { // Pass back a string with the error } STAFConnectionProviderPtr connProv; unsigned int connProvStarted = 0; try { STAFString jvmName(jvmNameUTF, strlen(jvmNameUTF), STAFString::kUTF8); env->ReleaseStringUTFChars(jvmNameObj, jvmNameUTF); env->DeleteLocalRef(jvmNameObj); STAFString jvmIPCName = "JSTAF_"+ jvmName; STAFStringConst_t optionData[] = { sIPCName.getImpl(), jvmIPCName.getImpl() }; STAFConnectionProviderConstructInfoLevel1 constructInfo = { kSTAFConnectionProviderInbound, 1, optionData, &optionData[1] }; connProv = STAFConnectionProvider::createRefPtr( jvmIPCName, "STAFLIPC", &constructInfo, 1); connProv->start(HandleRequest); connProvStarted = 1; gExitJVMSem.wait(); connProv->stop(); } catch (STAFException &se) { se.trace("STAFServiceHelper.listen()"); if (connProvStarted) connProv->stop(); } catch (...) { STAFTrace::trace( kSTAFTraceError, "Caught unknown exception in STAFServiceHelper.listen()"); if (connProvStarted) connProv->stop(); } env->DeleteGlobalRef(gHelperObj);}STAFRC_t HandleRequest(const STAFConnectionProvider *provider, STAFConnectionPtr &connection){ unsigned int doShutdown = 0; try { unsigned int reqType = connection->readUInt(); switch (reqType) { case JAVA_SERVICE_JVMPING: { connection->writeUInt(kSTAFOk); connection->writeString(STAFString()); break; } case JAVA_SERVICE_LOAD: { HandleServiceConstruct(connection); break; } case JAVA_SERVICE_INIT: { HandleServiceInit(connection); break; } case JAVA_SERVICE_ACCEPT_REQUEST: { HandleServiceRequest(connection); break; } case JAVA_SERVICE_TERM: { HandleServiceTerm(connection); break; } case JAVA_SERVICE_JVMEXIT: { gExitJVMSem.post(); connection->writeUInt(kSTAFOk); connection->writeString(STAFString()); break; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -