Class CLITool2

  • All Implemented Interfaces:
    Logable

    public abstract class CLITool2
    extends Object
    implements Logable
    Basic class for writing command line tools. This allows for verbose, debug and help options automatically. It supports logging to a file and specifying a configuration file names. In short, a lot of the random grunt work for using the commons command line toolkit is here.

    Created by Jeff Gaynor
    on 9/7/11 at 12:31 PM

    • Constructor Detail

      • CLITool2

        public CLITool2()
    • Method Detail

      • getComponentName

        public abstract String getComponentName()
        Returns the name of the configuration component (usually "server" or "client") that identifies the XML elements which might have the configuration. These are resolved by their name attribute.
        Returns:
      • setBufferedReader

        public void setBufferedReader​(BufferedReader bufferedReader)
      • getIoInterface

        public static IOInterface getIoInterface()
      • setIoInterface

        public static void setIoInterface​(IOInterface newIOInterface)
      • doIt

        public abstract void doIt()
                           throws Exception
        Does the actual work. This is where you put your executable code.
        Throws:
        Exception
      • getConfigurationNode

        protected org.apache.commons.configuration.tree.ConfigurationNode getConfigurationNode()
      • setConfigurationNode

        protected void setConfigurationNode​(org.apache.commons.configuration.tree.ConfigurationNode configurationNode)
      • initialize

        public void initialize()
                        throws Exception
        Sets up the configuration and runtime environment. This is called typically in the constructors before anything else is called. Always call super on this method if you override it! At the end of this method, the configuration node has been found and set for you to call in your getLoader() method.
        Throws:
        Exception
      • help

        public abstract void help()
        This will be automatically invoked for you if the user supplies a help option. This should just print out to the command line (using the say(String) method), rather than logging the help.
      • defaultHelp

        public static void defaultHelp​(boolean printHeader)
        Convenience method. This prints out a short message describing the built-in options for this class. Best called at the end of your custom help method. Prints a header (informational line) if you wish.
      • hasConfigFile

        public boolean hasConfigFile()
      • getConfigFile

        public String getConfigFile()
      • setConfigFile

        public void setConfigFile​(String configFile)
      • getLogfileName

        public String getLogfileName()
      • setLogfileName

        public void setLogfileName​(String logfileName)
      • isVerbose

        public boolean isVerbose()
      • setVerbose

        public void setVerbose​(boolean verbose)
      • getOptions

        protected boolean getOptions​(String[] args)
        Returns true if execution should continue, false if not. Call this first to see if execution should proceed.
        Parameters:
        args -
        Returns:
        Throws:
        Exception
      • getInputLine

        public InputLine getInputLine()
        Get the original input line.
        Returns:
      • setInputLine

        public void setInputLine​(InputLine inputLine)
      • getConfigName

        public String getConfigName()
      • setConfigName

        public void setConfigName​(String configName)
      • hasConfigName

        public boolean hasConfigName()
      • sayv

        public void sayv​(String x)
        Prints to the console only if verbose is enabled.
        Parameters:
        x -
      • say

        public static void say​(String x)
        Prints to the console.
        Parameters:
        x -
      • say2

        public void say2​(String x)
        Prints to the console WITHOUT a carriage return. Useful for imput prompts rather than informational messages.
        Parameters:
        x -
      • setMyLogger

        public void setMyLogger​(MyLoggingFacade myLoggingFacade)
      • debug

        public void debug​(String x)
        Description copied from interface: Logable
        Write a debug message to the log.
        Specified by:
        debug in interface Logable
      • isDebugOn

        public boolean isDebugOn()
        Description copied from interface: Logable
        Query if debugging is enabled for this logger.
        Specified by:
        isDebugOn in interface Logable
        Returns:
      • setDebugOn

        public void setDebugOn​(boolean setOn)
        Description copied from interface: Logable
        Enable/disable debugging for this logger
        Specified by:
        setDebugOn in interface Logable
      • info

        public void info​(String x)
        Description copied from interface: Logable
        Write an informational message to the log
        Specified by:
        info in interface Logable
      • warn

        public void warn​(String x)
        Description copied from interface: Logable
        Write a warning to the log. This indicates a severe, but non-fatal condition exists.
        Specified by:
        warn in interface Logable
      • error

        public void error​(String x)
        Description copied from interface: Logable
        Write an error message to the log. Typically this is invoked as the last command before throwing an exception.
        Specified by:
        error in interface Logable
      • run

        public void run()
                 throws Throwable
        Calls the initialize() method and then runs the main doIt() method. Call this if you set the target and source in the constructor.
        Throws:
        Throwable