📄 stafsxe.java
字号:
return formattedTime;}/** * Returns a formatted String representation of a time of day. * The time should be expressed in milliseconds and stored in * a long (see java.util.Date). * The returned format is: hh:mm:ss.mmm */private String getTime(long time){ Calendar cal = Calendar.getInstance(); cal.setTime(new Date(time)); int hour = cal.get(Calendar.HOUR_OF_DAY); int min = cal.get(Calendar.MINUTE); int sec = cal.get(Calendar.SECOND); int milli = cal.get(Calendar.MILLISECOND); String milliString; /* Make sure that millis are represented properly */ if(milli < 10) { milliString = "00"+String.valueOf(milli); } else if (milli < 100) { milliString = "0"+String.valueOf(milli); } else { milliString = String.valueOf(milli); } String formattedTime = String.valueOf(hour)+":"+String.valueOf(min)+":"+ String.valueOf(sec)+"."+milliString; return formattedTime;}/** * Given a String representation of time of day in the format * hh:mm:ss.mmm returns a long representing the number of milliseconds * since the beginning of that day (12 am). */private long getTimeInMillis(String formattedTime) throws NumberFormatException{ StringTokenizer tok = new StringTokenizer(formattedTime, ":."); int hours = Integer.parseInt(tok.nextToken()); int mins = Integer.parseInt(tok.nextToken()); int secs = Integer.parseInt(tok.nextToken()); String mills = tok.nextToken(); // Make sure millis is correct (3 chars) // otherwise 0:0:0.5 is 5 millis instead of 500 while(true) { if (mills.length() == 3) { break; } mills = mills.concat("0"); } int millis = Integer.parseInt(mills); long time = (hours*3600000)+(mins*60000)+(secs*1000)+millis; return time;}/** * Displays help output for the SXE service. */private STAFResult handleHelp( STAFServiceInterfaceLevel30.RequestInfo reqInfo){ // Verify the requester has at least trust level 1 STAFResult trustResult = STAFUtil.validateTrust( 1, fServiceName, "HELP", fLocalMachineName, reqInfo); if (trustResult.rc != STAFResult.Ok) return trustResult; // Return help text for the service String help = "SXE STAF Service\n\n" + "EXECUTE FILE <File> [LOOP <Number of Loops> | MINRUNTIME <HH:MM>]\n" + "LIST SETTINGS\n" + "VERSION\n"+ "HELP"; return new STAFResult(STAFResult.Ok, help);}/** * Outputs SXE Service version information. */private STAFResult handleVersion( STAFServiceInterfaceLevel30.RequestInfo reqInfo){ // Verify the requester has at least trust level 1 STAFResult trustResult = STAFUtil.validateTrust( 1, fServiceName, "VERSION", fLocalMachineName, reqInfo); if (trustResult.rc != STAFResult.Ok) return trustResult; return new STAFResult(STAFResult.Ok, SXEVERSION);}/** * Method required by implemented STAF interface. * This method is called when STAF first initializes * the service. */public STAFResult init(STAFServiceInterfaceLevel30.InitInfo initInfo){ int result = STAFResult.Ok; /* Generate STAFParser */ sxeParser = new STAFCommandParser(); sxeParser.addOption(EXECUTE, 1, STAFCommandParser.VALUENOTALLOWED); sxeParser.addOption(VERSION, 1, STAFCommandParser.VALUENOTALLOWED); sxeParser.addOption(HELP, 1, STAFCommandParser.VALUENOTALLOWED); sxeParser.addOption(FILE, 1, STAFCommandParser.VALUEREQUIRED); sxeParser.addOption(LOOP, 1, STAFCommandParser.VALUEREQUIRED); sxeParser.addOption(MINRUNTIME, 1, STAFCommandParser.VALUEREQUIRED); sxeParser.addOptionGroup(FILE, 1, 1); sxeParser.addOptionGroup(LOOP+" "+MINRUNTIME, 0, 1); sxeParser.addOptionGroup(EXECUTE+" "+VERSION+" "+HELP, 1, 1); fListParser = new STAFCommandParser(); fListParser.addOption("LIST", 1, STAFCommandParser.VALUENOTALLOWED); fListParser.addOption("SETTINGS", 1, STAFCommandParser.VALUENOTALLOWED); fListParser.addOptionGroup("LIST", 1, 1); fListParser.addOptionNeed("LIST", "SETTINGS"); /* Create STAFHandle */ try { sHandle = new STAFHandle("STAF/Service/" + initInfo.name); } catch(STAFException se) { se.printStackTrace(); return new STAFResult(STAFResult.STAFRegistrationError, se.toString()); } fServiceName = initInfo.name; /* Parse registration parms */ STAFCommandParser parmsParser = new STAFCommandParser(); parmsParser.addOption(LOGNAME, 1, STAFCommandParser.VALUEREQUIRED); STAFCommandParseResult pResult = parmsParser.parse(initInfo.parms); if (pResult.rc != 0) { System.out.println( "Error parsing " + initInfo.name + " service parms."); result = pResult.rc; return new STAFResult( result, "Error parsing " + initInfo.name + " service parms."); } if (pResult.optionTimes(LOGNAME) == 1) { STAFResult resolvedResult = STAFUtil.resolveInitVar( pResult.optionValue(LOGNAME), sHandle); if (resolvedResult.rc != STAFResult.Ok) { System.out.println("Error resolving LOGNAME. RC=" + resolvedResult.rc + " Result=" + resolvedResult.result); return resolvedResult; } logName = resolvedResult.result; } // Construct map class for a successful EXECUTE request. fExecutionResultsMapClass = new STAFMapClassDefinition( "STAF/Service/SXE/ExecutionResults"); fExecutionResultsMapClass.addKey("loops", "Loops Executed"); fExecutionResultsMapClass.addKey("commands", "Commands Per Loop"); // Construct map class for a failed EXECUTE request. fErrorResultsMapClass = new STAFMapClassDefinition( "STAF/Service/SXE/ErrorInfo"); fErrorResultsMapClass.addKey("loopNum", "Loop Number"); fErrorResultsMapClass.addKey("lineNum", "Line Number"); fErrorResultsMapClass.addKey("commandNum", "Command Number"); fErrorResultsMapClass.addKey("command", "Command"); fErrorResultsMapClass.addKey("rc", "RC"); fErrorResultsMapClass.addKey("result", "Result"); fLogStartStopMapClass = new STAFMapClassDefinition( "STAF/Service/SXE/LogStartStop"); fLogStartStopMapClass.addKey("loopNum", "Loop Number"); fLogStartStopMapClass.addKey("timestamp", "Time"); fLogPassFailMapClass = new STAFMapClassDefinition( "STAF/Service/SXE/LogPassFail"); fLogPassFailMapClass.addKey("loopNum", "Loop Number"); fLogPassFailMapClass.addKey("elapsedTime", "Elapsed Time"); fLogPassFailMapClass.addKey("elapsedTarget", "Elapsed Target"); fLogPassFailMapClass.addKey("elapsedTolerance", "Elapsed Tolerance Percent"); fLogCommandMapClass = new STAFMapClassDefinition( "STAF/Service/SXE/LogCommand"); fLogCommandMapClass.addKey("action", "Action"); fLogCommandMapClass.addKey("command", "Command"); fLogCommandMapClass.addKey("result", "Result"); // Construct map-class for list settings information fSettingsMapClass = new STAFMapClassDefinition( "STAF/Service/SXE/Settings"); fSettingsMapClass.addKey("logName", "Log Name"); // Resolve the machine name variable for the local machine STAFResult res = STAFUtil.resolveInitVar("{STAF/Config/Machine}", sHandle); if (res.rc != STAFResult.Ok) return res; fLocalMachineName = res.result; /* Register RCs with help service. */ /* Do not replace result int if it already contained an error */ int regResult = registerHelp(initInfo.name); if (regResult != 0) { System.out.println("Error registering " + initInfo.name + " return codes with Help service."); if (result == 0) { result = regResult; } return new STAFResult(result, "Error registering " + initInfo.name + " return codes with Help service."); } return new STAFResult(result);}/** * This method determines if elapsedTime checking should be performed * for this execution. If both STAF variables STAF/Service/SXE/ElapsedTarget * and STAF/Service/SXE/ElapsedTolerance can be resolved, then elapsed time * checking should be performed. Note that a StringBuffer and 1 length arrays * are used, so that once the variables are resolved they may be used by the * calling method. * Returns true if elapsed time checking should be performed or false * otherwise. */private boolean isCheckElapsed(STAFHandle fHandle, StringBuffer elapsedTargetSBuf, long[] elapsedTarget, int[] elapsedTolerance) throws STAFException, NumberFormatException{ /* Resolve parms for ELAPSEDTARGET and ELAPSEDTOLERANCE, if both resolve OK, do elapsedTime checking */ boolean checkElapsed = false; String targetReq = "RESOLVE HANDLE " + String.valueOf(fHandle.getHandle()) + " STRING " + ELAPSEDTARGETVAR; String tolReq = "RESOLVE HANDLE " + String.valueOf(fHandle.getHandle()) + " STRING " + ELAPSEDTOLVAR; String elapsedTargetString; try { elapsedTargetString = fHandle.submit("LOCAL", "VAR", targetReq); } catch(STAFException se) { //don't throw STAFResult.VariableDoesNotExist if (se.rc == STAFResult.VariableDoesNotExist) { return false; } else { throw new STAFException(se.rc, se.getMessage()); } } elapsedTarget[0] = getTimeInMillis(elapsedTargetString); elapsedTolerance[0] = Integer.parseInt(fHandle.submit( "LOCAL", "VAR", tolReq)); elapsedTargetSBuf.replace(0, elapsedTargetSBuf.length(), elapsedTargetString); checkElapsed = true; // no exceptions, so check elapsed time return checkElapsed;}/** * Logs the start and success or failure of an individual command. * If the STAF variable STAF/Service/SXE/LogLevel is not set to "command", * then this logging will not occur. */private void logCommand(String logName, STAFHandle handle, int logType, String command, STAFResult result){ STAFResult llResult = handle.submit2( "local", "VAR", "RESOLVE STRING " + LOGLEVELVAR); if ((llResult.rc == STAFResult.Ok) && (llResult.result.equalsIgnoreCase(LOGCOMMAND))) { String where = "local"; String service = "LOG"; String request = null; String logMessage; String logLevel = "info"; // Create a map containing the log message information Map logMap = fLogCommandMapClass.createInstance(); switch(logType) { case START: logMap.put("action", "Begin"); logMap.put("command", command); break; case STOPPASS: logMap.put("action", "End"); logMap.put("command", command); logMap.put("result", result.result); break; case STOPFAIL: logMap.put("action", "End"); logMap.put("command", command); logMap.put("result", result.result); logLevel = "error"; break; } STAFMarshallingContext mc = new STAFMarshallingContext(); mc.setRootObject(logMap); mc.setMapClassDefinition(fLogCommandMapClass); logMessage = mc.marshall(); request = "LOG MACHINE LOGNAME " + logName + " LEVEL " + logLevel + " MESSAGE " + STAFUtil.wrapData(logMessage); handle.submit2(where, service, request); }}/** * Logs the start and success or failure of the execution of a file. * If the STAF variable STAF/Service/SXE/LogLevel is not set to "command" or * "file" then this logging will not occur. */private void logFile(String logName, int logType, STAFHandle handle, int loopNum, long startTime, long stopTime, String elapsedTargetString, int elapsedTolerance){ STAFResult llResult = handle.submit2( "local", "VAR", "RESOLVE STRING " + LOGLEVELVAR); if ((llResult.rc == STAFResult.Ok) && (llResult.result.equalsIgnoreCase(LOGCOMMAND) || llResult.result.equalsIgnoreCase(LOGFILEONLY))) { String where = "local"; String service = "LOG"; String request; String logMessage; String level = null; STAFMarshallingContext mc = new STAFMarshallingContext(); // Create a map containing the start or stop information to log Map logMap = fLogStartStopMapClass.createInstance(); mc.setMapClassDefinition(fLogStartStopMapClass); switch(logType) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -