📄 namedcounterservice.java
字号:
{ synchronized(counters) { if (counters.containsKey(myParsedRequest.optionValue("GET"))) { Integer i = (Integer) counters.get( myParsedRequest.optionValue("GET")); return new STAFResult(STAFResult.Ok, i.toString()); } else { return new STAFResult(CounterNotExist, "Counter does not exist"); } } } /** Process a LIST request * @param myParsedRequest The pre-parsed result * @return The STAFResult object which is the result of the request */ private STAFResult optionLIST(STAFCommandParseResult myParsedRequest) { STAFMarshallingContext mc = new STAFMarshallingContext(); synchronized(counters) { // List all counters by creating returning a marshalled map of // the counters mc.setRootObject(counters); return new STAFResult(STAFResult.Ok, mc.marshall()); } } /** Process an INC request * @param myParsedRequest The pre-parsed result * @return The STAFResult object which is the result of the request */ private STAFResult optionINC(STAFCommandParseResult myParsedRequest) { synchronized(counters) { if (counters.containsKey(myParsedRequest.optionValue("INC"))) { Integer i = (Integer) counters.get( myParsedRequest.optionValue("INC")); if (Integer.MAX_VALUE == i.intValue()) { return new STAFResult(CounterAtMax, "Counter already at maximum"); } else { i = new Integer(i.intValue() + 1); counters.put(myParsedRequest.optionValue("INC"), i); return new STAFResult(STAFResult.Ok, i.toString()); } } else { Integer i = new Integer(1); counters.put(myParsedRequest.optionValue("INC"), i); return new STAFResult(STAFResult.Ok, i.toString()); } } } /** Process a DEC request * @param myParsedRequest The pre-parsed result * @return The STAFResult object which is the result of the request */ private STAFResult optionDEC(STAFCommandParseResult myParsedRequest) { synchronized(counters) { if (counters.containsKey(myParsedRequest.optionValue("DEC"))) { Integer i = (Integer) counters.get( myParsedRequest.optionValue("DEC")); if (i.intValue() >= 1) { i = new Integer(i.intValue() - 1); counters.put(myParsedRequest.optionValue("DEC"), i); return new STAFResult(STAFResult.Ok, i.toString()); } else { return new STAFResult(CounterAlreadyZero, "Counter already at zero"); } } else { return new STAFResult(CounterNotExist, "Counter does not exist"); } } } /** Process a RESET request * @param myParsedRequest The pre-parsed result * @return The STAFResult object which is the result of the request */ private STAFResult optionRESET(STAFCommandParseResult myParsedRequest) { synchronized(counters) { Integer i = new Integer(0); counters.put(myParsedRequest.optionValue("RESET"), i); return new STAFResult(STAFResult.Ok, i.toString()); } } /** Process a DELETE reqest * @param myParsedRequest The pre-parsed result * @return The STAFResult object which is the result of the request */ private STAFResult optionDELETE(STAFCommandParseResult myParsedRequest) { synchronized(counters) { if (counters.containsKey(myParsedRequest.optionValue("DELETE"))) { counters.remove(myParsedRequest.optionValue("DELETE")); } return new STAFResult(STAFResult.Ok); } } /***************************************************************************/ /* acceptRequest- Calls appropriate methods to process a request from a */ /* client process. */ /* */ /* accepts: STAFServiceInterfaceLevel30 request information */ /* */ /* returns: STAFResult.rc = STAFResult.Ok, if successful; STAFResult. */ /* InvalidRequestString, if unsuccessful; */ /* */ /* STAFResult.result contains the result returned by the called */ /* method, if successful; */ /* STAFResult.result contains the command, if unsuccessful */ /***************************************************************************/ public STAFResult acceptRequest(STAFServiceInterfaceLevel30.RequestInfo info) { STAFResult result; try { // Create a STAFCommandParseResult object to evaluate the // arguments passed to the service. This is done using the // STAFCommandParser, which was created in init(). STAFCommandParseResult myParsedRequest = parser.parse(info.request); // If STAFEResult.rc is not 0, the argument string // contained an invalid argument if (myParsedRequest.rc != STAFResult.Ok) { result = new STAFResult(STAFResult.InvalidRequestString, myParsedRequest.errorBuffer); } else { if (myParsedRequest.optionTimes("HELP") == 1) { // Verify the requester has at least trust level 1 STAFResult trustResult = STAFUtil.validateTrust( 1, fServiceName, "HELP", fLocalMachineName, info); if (trustResult.rc != STAFResult.Ok) return trustResult; result = optionHELP(myParsedRequest); } else if (myParsedRequest.optionTimes("GET") == 1) { // Verify the requester has at least trust level 1 STAFResult trustResult = STAFUtil.validateTrust( 1, fServiceName, "GET", fLocalMachineName, info); if (trustResult.rc != STAFResult.Ok) return trustResult; result = optionGET(myParsedRequest); } else if (myParsedRequest.optionTimes("LIST") == 1) { // Verify the requester has at least trust level 1 STAFResult trustResult = STAFUtil.validateTrust( 1, fServiceName, "LIST", fLocalMachineName, info); if (trustResult.rc != STAFResult.Ok) return trustResult; result = optionLIST(myParsedRequest); } else if (myParsedRequest.optionTimes("VERSION") == 1) { // Verify the requester has at least trust level 1 STAFResult trustResult = STAFUtil.validateTrust( 1, fServiceName, "VERSION", fLocalMachineName, info); if (trustResult.rc != STAFResult.Ok) return trustResult; result = optionVERSION(myParsedRequest); } else { String action = ""; if (myParsedRequest.optionTimes("INC") == 1) action = "INC"; else if (myParsedRequest.optionTimes("RESET") == 1) action = "RESET"; else if (myParsedRequest.optionTimes("DELETE") == 1) action = "DELETE"; else if (myParsedRequest.optionTimes("DEC") == 1) action = "DEC"; else { // This should never be reached because it // would mean that the parse had failed, // however the following line is required // so that the code can be compiled. result = new STAFResult(STAFResult.UnknownError); } // Verify the requester has at least trust level 4 STAFResult trustResult = STAFUtil.validateTrust( 4, fServiceName, action, fLocalMachineName, info); if (trustResult.rc != STAFResult.Ok) return trustResult; if (action.equals("INC")) result = optionINC(myParsedRequest); else if (action.equals("RESET")) result = optionRESET(myParsedRequest); else if (action.equals("DELETE")) result = optionDELETE(myParsedRequest); else result = optionDEC(myParsedRequest); } } } catch (Exception e) { System.err.println("NamedCounterService:Error: " + "Exception caught whilst processing a request"); System.err.println(" : " + e.getMessage()); e.printStackTrace(); result = new STAFResult(STAFResult.JavaError, "Internal Java error."); } return result; } /** Terminate the service, and save any existing counters if * the option to persist them is set * @return Ok if saving the counters succeeded, or JavaError if * the save failed (this is assuming that the service has * been setup to persist the counters) */ public STAFResult term() { boolean saveResult = true; // Un-register Help Data unregisterHelpData(CounterNotExist); unregisterHelpData(CounterAlreadyZero); unregisterHelpData(CounterAtMax); // If persisting counters if (persist) { saveResult = saveCounters(); } // Un-register the service handle try { stafHandle.unRegister(); } catch (STAFException ex) { if (saveResult == true) { return new STAFResult(STAFResult.STAFRegistrationError, ex.toString()); } } // If the counters saved ok, (or we aren't persisting them) if (saveResult == true) { return new STAFResult(STAFResult.Ok); // Else something went wrong whilst saving the counters } else { return new STAFResult(STAFResult.JavaError, "Error saving counters."); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -