📄 stafjavaservice.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 "STAFServiceInterface.h"#include "STAFJavaService.h"#include "STAFConnectionProvider.h"#include "STAFUtil.h"#include "STAFThread.h"#include "STAFEventSem.h"#include "STAFMutexSem.h"#include "STAFProcess.h"#include "STAFFileSystem.h"#include "STAFTrace.h"#include "STAF_fstream.h"struct JVMData{ STAFString fName; STAFString fExec; STAFString fOptions; STAFConnectionProviderPtr fConnProv; STAFEventSemPtr fJVMExitedSem; STAFProcessID_t fJVM_PID; unsigned int fNumServices;};typedef STAFRefPtr<JVMData> JVMDataPtr;typedef std::map<STAFString, JVMDataPtr> JVMDataMap;struct JVMStartThreadData{ JVMStartThreadData (STAFString_t startString, STAFEventSemPtr &exitedSem) : fStartString(startString), fJVMExitedSem(exitedSem) { /* Do nothing */ } STAFString_t fStartString; STAFEventSemPtr fJVMExitedSem;};struct STAFProcJavaServiceData{ STAFString fName; STAFString fExec; JVMDataPtr fJVM;};static JVMDataMap sJVMDataMap;static STAFMutexSem sJVMDataSem;static STAFString sLocal("local");static STAFString sIPCName("IPCNAME");STAFRC_t STAFServiceGetLevelBounds(unsigned int levelID, unsigned int *minimum, unsigned int *maximum){ switch (levelID) { case kServiceInfo: { *minimum = 30; *maximum = 30; break; } case kServiceInit: { *minimum = 30; *maximum = 30; break; } case kServiceAcceptRequest: { *minimum = 30; *maximum = 30; break; } case kServiceTerm: case kServiceDestruct: { *minimum = 0; *maximum = 0; break; } default: { return kSTAFInvalidAPILevel; } } return kSTAFOk;}unsigned int STAFDoShutdownJVM(STAFConnectionProviderPtr &connProv){ try { STAFConnectionPtr connPtr = connProv->connect(sLocal); connPtr->writeUInt(JAVA_SERVICE_JVMEXIT); STAFRC_t jvmRC = connPtr->readUInt(); STAFString jvmResultString = connPtr->readString(); // Note, this last connect is required depending on the thread timing // in STAFJavaServiceHelper.cpp. In that file, if the request // thread gets started before the creating thread gets control back, // then this call is unnecessary. Otherwise, we need to send this // final request so that the main thread will be able to check the // exit flag. connPtr = connProv->connect(sLocal); connPtr->writeUInt(JAVA_SERVICE_JVMFIN); } catch (STAFConnectionProviderException) { // These exceptions are anticipated in cases where the JVM has // already shutdown before we send the shutdown commands } return 0;}// Note: You should already own sJVMDataSem before calling STAFShutdownJMVunsigned int STAFShutdownJVM(STAFString &jvmName){ try { JVMDataPtr jvm = sJVMDataMap[jvmName]; sJVMDataMap.erase(jvmName); STAFDoShutdownJVM(jvm->fConnProv); } catch (STAFException &e) { e.trace((STAFString("JSTAF.STAFShutdownJVM(), JVMName: ") + jvmName). toCurrentCodePage()->buffer()); } catch (...) { STAFTrace::trace( kSTAFTraceError, "Caught unknown exception in JSTAF.STAFShutdownJVM(), JVMName: " + jvmName); } return 0;}STAFRC_t STAFServiceConstruct(STAFServiceHandle_t *pServiceHandle, void *pServiceInfo, unsigned int infoLevel, STAFString_t *pErrorBuffer){ if (infoLevel != 30) return kSTAFInvalidAPILevel; STAFProcJavaServiceData data; STAFServiceInfoLevel30 *pInfo = static_cast<STAFServiceInfoLevel30 *>(pServiceInfo); unsigned int serviceLoaded = 0; STAFString jvmName = "STAFJVM1"; try { STAFString jvmExec = "java"; STAFString jvmOptions = " "; bool jvmSpecified = false; bool j2Specified = false; unsigned int maxLogs = 5; // Defaults to keeping 5 JVM logs long maxLogSize = 1048576; // Default size of each log is 1M data.fName = pInfo->name; data.fExec = pInfo->exec; // Walk through and verify the config options for (unsigned int i = 0; i < pInfo->numOptions; ++i) { STAFString upperOptionName = STAFString(pInfo->pOptionName[i]).upperCase(); STAFString optionValue(pInfo->pOptionValue[i]); if (upperOptionName == "JVMNAME") { jvmName = optionValue; } else if (upperOptionName == "JVM") { jvmExec = optionValue; jvmSpecified = true; } else if (upperOptionName == "J2") { jvmOptions += optionValue + " "; j2Specified = true; } else if (upperOptionName == "MAXLOGS") { // Check to make sure it is an integer value > 0 STAFString maxLogsStr = optionValue; if (maxLogsStr.isDigits() && (maxLogsStr.asUInt() > 0)) { maxLogs = maxLogsStr.asUInt(); } else { STAFString errmsg( "Error constructing the JVM using JVMName: " + jvmName + ", JVM: " + jvmExec + ", JVMOptions: " + jvmOptions + ", RC: " + STAFString(kSTAFInvalidValue) + ", MAXLOGS must be an positive integer"); *pErrorBuffer = errmsg.adoptImpl(); return kSTAFServiceConfigurationError; } } else if (upperOptionName == "MAXLOGSIZE") { // Check to make sure it is an integer value > 0 STAFString maxLogSizeStr = optionValue; if (maxLogSizeStr.isDigits() && (maxLogSizeStr.asUInt() > 0)) { maxLogSize = (long)maxLogSizeStr.asUInt(); } else { STAFString errmsg( "Error constructing the JVM using JVMName: " + jvmName + ", JVM: " + jvmExec + ", JVMOptions: " + jvmOptions + ", RC: " + STAFString(kSTAFInvalidValue) + ", MAXLOGSIZE must be an positive integer"); *pErrorBuffer = errmsg.adoptImpl(); return kSTAFServiceConfigurationError; } } else { STAFString optionError(pInfo->pOptionName[i]); *pErrorBuffer = optionError.adoptImpl(); return kSTAFServiceConfigurationError; } } // Check to see if the specified JVM already exists STAFMutexSemLock lock(sJVMDataSem); if (sJVMDataMap.find(jvmName) == sJVMDataMap.end()) { // The JVM doesn't exist so we need to start it JVMData jvmData; jvmData.fName = jvmName; jvmData.fExec = jvmExec; jvmData.fOptions = jvmOptions; jvmData.fJVMExitedSem = STAFEventSemPtr(new STAFEventSem, STAFEventSemPtr::INIT); jvmData.fNumServices = 0; STAFString jvmStartString = jvmOptions + "com.ibm.staf.service.STAFServiceHelper " + jvmName; // Create the connection provider for the JVM STAFString ipcName = "JSTAF_" + jvmName; STAFStringConst_t optionData[] = { sIPCName.getImpl(), ipcName.getImpl() }; STAFConnectionProviderConstructInfoLevel1 constructInfo = { kSTAFConnectionProviderOutbound, 1, optionData, &optionData[1] }; jvmData.fConnProv = STAFConnectionProvider::createRefPtr(ipcName, "STAFLIPC", &constructInfo, 1); // We need to shutdown any JVM that might happen to be leftover // from a "bad" exit from a previous STAFProc STAFDoShutdownJVM(jvmData.fConnProv); // We need to capture stdout/stderr for diagnostic purposes // Create directory for JVM log file if doesn't already exist STAFFSPath logPath; logPath.setRoot(pInfo->writeLocation); logPath.addDir("lang");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -