📄 stafjavaservice.cpp
字号:
logPath.addDir("java"); logPath.addDir("jvm"); logPath.addDir(jvmName); if (!logPath.exists()) { try { // Don't want exceptions here STAFFSEntryPtr dir = logPath.createDirectory(kSTAFFSCreatePath); } catch (...) { /* Do Nothing */ } if (!logPath.exists()) { STAFString errmsg( "Error constructing the JVM using JVMName: " + jvmName + ", JVM: " + jvmExec + ", JVMOptions: " + jvmOptions + ", Error creating JVMLog directory: " + logPath.asString()); *pErrorBuffer = errmsg.adoptImpl(); return kSTAFServiceConfigurationError; } } // Name the current JVM log file - JVMLog.1 logPath.setName("JVMLog"); logPath.setExtension("1"); STAFString logName = logPath.asString(); // Instead of replacing the JVM log file each time a JVM is created, // use the following JVM OPTIONs to determine when to create a new // JVM log file and how many JVM log files to save: // - MaxLogs : Maximum number of JVM log files to keep. // - MaxLogSize: Maximum size of a JVM log file in bytes. // Open the JVM log file fstream outfile(logName.toCurrentCodePage()->buffer(), ios::out | ios::app); if (!outfile) { STAFString errmsg( "Error constructing the JVM using JVMName: " + jvmName + ", JVM: " + jvmExec + ", JVMOptions: " + jvmOptions + ", RC: " + STAFString(kSTAFFileOpenError) + ", Error opening file " + logName); *pErrorBuffer = errmsg.adoptImpl(); return kSTAFServiceConfigurationError; } // Figure out how big the JVM log file is outfile.seekp(0, ios::end); unsigned int fileLength = (unsigned int)outfile.tellp(); if (fileLength > maxLogSize) { // Roll any existing log files (e.g. Rename JVMLog.2.out to // JVMLog.3, JVMLog.1 to JVMLog.2, etc) and create a new // JVMLog.1 file. If the # of existing logs > MAXLOGS, don't // save the oldest log. outfile.close(); STAFFSPath fromLogPath(logPath); for (int i = maxLogs; i > 0; --i) { fromLogPath.setExtension(STAFString(i)); if (fromLogPath.exists() && i < maxLogs) { // Rename JVMLog.<i> to JVMLog.<i+1> STAFFSPath toLogPath(fromLogPath); toLogPath.setExtension(STAFString(i + 1)); fromLogPath.getEntry()->move(toLogPath.asString()); } } // Open a new empty current log file outfile.open(logName.toCurrentCodePage()->buffer(), ios::out | ios::trunc); if (!outfile) { STAFString errmsg( "Error constructing the JVM using JVMName: " + jvmName + ", JVM: " + jvmExec + ", JVMOptions: " + jvmOptions + ", RC: " + STAFString(kSTAFFileOpenError) + ", Error opening file " + logName); *pErrorBuffer = errmsg.adoptImpl(); return kSTAFServiceConfigurationError; } } // Write the JVM start information to the JVM log file STAFString separatorLine("***************************************" "***************************************"); STAFString line1("*** " + STAFTimestamp().asString() + " - Start of Log for JVMName: " + jvmName); STAFString line2("*** JVM Executable: " + jvmExec); STAFString line3("*** JVM Options :"); if (jvmOptions != STAFString(" ")) line3 += jvmOptions; else line3 += " none"; outfile << separatorLine.toCurrentCodePage()->buffer() << endl << line1.toCurrentCodePage()->buffer() << endl << line2.toCurrentCodePage()->buffer() << endl << line3.toCurrentCodePage()->buffer() << endl; outfile.close(); // Start a process for the JVM STAFProcessStartInfoLevel1 startInfo = { 0 }; startInfo.command = jvmExec.getImpl(); startInfo.parms = jvmStartString.getImpl(); startInfo.consoleMode = kSTAFProcessSameConsole; startInfo.stdoutMode = kSTAFProcessIOAppendFile; startInfo.stderrMode = kSTAFProcessIOStdout; startInfo.stdoutRedirect = logName.getImpl(); unsigned int osRC = 0; STAFString_t errorBuffer = 0; STAFRC_t rc = STAFProcessStart2( &jvmData.fJVM_PID, 0, &startInfo, 1, &osRC, &errorBuffer); if (rc != kSTAFOk) { STAFString startError( "Error starting a process for the JVM using " "JVMName: " + jvmName + ", JVM: " + jvmExec + ", JVMOptions: " + jvmOptions + ", RC: " + STAFString(rc) + ", Result: " + STAFString(errorBuffer, STAFString::kShallow)); // Add more details to the error msg when the Java executable // cannot be found. if (rc == 10) { if (!jvmSpecified) { startError = startError + " Make sure the java " + "executable is in the PATH."; } else { startError = startError + " Make sure " + jvmExec + " exists."; } } *pErrorBuffer = startError.adoptImpl(); return kSTAFServiceConfigurationError; } // Now we need to wait for it to start bool jvmReady = false; for (int i = 0; (i < 30) & !jvmReady; ++i) { // XXX: Need to check to see if the JVM is actually still running try { // First connect to the JVM STAFConnectionPtr connPtr = jvmData.fConnProv->connect(sLocal); // Now see if it's alive connPtr->writeUInt(JAVA_SERVICE_JVMPING); STAFRC_t jvmRC = connPtr->readUInt(); STAFString jvmResultString = connPtr->readString(); if (jvmRC != kSTAFOk) { jvmResultString = "Error starting JVM: " + jvmResultString; *pErrorBuffer = jvmResultString.adoptImpl(); return kSTAFServiceConfigurationError; } jvmReady = true; } catch (STAFException) { // XXX: This should really be more specific } if (!jvmReady) STAFThreadSleepCurrentThread(1000, 0); } // If we didn't time out waiting for the JVM then we add the // JVM to the map of JVMs if (!jvmReady) { *pErrorBuffer = STAFString("Unable to connect to JVM").adoptImpl(); return kSTAFServiceConfigurationError; } sJVMDataMap[jvmName] = JVMDataPtr(new JVMData(jvmData), JVMDataPtr::INIT); } else if (jvmSpecified || j2Specified) { *pErrorBuffer = STAFString("You may not specify the JVM or J2 " "options without specifying a new " "JVMNAME").adoptImpl(); return kSTAFServiceConfigurationError; } // Ok. We have a JVM. Now let's try to load the service. data.fJVM = sJVMDataMap[jvmName]; STAFConnectionPtr connPtr = data.fJVM->fConnProv->connect(sLocal); connPtr->writeUInt(JAVA_SERVICE_LOAD); connPtr->writeString(data.fName); connPtr->writeString(data.fExec); connPtr->writeString(pInfo->writeLocation); connPtr->writeUInt(pInfo->serviceType); unsigned int constructRC = kSTAFOk; try { constructRC = connPtr->readUInt(); } catch (STAFException) { // This error can occur when using certain versions of Java // (e.g gcj that is provided with some versions of Linux) STAFString error = STAFString( "JSTAF.STAFServiceConstruct(): Cannot load the Java ") + "service in the JVM. Verify you are using a valid version" " of Java (e.g. Sun or IBM Java)."; *pErrorBuffer = error.adoptImpl(); if (data.fJVM->fNumServices == 0) STAFShutdownJVM(jvmName); return kSTAFJavaError; } // Set a flag to indicate that the service has been loaded so that // if an exception occurs, the catch blocks will shutdown the JVM serviceLoaded = 1; STAFString constructResult = connPtr->readString(); if (constructRC != kSTAFOk) { if (data.fJVM->fNumServices == 0) STAFShutdownJVM(jvmName); *pErrorBuffer = constructResult.adoptImpl(); return constructRC; } // The service is now loaded STAFProcJavaServiceData *pService = new STAFProcJavaServiceData(data); *pServiceHandle = pService; ++pService->fJVM->fNumServices; return kSTAFOk; } catch (STAFException &e) { STAFString error = STAFString( "JSTAF.STAFServiceConstruct(), Service: ") + pInfo->name; e.trace(error.toCurrentCodePage()->buffer()); } catch (...) { STAFTrace::trace( kSTAFTraceError, STAFString("Caught unknown exception in ") + "JSTAF.STAFServiceConstruct(), Service: " + pInfo->name); } if (serviceLoaded && (data.fJVM->fNumServices == 0))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -