⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stafjavaservicehelper.cpp

📁 Software Testing Automation Framework (STAF)的开发代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
        env->ExceptionDescribe();        env->ExceptionClear();        connection->writeUInt(kSTAFJavaError);        connection->writeString("Error finding Java class: " +                                *sServiceInitClassStringPtr + " for "                                + serviceName + " in initService");        gJVM->DetachCurrentThread();        return;    }    jmethodID constructor = env->GetMethodID(serviceInitClass,                                             sJavaConstructorMethod,                                             sServiceInitConstructorMethodSig);    if (constructor == 0)    {        STAFTrace::trace(            kSTAFTraceError, "Error initializing service " + serviceName +             ": Error loading constructor for Java class " +            *sServiceInitClassStringPtr);        env->ExceptionDescribe();        env->ExceptionClear();        connection->writeUInt(kSTAFJavaError);        connection->writeString("Error loading constructor for Java class " +                                *sServiceInitClassStringPtr + " for " +                                serviceName);        gJVM->DetachCurrentThread();        return;    }            jobject serviceInitObject = env->NewObject(serviceInitClass, constructor,                                               javaServiceName, javaParms,                                               javaWriteLocation);    if (serviceInitObject == 0)    {        STAFTrace::trace(            kSTAFTraceError, "Error initializing service " + serviceName +             ": Error creating Java object of type " +            *sServiceInitClassStringPtr);        env->ExceptionDescribe();        env->ExceptionClear();        connection->writeUInt(kSTAFJavaError);        connection->writeString("Error creating Java object of type " +                                *sServiceInitClassStringPtr + " for " +                                serviceName);        gJVM->DetachCurrentThread();        return;    }    // Call the initService() method    jobject javaResult = env->CallObjectMethod(gHelperObj, initService,                                               javaServiceName,                                               serviceInitObject);    if (javaResult == 0)    {        STAFTrace::trace(            kSTAFTraceError, "Error initializing service " + serviceName +             ": Error calling the initService method");        env->ExceptionDescribe();        env->ExceptionClear();        connection->writeUInt(kSTAFJavaError);        connection->writeString("Error calling the initService method for " +                                serviceName);        gJVM->DetachCurrentThread();        return;    }    // Get the field IDs of the STAFResult class    jclass resultClass = env->FindClass(sSTAFResultClass);    if (resultClass == 0)    {        STAFTrace::trace(            kSTAFTraceError, "Error initializing service " + serviceName +             ": Error finding Java class " + *sSTAFResultClassStringPtr);        env->ExceptionDescribe();        env->ExceptionClear();        connection->writeUInt(kSTAFJavaError);        connection->writeString("Error finding Java class " +                                *sSTAFResultClassStringPtr +                                " in initService for " + serviceName);        gJVM->DetachCurrentThread();        return;    }    jfieldID resultRCID = env->GetFieldID(resultClass, sSTAFResultRCField,                                          sJavaIntFieldType);    if (resultRCID == 0)    {        STAFTrace::trace(            kSTAFTraceError, "Error initializing service " + serviceName +             ": Error getting rc field of Java class " +            *sSTAFResultClassStringPtr);        env->ExceptionDescribe();        env->ExceptionClear();        connection->writeUInt(kSTAFJavaError);        connection->writeString("Error getting rc field of Java class " +                                *sSTAFResultClassStringPtr +                                " in initService for " + serviceName);        gJVM->DetachCurrentThread();        return;    }    jfieldID resultStringID = env->GetFieldID(resultClass,                                              sSTAFResultResultField,                                              sJavaStringFieldType);    if (resultStringID == 0)    {        STAFTrace::trace(            kSTAFTraceError, "Error initializing service " + serviceName +             ": Error getting result field of Java class " +            *sSTAFResultClassStringPtr);        env->ExceptionDescribe();        env->ExceptionClear();        connection->writeUInt(kSTAFJavaError);        connection->writeString("Error getting result field of Java class " +                                *sSTAFResultClassStringPtr +                                " in initService for " + serviceName);        gJVM->DetachCurrentThread();        return;    }    // Get the return code and result string from the STAFResult object    jint rc = env->GetIntField(javaResult, resultRCID);    jstring javaResultString = (jstring)env->GetObjectField(javaResult,                                                            resultStringID);    const char *utfResultString =               env->GetStringUTFChars(javaResultString, 0);    if (utfResultString == 0)    {        STAFTrace::trace(            kSTAFTraceError, "Error initializing service " + serviceName +             ": Error getting UTF-8 result string");        env->ExceptionDescribe();        env->ExceptionClear();        connection->writeUInt(kSTAFJavaError);        connection->writeString("Error getting UTF-8 result string in "                                "initService for " + serviceName);        gJVM->DetachCurrentThread();        return;    }    STAFString resultString(utfResultString,                            env->GetStringUTFLength(javaResultString),                            STAFString::kUTF8);    env->ReleaseStringUTFChars(javaResultString, utfResultString);    connection->writeUInt(rc);    connection->writeString(resultString);    // Detach the current thread    gJVM->DetachCurrentThread();}void HandleServiceRequest(STAFConnectionPtr &connection){    // Note: The request type has already been read    unsigned int totalLength = connection->readUInt();    STAFBuffer<char> buffer(new char[totalLength], STAFBuffer<char>::INIT,                            STAFBuffer<char>::ARRAY);    unsigned int *uintBuffer = reinterpret_cast<unsigned int *>((char *)buffer);    // IMPORTANT:  Increase the numFields value if add a field to the    //             ServiceRequest class for a new STAFServiceInterfaceLevel.    //             Note:  This value is 2 less than the numFields value in    //                    STAFJavaService.cpp.    unsigned int numFields = 14;  // # of fields in buffer    // uintBuffer[0] = Service name length    // uintBuffer[1] = Handle    // uintBuffer[2] = Trust level    // uintBuffer[3] = Machine length    // uintBuffer[4] = Machine nickname length    // uintBuffer[5] = Handle name length    // uintBuffer[6] = Request length    // uintBuffer[7] = Diagnostics Enabled Flag    // uintBuffer[8] = Request Number    // uintBuffer[9] = User length    // uintBuffer[10] = Endpoint length    // uintBuffer[11] = STAF Instance UUID length    // uintBuffer[12] = Is Local Request Flag    // uintBuffer[13] = Physical Identifier length    connection->read(buffer, totalLength);        char *serviceNameBuffer = buffer + (numFields * sizeof(unsigned int));    char *machineBuffer = serviceNameBuffer + uintBuffer[0];    char *machineNicknameBuffer = machineBuffer +  uintBuffer[3];    char *handleNameBuffer = machineNicknameBuffer + uintBuffer[4];    char *requestBuffer = handleNameBuffer + uintBuffer[5];    char *userBuffer = requestBuffer + uintBuffer[6];    char *endpointBuffer = userBuffer + uintBuffer[9];    char *stafInstanceUUIDBuffer = endpointBuffer + uintBuffer[10];    char *physicalInterfaceIDBuffer = stafInstanceUUIDBuffer + uintBuffer[11];    if (serviceNameBuffer == 0 || machineBuffer == 0 ||        machineNicknameBuffer == 0 || handleNameBuffer == 0 ||        requestBuffer == 0 || userBuffer == 0 ||        endpointBuffer == 0 || stafInstanceUUIDBuffer == 0 ||        physicalInterfaceIDBuffer == 0)    {        STAFTrace::trace(            kSTAFTraceError, "Memory allocation failure in "            "JSTAFSH.HandleServiceRequest()");        connection->writeUInt(kSTAFJavaError);        connection->writeString("Memory allocation failure in "                                "JSTAFSH.HandleServiceRequest");        return;    }    STAFString serviceName(serviceNameBuffer, uintBuffer[0], STAFString::kUTF8);    unsigned int handle = uintBuffer[1];    unsigned int trustLevel = uintBuffer[2];    STAFString machine(machineBuffer, uintBuffer[3], STAFString::kUTF8);    STAFString machineNickname(machineNicknameBuffer, uintBuffer[4],                               STAFString::kUTF8);    STAFString handleName(handleNameBuffer, uintBuffer[5], STAFString::kUTF8);    STAFString request(requestBuffer, uintBuffer[6], STAFString::kUTF8);    unsigned int diagEnabled = uintBuffer[7];        unsigned int requestNumber = uintBuffer[8];    STAFString user(userBuffer, uintBuffer[9], STAFString::kUTF8);    STAFString endpoint(endpointBuffer, uintBuffer[10], STAFString::kUTF8);    STAFString stafInstanceUUID(stafInstanceUUIDBuffer, uintBuffer[11],                                STAFString::kUTF8);    unsigned int isLocalRequest = uintBuffer[12];    STAFString physicalInterfaceID(physicalInterfaceIDBuffer, uintBuffer[13],                                   STAFString::kUTF8);        /* Debug:    cout << "Total length         : " << totalLength << endl         << "Service name         : " << serviceName << endl         << "Machine              : " << machine << endl         << "Machine nickname     : " << machineNickname << endl         << "Handle name length   : " << uintBuffer[5] << endl         << "Handle name          : " << handleName << endl         << "Handle               : " << handle << endl         << "Trust level          : " << trustLevel << endl         << "Is Local Request     : " << isLocalRequest << endl         << "Request length       : " << uintBuffer[6] << endl         << "Request              : " << request << endl         << "Diag enabled         : " << diagEnabled << endl         << "User                 : " << user << endl         << "Endpoint             : " << endpoint << endl         << "STAF Instance UUID   : " << stafInstanceUUID << endl         << "Physical Identifier  : " << physicalInterfaceID << endl;    cout << endl << "Buffer: " << endl;        for (unsigned int i = 0; i < totalLength; ++i)    {        if ((i != 0) && (i % 8 == 0)) cout << endl;        unsigned int currChar = buffer[i];        if (currChar < 16) cout << "0";        cout << hex << currChar << dec << ":" << (char)currChar << " ";    }    cout << endl;    */    // Attach the current thread    JNIEnv *env = 0;    jint attachRC = gJVM->AttachCurrentThread(reinterpret_cast<void **>(&env),                                              NULL);    if (attachRC != 0)    {        STAFTrace::trace(            kSTAFTraceError, "Service: " + serviceName +            ", Request: " + request +            ", Error attaching Java VM thread, RC: " + STAFString(attachRC));        connection->writeUInt(kSTAFJavaError);        connection->writeString("Error attaching Java VM thread, RC: " +                                STAFString(attachRC));        return;    }    // Call JVM to load the class    jclass helperClass = env->GetObjectClass(gHelperObj);    jmethodID callService = env->GetMethodID(helperClass, sCallServiceMethod,                                             sCallServiceMethodSig);    if (callService == 0)    {        STAFTrace::trace(            kSTAFTraceError, "Service: " + serviceName +            ", Request: " + request +            ", Error getting STFServiceHelper.callService() method ID");        env->ExceptionDescribe();        env->ExceptionClear();        connection->writeUInt(kSTAFJavaError);        connection->writeString("Error getting "                                "STAFServiceHelper.callService() method ID");        gJVM->DetachCurrentThread();        return;    }    env->DeleteLocalRef(helperClass);    // Convert the various strings to UTF-8 for Java    serviceName += kUTF8_NULL;    machine += kUTF8_NULL;    machineNickname += kUTF8_NULL;    handleName = handleName.replace(kUTF8_NULL, kUTF8_NULL2);    handleName += kUTF8_NULL;    request = request.replace(kUTF8_NULL, kUTF8_NULL2);    request += kUTF8_NULL;    user += kUTF8_NULL;    endpoint += kUTF8_NULL;    stafInstanceUUID += kUTF8_NULL;    physicalInterfaceID += kUTF8_NULL;    jstring javaServiceName = env->NewStringUTF(serviceName.buffer());    jstring javaMachine = env->NewStringUTF(machine.buffer());    jstring javaMachineNickname = env->NewStringUTF(machineNickname.buffer());    jstring javaHandleName = env->NewStringUTF(handleName.buffer());    jstring javaRequest = env->NewStringUTF(request.buffer());    jstring javaUser = env->NewStringUTF(user.buffer());    jstring javaEndpoint = env->NewStringUTF(endpoint.buffer());    jstring javaSTAFInstanceUUID = env->NewStringUTF(stafInstanceUUID.buffer());    jstring javaPhysicalInterfaceID = env->NewStringUTF(physicalInterfaceID.buffer());    // Load and construct the ServiceRequest class    jclass serviceRequestClass = env->FindClass(sServiceRequestClass);    if (serviceRequestClass == 0)    {        STAFTrace::trace(            kSTAFTraceError, "Service: " + serviceName +            ", Request: " + request +            ", Error finding Java class: " + *sServiceRequestClassStringPtr);        env->ExceptionDescribe();        env->ExceptionClear();        connection->writeUInt(kSTAFJavaError);        connection->writeString("Error finding Java class: " +                                *sServiceRequestClassStringPtr);        gJVM->DetachCurrentThread();        return;    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -