📄 deviceservice.java
字号:
catch (NumberFormatException e) { // Note that instead of creating a new error code specific for // this service, should use STAFResult.InvalidValue instead, but // wanted to demonstrate the registration of a service error code. return new STAFResult(kDeviceInvalidSerialNumber, sn); } // Add the device to the printer map or the modem map if (!printer.equals("")) { synchronized (fPrinterMap) { fPrinterMap.put(printer, new DeviceData(model, sn)); } } else if (!modem.equals("")) { synchronized (fModemMap) { fModemMap.put(modem, new DeviceData(model, sn)); } } return new STAFResult(STAFResult.Ok); } private STAFResult handleList(STAFServiceInterfaceLevel30.RequestInfo info) { // Verify the requester has at least trust level 2 STAFResult trustResult = STAFUtil.validateTrust( 2, fServiceName, "LIST", fLocalMachineName, info); if (trustResult.rc != STAFResult.Ok) return trustResult; // Parse the request STAFCommandParseResult parsedRequest = fListParser.parse(info.request); if (parsedRequest.rc != STAFResult.Ok) { return new STAFResult(STAFResult.InvalidRequestString, parsedRequest.errorBuffer); } // Check if specified printers or modems int printersOption = parsedRequest.optionTimes("printers"); int modemsOption = parsedRequest.optionTimes("modems"); boolean defaultList = false; if (printersOption == 0 && modemsOption == 0) { defaultList = true; } // Create a marshalling context and set any map classes (if any). STAFMarshallingContext mc = new STAFMarshallingContext(); mc.setMapClassDefinition(fListDeviceMapClass); // Create an empty result list to contain the result List resultList = new ArrayList(); // Add printer entries to the result list if (defaultList || printersOption > 0) { Iterator iter = fPrinterMap.keySet().iterator(); while (iter.hasNext()) { String key = (String)iter.next(); DeviceData data = (DeviceData)fPrinterMap.get(key); Map resultMap = fListDeviceMapClass.createInstance(); resultMap.put("name", key); resultMap.put("type", "Printer"); resultMap.put("model", data.model); resultMap.put("serial#", data.sn); resultList.add(resultMap); } } // Add modem entries to the result list if (defaultList || modemsOption > 0) { Iterator iter = fModemMap.keySet().iterator(); while (iter.hasNext()) { String key = (String)iter.next(); DeviceData data = (DeviceData)fModemMap.get(key); Map resultMap = fListDeviceMapClass.createInstance(); resultMap.put("name", key); resultMap.put("type", "Modem"); resultMap.put("model", data.model); resultMap.put("serial#", data.sn); resultList.add(resultMap); } } // Set the result list as the root object for the marshalling context // and return the marshalled result mc.setRootObject(resultList); return new STAFResult(STAFResult.Ok, mc.marshall()); } private STAFResult handleQuery(STAFServiceInterfaceLevel30.RequestInfo info) { // Verify the requester has at least trust level 2 STAFResult trustResult = STAFUtil.validateTrust( 2, fServiceName, "QUERY", fLocalMachineName, info); if (trustResult.rc != STAFResult.Ok) return trustResult; // Parse the request STAFCommandParseResult parsedRequest = fQueryParser.parse(info.request); if (parsedRequest.rc != STAFResult.Ok) { return new STAFResult(STAFResult.InvalidRequestString, parsedRequest.errorBuffer); } // Resolve any STAF variables in the printer option's value STAFResult res = new STAFResult(); res = STAFUtil.resolveRequestVar( parsedRequest.optionValue("printer"), fHandle, info.requestNumber); if (res.rc != STAFResult.Ok) return res; String printer = res.result; // Resolve any STAF variables in the modem option's value res = STAFUtil.resolveRequestVar( parsedRequest.optionValue("modem"), fHandle, info.requestNumber); if (res.rc != STAFResult.Ok) return res; String modem = res.result; // Create a marshalling context and set any map classes (if any). STAFMarshallingContext mc = new STAFMarshallingContext(); mc.setMapClassDefinition(fQueryDeviceMapClass); // Create an empty result map to contain the result Map resultMap = fQueryDeviceMapClass.createInstance(); // Find the specified printer/modem and add its info to the result map if (!printer.equals("")) { if (fPrinterMap.containsKey(printer)) { DeviceData data = (DeviceData)fPrinterMap.get(printer); resultMap.put("model", data.model); resultMap.put("serial#", data.sn); } else { return new STAFResult(STAFResult.DoesNotExist, printer); } } else if (!modem.equals("")) { if (fModemMap.containsKey(modem)) { DeviceData data = (DeviceData)fModemMap.get(modem); resultMap.put("model", data.model); resultMap.put("serial#", data.sn); } else { return new STAFResult(STAFResult.DoesNotExist, modem); } } // Set the result map as the root object for the marshalling context // and return the marshalled result mc.setRootObject(resultMap); return new STAFResult(STAFResult.Ok, mc.marshall()); } private STAFResult handleDelete(STAFServiceInterfaceLevel30.RequestInfo info) { // Verify the requester has at least trust level 4 STAFResult trustResult = STAFUtil.validateTrust( 4, fServiceName, "DELETE", fLocalMachineName, info); if (trustResult.rc != STAFResult.Ok) return trustResult; // Parse the request STAFCommandParseResult parsedRequest = fDeleteParser.parse(info.request); if (parsedRequest.rc != STAFResult.Ok) { return new STAFResult(STAFResult.InvalidRequestString, parsedRequest.errorBuffer); } // Resolve any STAF variables in the print option's value STAFResult res = new STAFResult(); res = STAFUtil.resolveRequestVar( parsedRequest.optionValue("printer"), fHandle, info.requestNumber); if (res.rc != STAFResult.Ok) return res; String printer = res.result; // Resolve any STAF variables in the modem option's value res = STAFUtil.resolveRequestVar( parsedRequest.optionValue("modem"), fHandle, info.requestNumber); if (res.rc != STAFResult.Ok) return res; String modem = res.result; // Find the device in the printer or modem map and remove it if (!printer.equals("")) { synchronized (fPrinterMap) { if (fPrinterMap.containsKey(printer)) fPrinterMap.remove(printer); else return new STAFResult(STAFResult.DoesNotExist, printer); } } else if (!modem.equals("")) { synchronized (fModemMap) { if (fModemMap.containsKey(modem)) fModemMap.remove(modem); else return new STAFResult(STAFResult.DoesNotExist, modem); } } return new STAFResult(STAFResult.Ok); } public STAFResult term() { try { // Un-register Help Data unregisterHelpData(kDeviceInvalidSerialNumber); // Un-register the service handle fHandle.unRegister(); } catch (STAFException ex) { return new STAFResult(STAFResult.STAFRegistrationError, ex.toString()); } return new STAFResult(STAFResult.Ok); } // Register error codes for the STAX Service with the HELP service private void registerHelpData(int errorNumber, String info, String description) { STAFResult res = fHandle.submit2( "local", "HELP", "REGISTER SERVICE " + fServiceName + " ERROR " + errorNumber + " INFO " + STAFUtil.wrapData(info) + " DESCRIPTION " + STAFUtil.wrapData(description)); } // Un-register error codes for the STAX Service with the HELP service private void unregisterHelpData(int errorNumber) { STAFResult res = fHandle.submit2( "local", "HELP", "UNREGISTER SERVICE " + fServiceName + " ERROR " + errorNumber); } public class DeviceData { public String model = ""; public String sn = ""; public DeviceData(String model, String sn) { this.model = model; this.sn = sn; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -