⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 namespaceservice.java

📁 Software Testing Automation Framework (STAF)的开发代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                return new STAFResult(STAFResult.Ok, varValue);    }    /**     * Handle a DELETE request     */     private STAFResult handleDelete(        STAFServiceInterfaceLevel30.RequestInfo info)    {        // Parse the request        STAFCommandParseResult parsedRequest = fDeleteParser.parse(            info.request);        if (parsedRequest.rc != STAFResult.Ok)        {            return new STAFResult(STAFResult.InvalidRequestString,                                  parsedRequest.errorBuffer);        }        int numVars = parsedRequest.optionTimes("VAR");        // Check trust level        STAFResult trustResult;        if (numVars > 0)        {            // Delete var from a namespace request requires trust level 3            trustResult = STAFUtil.validateTrust(                3, fServiceName, "DELETE", fLocalMachineName, info);        }        else        {            // Delete namespace request requires trust level 4            trustResult = STAFUtil.validateTrust(                4, fServiceName, "DELETE", fLocalMachineName, info);        }        if (trustResult.rc != STAFResult.Ok) return trustResult;        // 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;        if (numVars == 0)        {            // DELETE NAMESPACE            if (parsedRequest.optionTimes("CONFIRM") == 0)            {                return new STAFResult(                    STAFResult.InvalidRequestString,                    "When specifying option DELETE without any VAR options," +                    " you must also specify option CONFIRM to delete the " +                    "namespace");            }            // 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)            {                // Check if the namespace exists                Namespace ns = fNamespaceManager.get(namespace);                if (ns == null)                {                    return new STAFResult(                        STAFResult.DoesNotExist,                        "Namespace '" + namespace + "' does not exist");                }                                // If the namespace has any child namespaces, their parents                // will be changed to be the parent of the deleted namespace                // or null if it has no parent                namespace = ns.getName();                String parent = ns.getParent();                Namespace parentNS = null;                if (!parent.equalsIgnoreCase(Namespace.sNONE))                {                    parentNS = fNamespaceManager.get(parent);                }                                Map children = ns.getChildren();                Iterator iter = children.keySet().iterator();                while (iter.hasNext())                {                    String childNSName = (String)iter.next();                    Namespace childNS = (Namespace)children.get(childNSName);                    // Set new parent                    childNS.setParent(parent);                    // Add this namespace's children to the new parent's                    // children                    if (parentNS != null)                    {                        parentNS.addChild(childNSName, childNS);                    }                }                if (parentNS != null)                {                    // Delete this namespace from its parent's children list                                    parentNS.removeChild(namespace);                }                                // Save a copy of the Namespaces file in a temporary directory                // before deleting the namespace                                copyData("delete");                // Delete the namespace                fNamespaceManager.delete(namespace);                // Save namespaces to persistent data storage                res = storeData(info);                if (res.rc != STAFResult.Ok) return res;            }        }        else        {            // DELETE Variable(s) in a NAMESPACE                        List varKeyList = new ArrayList();            for (int i = 1; i <= numVars; ++i)            {                res = STAFUtil.resolveRequestVar(                    parsedRequest.optionValue("VAR", i),                    fHandle, info.requestNumber);                if (res.rc != STAFResult.Ok) return res;                        varKeyList.add(res.result);            }            // 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)            {                // Remove variables from the namespace                Namespace ns = fNamespaceManager.get(namespace);                if (ns == null)                {                    return new STAFResult(                        STAFResult.DoesNotExist,                        "Namespace '" + namespace + "' does not exist");                }                                Iterator iter = varKeyList.iterator();                while (iter.hasNext())                {                    String key = (String)iter.next();                    if (!ns.hasVariable(key))                    {                        return new STAFResult(                            STAFResult.DoesNotExist,                            "Variable with key '" + key + "' does not exist");                    }                                        // Delete the variable from the namespace                    ns.removeVariable(key);                }                                // Save namespaces to persistent data storage                res = storeData(info);                if (res.rc != STAFResult.Ok) return res;            }        }                return new STAFResult(STAFResult.Ok);    }    /**     * This is the STAF Service termination method that is run when the     * service is unregistered.  It performs termination functions such as:     * <ul>     * <li>Unregisters any serivce help messages     * <li>Unregisters the STAF handle it used to submit requests to STAF     * services     * * </ul>     * @return An instance of STAFResult which contains the return code and     * result buffer indicating if the service terminated successfully.     */     public STAFResult term()    {        logMessage("Stop", "Terminating the " + fServiceName + " service",                   false, null);        try        {            // Save a copy of the Namespaces file in a temporary directory            copyData("term");            // Un-register Help Data            unregisterHelpData(kDataStorageError);            // Un-register the service handle            fHandle.unRegister();        }        catch (STAFException ex)        {            return new STAFResult(STAFResult.STAFRegistrationError,                                  ex.toString());        }        return new STAFResult(STAFResult.Ok);    }    /**     * Rename a file     */    private boolean renameFile(File oldFile, File newFile)    {        newFile.delete();        return oldFile.renameTo(newFile);    }    /**     * Save the namespaces data currently in memory to persistent storage.     * If an error occurs writing to persistent storage, restore the     * namespaces data to it's previous state to keep the namespaces data     * in memory in sync with the data in persistent storage.     */     private STAFResult storeData(STAFServiceInterfaceLevel30.RequestInfo info)    {        // Backup the Namespaces.xml file        boolean haveBackup = renameFile(fDataFile, fBackupFile);        // Write the namespaces data to persistent storage        try        {            fStorageManager.saveNamespaces();        }        catch (NSException e)        {            // Writing the namespaces data to persistent storage failed.            // Use the Namespaces backup file to restore namespaces data in            // memory to keep them in sync (and to back out the change just            // made).            // Log to service log and print stack trace to JVM log            String errorMsg = "Saving namespaces data to persistent " +                "storage failed. " + fLineSep + e + fLineSep +                "See the service log for more information and contact the " +                "system administrator for the service machine.";            logMessage("Error", errorMsg, true, info);            e.printStackTrace();            if (haveBackup)            {                // Restore to the backup Namespaces xml file                if (renameFile(fBackupFile, fDataFile))                {                    try                    {                        // Reload namespace data from backup file                        fStorageManager.loadNamespaces();                        logMessage(                            "Info", "Successfully reloaded namespaces data " +                            "from backup file.", false, null);                    }                    catch (NSException nse)                    {                        logMessage(                            "Error", "Namespaces data in memory is out of " +                            "sync with data stored in persistent storage " +                            "because loadNamespaces() failed to restore " +                            "Namespaces data from backup file. " + fLineSep +                            nse, false, null);                    }                }                else                {                    logMessage(                        "Error", "Namespaces data in memory is out of sync " +                        "with data stored in persistent storage because " +                        " renameFile(" + fBackupFile + ", " + fDataFile +                        ") failed.", false, null);                }            }            else            {                logMessage("Error", "Namespaces data in memory is out of " +                           "sync with data stored in persistent storage " +                           "because a backup file is not available.",                           false, null);            }            return new STAFResult(kDataStorageError, errorMsg);        }        logMessage("Info", "", true, info);        return new STAFResult(STAFResult.Ok);    }    /**     * Copy the current Namespaces.xml file to a temporary file that is     * available for backup purposes     */     private void copyData(String phase)    {        // Create a unique file name to copy the Namespace file to using an        // extension containing the current time in milli-seconds.  The        // format for this file name (stored in a temporary directory) is:        //        //  Namespaces.xml.<phase>.<current time in milli-seconds>        long currentTime = new Date().getTime();  // Get current time        String toFile = fTempDir + fFileSep + StorageManager.sXmlFileName +            "." + phase + "." + currentTime;        // Copy the file        String copyRequest = "COPY FILE " +            STAFUtil.wrapData(fStorageManager.getFileName()) +            " TOFILE " + STAFUtil.wrapData(toFile) +            " TOMACHINE local";        STAFResult res = fHandle.submit2("local", "FS", copyRequest);        if (res.rc != STAFResult.Ok)        {            logMessage(                "Error", "Error copying Namespaces data file.  RC=" +                res.rc + ", Result=" + res.result +                ", Request=" + copyRequest, false, null);        }    }        /**     * Backup the current Namespaces.xml file     */     private STAFResult makeBackupFile()    {        String toFile = fDataDir + fFileSep + "Namespaces_backup.xml";        // Copy the file        String copyRequest = "COPY FILE " +            STAFUtil.wrapData(fStorageManager.getFileName()) +            " TOFILE " + STAFUtil.wrapData(toFile) +            " TOMACHINE local";        STAFResult res = fHandle.submit2("local", "FS", copyRequest);        if (res.rc != STAFResult.Ok)        {            logMessage(                "Error", "Error making a backup copy of the Namespaces.xml" +                " file.  RC=" + res.rc + ", Result=" + res.result +                ", Request=" + copyRequest, false, null);            return res;        }        return new STAFResult(STAFResult.Ok);    }        /**     * Log a message to the global service log     */     private void logMessage(String level, String msg, boolean logRequestInfo,                            STAFServiceInterfaceLevel30.RequestInfo info)    {        String message = msg;        if (logRequestInfo)        {            message = "[" + info.endpoint + " " + info.handleName + " " +                info.handle + "] " + info.request;            if (!msg.equals(""))            {                message = message + " - " + msg;            }        }        fHandle.submit2(            "local", "LOG", "LOG GLOBAL LOGNAME " + fServiceName +            " LEVEL " + level + " MESSAGE " + STAFUtil.wrapData(message));    }    /**     *  Register error codes for the Namespace 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 Namespace Service with the HELP service     */     private void unregisterHelpData(int errorNumber)    {        STAFResult res = fHandle.submit2(            "local", "HELP", "UNREGISTER SERVICE " + fServiceName +            " ERROR " + errorNumber);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -