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

📄 namedcounterservice.java

📁 Software Testing Automation Framework (STAF)的开发代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF)                              *//* (C) Copyright IBM Corp. 2002, 2004, 2005                                  *//*                                                                           *//* This software is licensed under the Common Public License (CPL) V1.0.     *//*****************************************************************************/package com.ibm.staf.service.namedcounter;import com.ibm.staf.*;import com.ibm.staf.service.*;import java.util.HashMap;import java.util.Map;import java.util.Iterator;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.FileNotFoundException;/** STAF Named Counter Serivce, providing a mechanism for managing * Named Counters across a testing environment. * @author Karl Donald * @version 1.1.0 */public class NamedCounterService implements STAFServiceInterfaceLevel30{    /** The STAF Handle that the service uses itself     */    private STAFHandle stafHandle;    /** Error code for a request for a counter which doesn't exist     */    static final int CounterNotExist = 4001;    /** Return code for a counter which is at Zero and as such     * can't be decremented     */    static final int CounterAlreadyZero = 4002;    /** Return code for is for when a counter hits the maximum     * value and is unable to increment     */    static final int CounterAtMax = 4003;    /** The structure in which all of the named counters are stored     */    private HashMap counters;    /** Flag to check whether to persist the named counters or not     */    private boolean persist;    /** The name that the service has been assigned     */    private String fServiceName;    /** The local machine's logical identifier     */    private String fLocalMachineName;    /** Name of the directory to which the service can write persistent data     */    private String fDataDir;    /** Fully-qualified name of file containing the service's persistent data     */    private String fNamedCounterFileName;    /** The Parser which is used to process the STAF request     */    STAFCommandParser parser = null;    private final String serviceVersion = "3.0.0";    /** Construct an instance of the NamedCounterService     */    public NamedCounterService() {        counters = new HashMap();    }    /** Loads the counters from the file that they were persisted to     */    private void loadCounters()    {        try        {            FileInputStream fi = new FileInputStream(fNamedCounterFileName);            ObjectInputStream si = new ObjectInputStream(fi);            counters = (HashMap) si.readObject();        }        catch (FileNotFoundException e1)        {            System.err.println(                "NamedCounterService:Error: Named Counters " +                "File not Found for service: " + fNamedCounterFileName);            // Not a problem, will create a new one on exit.            // The object has already been initialised        }        catch (Exception e2)        {            System.err.println(                "NamedCounterService:Error: Exception caught " +                "whilst loading counters");            System.err.println("                         : " +                e2.getMessage());            e2.printStackTrace();        }    }    /** Save the counters to a file     * @return The STAFResult object which is the result of the request     */    private boolean saveCounters()    {        boolean result = true;        try        {            FileOutputStream fo = new FileOutputStream(fNamedCounterFileName);            ObjectOutputStream so = new ObjectOutputStream(fo);            so.writeObject(counters);            so.flush();        }        catch (Exception e)        {            result = false;            System.err.println(                "NamedCounterService:Error: Exception caught " +                "whilst saving counters in file " + fNamedCounterFileName);            System.err.println("                         : " +                e.getMessage());            e.printStackTrace();        }        return result;    }    // Register error codes for the service with the HELP service    private void registerHelpData(int errorNumber, String info,                                 String description)    {        STAFResult res = stafHandle.submit2("local", "HELP",                         "REGISTER SERVICE " + fServiceName +                         " ERROR " + errorNumber +                         " INFO " + STAFUtil.wrapData(info) +                         " DESCRIPTION " + STAFUtil.wrapData(description));    }    // Un-register error codes for the service with the HELP service    private void unregisterHelpData(int errorNumber)    {        STAFResult res = stafHandle.submit2("local", "HELP",                         "UNREGISTER SERVICE " + fServiceName +                         " ERROR " + errorNumber);    }    /***************************************************************/    /* init - Initializes NamedCounter Service with STAF.          */    /* Instantiates all parsers.                                   */    /* Registers help for service error codes.                     */    /*                                                             */    /* accepts: STAFServiceInterfaceLevel30 initialization info    */    /*                                                             */    /* Returns: STAFResult.Ok or STAFResult.STAFRegistrationError  */    /***************************************************************/    public STAFResult init(STAFServiceInterfaceLevel30.InitInfo info)    {        fServiceName = info.name;        try        {            parser = new STAFCommandParser(2, false);            // Add some options that are permitted in command statements            parser.addOption("INC", 1, STAFCommandParser.VALUEREQUIRED);            parser.addOption("DEC", 1, STAFCommandParser.VALUEREQUIRED);            parser.addOption("GET", 1, STAFCommandParser.VALUEREQUIRED);            parser.addOption("RESET", 1, STAFCommandParser.VALUEREQUIRED);            parser.addOption("DELETE", 1, STAFCommandParser.VALUEREQUIRED);            parser.addOption("LIST", 1, STAFCommandParser.VALUENOTALLOWED);            parser.addOption("HELP", 1, STAFCommandParser.VALUENOTALLOWED);            parser.addOption("VERSION", 1, STAFCommandParser.VALUENOTALLOWED);            // At least one and only one of these must appear            parser.addOptionGroup("INC DEC GET RESET DELETE LIST HELP VERSION",                1, 1);            // Get a STAFHandle for NamedCounterService <name>            stafHandle = new STAFHandle("STAF/Service/" + fServiceName);            STAFResult persistRes =                stafHandle.submit2(                    "local",                    "var",                    "get system var STAF/Service/" + fServiceName + "/Persist");            if ((persistRes.rc == 0)                & (persistRes.result.toUpperCase().equals("TRUE")))            {                persist = true;                // Get the file separator for the local machine                String fileSep = "/";                STAFResult res = STAFUtil.resolveInitVar(                    "{STAF/Config/Sep/File}", stafHandle);                if (res.rc == STAFResult.Ok) fileSep = res.result;                // Store data for the service in directory:                //   <STAF writeLocation>/service/<service name (lower-case)>                fDataDir = info.writeLocation;                if (!fDataDir.endsWith(fileSep))                {                    fDataDir += fileSep;                }                fDataDir = fDataDir + "service" + fileSep +                    fServiceName.toLowerCase();                File dir = new File(fDataDir);                if (!dir.exists())                {                    dir.mkdirs();                }                fNamedCounterFileName = fDataDir + fileSep + "namedCounter.ser";                loadCounters();            }            else            {                persist = false;            }            // Resolve the machine name variable for the local machine            STAFResult res = STAFUtil.resolveInitVar(                "{STAF/Config/Machine}", stafHandle);            if (res.rc != STAFResult.Ok) return res;            fLocalMachineName = res.result;            // Register Help Data            registerHelpData(                CounterNotExist,                "Counter Does Not Exist",                "The specified named counter could not be found.");            registerHelpData(                CounterAlreadyZero,                "Counter Already Zero",                "The named counter is currently at Zero.");            registerHelpData(                CounterAtMax,                "Counter At Max",                "The named counter is at its maximum possible value.");            return new STAFResult(STAFResult.Ok);        }        catch (STAFException e)        {            System.err.println(                "NamedCounterService:Error: Exception caught " +                "whilst initializing service");            System.err.println("                         : " +                e.getMessage());            e.printStackTrace();            return new STAFResult(STAFResult.STAFRegistrationError,                                  e.toString());        }    }    /** Process a HELP request     * @param myParsedRequest The pre-parsed result     * @return The STAFResult object which is the result of the request     */    private STAFResult optionHELP(STAFCommandParseResult myParsedRequest)    {        return new STAFResult(            STAFResult.Ok,            "NamedCounter Service Help\n\n"                + "INC <Name>\n"                + "DEC <Name>\n"                + "GET <Name>\n"                + "RESET <Name>\n"                + "DELETE <Name>\n"                + "LIST\n"                + "VERSION\n"                + "HELP");    }    /** Process a VERSION request     * @param myParsedRequest The pre-parsed result     * @return The STAFResult object which is the result of the request     */    private STAFResult optionVERSION(STAFCommandParseResult myParsedRequest)    {        return new STAFResult(STAFResult.Ok, serviceVersion);    }    /** Process a GET request     * @param myParsedRequest The pre-parsed result     * @return The STAFResult object which is the result of the request     */    private STAFResult optionGET(STAFCommandParseResult myParsedRequest)

⌨️ 快捷键说明

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