📄 namespaceservice.java
字号:
Variable variable = (Variable)iter.next(); String key = variable.getKey(); String value = variable.getValue(); Map resultMap = fListVariablesMapClass.createInstance(); resultMap.put("key", key); resultMap.put("value", value); if (!name.equalsIgnoreCase(Namespace.sNONE)) resultMap.put("namespace", name); resultList.add(resultMap); } if (!only) { // List variables from the parent namespaces String parent = ns.getParent(); while (!parent.equalsIgnoreCase(Namespace.sNONE)) { Namespace parentNS = fNamespaceManager.get(parent); variableMap = parentNS.getVariables(); iter = variableMap.values().iterator(); while (iter.hasNext()) { // Add an entry for each variable to result list Variable variable = (Variable)iter.next(); String key = variable.getKey(); // Don't list any variables from parent namespaces // that were overridden in the specified namespace if (ns.hasVariable(key)) { continue; } String value = variable.getValue(); Map resultMap = fListVariablesMapClass. createInstance(); resultMap.put("key", key); resultMap.put("value", value); if (!parent.equalsIgnoreCase(Namespace.sNONE)) resultMap.put("namespace", parent); resultList.add(resultMap); } parent = parentNS.getParent(); } } } } else { // LIST [NAMESPACES] mc.setMapClassDefinition(fListNamespacesMapClass); // Synchronized on the Namespace service so that only one request // to the Namespace service can run at a time (to ensure that // nothing else can change the Namespace data while this code is // running). synchronized(this) { // Add an entry for each namespace to the result list Iterator iter = fNamespaceManager.getNamespaceMapCopy(). values().iterator(); while (iter.hasNext()) { Namespace ns = (Namespace)iter.next(); Map resultMap = fListNamespacesMapClass.createInstance(); resultMap.put("name", ns.getName()); resultMap.put("description", ns.getDescription()); String parent = ns.getParent(); if (!parent.equalsIgnoreCase(Namespace.sNONE)) { resultMap.put("parent", ns.getParent()); } 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()); } /** * Handle a QUERY request */ 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 NAMESPACE option's value STAFResult res = STAFUtil.resolveRequestVar( parsedRequest.optionValue("NAMESPACE"), fHandle, info.requestNumber); if (res.rc != STAFResult.Ok) return res; String namespace = res.result; STAFMarshallingContext mc = new STAFMarshallingContext(); boolean tree = false; if (parsedRequest.optionTimes("TREE") > 0) tree = true; if (!tree) { // QUERY NAMESPACE <Namespace> // Create a marshalling context and set any map classes (if any). mc.setMapClassDefinition(fQueryfNamespaceMapClass); // Create an empty result map to contain the result Map resultMap = fQueryfNamespaceMapClass.createInstance(); // Synchronized on the Namespace service so that only one request // to the Namespace service can run at a time (to ensure that // nothing else can change the Namespace data while this code is // running). synchronized(this) { // Find the specified namespace and add its info to the // result map Namespace ns = fNamespaceManager.get(namespace); if (ns == null) { return new STAFResult( STAFResult.DoesNotExist, "Namespace '" + namespace + "' does not exist"); } resultMap.put("name", ns.getName()); resultMap.put("description", ns.getDescription()); String parent = ns.getParent(); if (!parent.equalsIgnoreCase(Namespace.sNONE)) { resultMap.put("parent", ns.getParent()); } resultMap.put("children", new ArrayList(ns.getChildren().keySet())); } mc.setRootObject(resultMap); } else { // QUERY NAMESPACE <Namespace> TREE // Create a marshalling context and set any map classes (if any). mc.setMapClassDefinition(fQueryTreeMapClass); // Create an empty result map to contain the result Map resultMap = fQueryTreeMapClass.createInstance(); // Synchronized on the Namespace service so that only one request // to the Namespace service can run at a time (to ensure that // nothing else can change the Namespace data while this code is // running). synchronized(this) { // Find the specified namespace and add its info to the // result map Namespace ns = fNamespaceManager.get(namespace); if (ns == null) { return new STAFResult( STAFResult.DoesNotExist, "Namespace '" + namespace + "' does not exist"); } resultMap.put("name", ns.getName()); // Iterate thru the children List resultList = visitChildren(ns.getChildren()); resultMap.put("children", resultList); } mc.setRootObject(resultMap); } return new STAFResult(STAFResult.Ok, mc.marshall()); } /** * Helper method for a QUERY TREE request to build up the output that * shows all of the child namespaces in the hierarchy */ private List visitChildren(Map children) { List resultList = new ArrayList(); Iterator iter = children.values().iterator(); while (iter.hasNext()) { Namespace childNS = (Namespace)iter.next(); Map childResultMap = fQueryTreeMapClass.createInstance(); childResultMap.put("name", childNS.getName()); childResultMap.put("children", visitChildren(childNS.getChildren())); resultList.add(childResultMap); } return resultList; } /** * Handle a SET request */ private STAFResult handleSet(STAFServiceInterfaceLevel30.RequestInfo info) { // Verify the requester has at least trust level 3 STAFResult trustResult = STAFUtil.validateTrust( 3, fServiceName, "SET", fLocalMachineName, info); if (trustResult.rc != STAFResult.Ok) return trustResult; // Parse the request STAFCommandParseResult parsedRequest = fSetParser.parse(info.request); if (parsedRequest.rc != STAFResult.Ok) { return new STAFResult(STAFResult.InvalidRequestString, parsedRequest.errorBuffer); } // Resolve any STAF variables in the NAMESPACE option's value STAFResult res = STAFUtil.resolveRequestVar( parsedRequest.optionValue("NAMESPACE"), fHandle, info.requestNumber); if (res.rc != STAFResult.Ok) return res; String namespace = res.result; // Don't resolve STAF variables in the VAR option values. // Make sure the variables have format Key=Value int numVars = parsedRequest.optionTimes("VAR"); HashMap vars = new HashMap(); for (int i = 1; i <= numVars; ++i) { String varString = parsedRequest.optionValue("VAR", i); int equalPos = varString.indexOf("="); if (equalPos == -1) { return new STAFResult( STAFResult.InvalidValue, "Variable format must be Key=Value. VAR: " + varString); } String key = varString.substring(0, equalPos); String value = varString.substring(equalPos + 1); vars.put(key, value); } // Synchronized on the Namespace service so that only one request // to the Namespace service can run at a time (to ensure that nothing // else can change the Namespace data while this code is running). synchronized(this) { // Make sure the namespace exists Namespace ns = fNamespaceManager.get(namespace); if (ns == null) { return new STAFResult( STAFResult.DoesNotExist, "Namespace '" + namespace + "' does not exist"); } // Set the variable(s) in the namespace: // - If the variable key already exists, update its value. // - If the variable key does not exist, add it. ns.addVariables(vars); // Save namespaces to persistent data storage res = storeData(info); if (res.rc != STAFResult.Ok) return res; } return new STAFResult(STAFResult.Ok); } /** * Handle a GET request */ private STAFResult handleGet(STAFServiceInterfaceLevel30.RequestInfo info) { // Verify the requester has at least trust level 2 STAFResult trustResult = STAFUtil.validateTrust( 2, fServiceName, "GET", fLocalMachineName, info); if (trustResult.rc != STAFResult.Ok) return trustResult; // Parse the request STAFCommandParseResult parsedRequest = fGetParser.parse(info.request); if (parsedRequest.rc != STAFResult.Ok) { return new STAFResult(STAFResult.InvalidRequestString, parsedRequest.errorBuffer); } // Resolve any STAF variables in the NAMESPACE option's value STAFResult res = STAFUtil.resolveRequestVar( parsedRequest.optionValue("NAMESPACE"), fHandle, info.requestNumber); if (res.rc != STAFResult.Ok) return res; String namespace = res.result; res = STAFUtil.resolveRequestVar( parsedRequest.optionValue("VAR"), fHandle, info.requestNumber); if (res.rc != STAFResult.Ok) return res; String varKey = res.result; String varValue = ""; // Synchronized on the Namespace service so that only one request // to the Namespace service can run at a time (to ensure that nothing // else can change the Namespace data while this code is running). synchronized(this) { // Make sure the namespace exists Namespace ns = fNamespaceManager.get(namespace); if (ns == null) { return new STAFResult( STAFResult.DoesNotExist, "Namespace '" + namespace + "' does not exist"); } // Check if the variable exists in the specified namespace. // If so, return it's value. if (ns.hasVariable(varKey)) { varValue = ns.getVariable(varKey); } else { // If not found in the specified namespace, check the parent // namespace, and so on, up the hierarchy, until the variable // key is found or until a namespace has no parent. // Return the variable's value in the result buffer for the // first instance of the variable found or return an error if // not found. String parent = ns.getParent(); while (!parent.equalsIgnoreCase(Namespace.sNONE)) { ns = fNamespaceManager.get(parent); if (ns.hasVariable(varKey)) { varValue = ns.getVariable(varKey); return new STAFResult(STAFResult.Ok, varValue); } else { parent = ns.getParent(); } } return new STAFResult( STAFResult.DoesNotExist, "Variable with key '" + varKey + "' does not exist"); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -