Package edu.uiuc.ncsa.security.util.cli
Class CLITool
- java.lang.Object
-
- edu.uiuc.ncsa.security.util.cli.CLITool
-
- All Implemented Interfaces:
Logable
- Direct Known Subclasses:
StoreUtil
public abstract class CLITool 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
-
-
Field Summary
Fields Modifier and Type Field Description static String
CONFIG_FILE_LONG_OPTION
static String
CONFIG_FILE_OPTION
static int
CONFIG_NAME_ACTION
static String
CONFIG_NAME_LONG_OPTION
static String
CONFIG_NAME_OPTION
static String
DEBUG_LONG_OPTION
static String
DEBUG_OPTION
static String
DEFAULT_LOG_FILE
static String
HELP_LONG_OPTION
static String
HELP_OPTION
static String
LOG_FILE_LONG_OPTION
static String
LOG_FILE_OPTION
static int
NO_ACTION
The default action for this tool at startup.static String
VERBOSE_LONG_OPTION
static String
VERBOSE_OPTION
-
Constructor Summary
Constructors Constructor Description CLITool()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
checkAction(String flag, String longFlag, int action)
Converts command line arguments into integer action values.void
debug(String x)
Write a debug message to the log.static void
defaultHelp(boolean printHeader)
Convenience method.abstract void
doIt()
Does the actual work.void
error(String x)
Write an error message to the log.void
error(String x, Throwable t)
int
getAction()
This application allows you to specify numeric values that determine the behavior of this application.BufferedReader
getBufferedReader()
org.apache.commons.cli.CommandLine
getCommandLine()
abstract String
getComponentName()
Returns the name of the configuration component (usually "server" or "client") that identifies the XML elements which might have the configuration.String
getConfigFile()
protected org.apache.commons.configuration.tree.ConfigurationNode
getConfigurationNode()
AbstractEnvironment
getEnvironment()
static IOInterface
getIoInterface()
abstract ConfigurationLoader<? extends AbstractEnvironment>
getLoader()
String
getLogfileName()
MyLoggingFacade
getMyLogger()
protected org.apache.commons.cli.Options
getOptions()
Override this to set up your options.protected boolean
getOptions(String[] args)
Returns true if execution should continue, false if not.protected boolean
hasOption(String shortForm, String longForm)
Checks if the long or short form is part of the command line options.abstract void
help()
This will be automatically invoked for you if the user supplies a help option.void
info(String x)
Write an informational message to the logvoid
initialize()
Sets up the configuration and runtime environment.boolean
isDebugOn()
Query if debugging is enabled for this logger.boolean
isVerbose()
protected void
parseCommandLine(String[] args)
protected String
readline()
void
run()
Calls theinitialize()
method and then runs the maindoIt()
method.void
run(String[] args)
Main call.static void
say(String x)
Prints to the console.void
say2(String x)
Prints to the console WITHOUT a carriage return.void
sayv(String x)
Prints to the console only if verbose is enabled.void
setAction(int action)
void
setBufferedReader(BufferedReader bufferedReader)
void
setCommandLine(String[] args)
So you can construct the argument list and have this tool use its built in configuration.void
setConfigFile(String configFile)
protected void
setConfigurationNode(org.apache.commons.configuration.tree.ConfigurationNode configurationNode)
void
setDebugOn(boolean setOn)
Enable/disable debugging for this loggervoid
setEnvironment(AbstractEnvironment environment)
static void
setIoInterface(IOInterface newIOInterface)
void
setLogfileName(String logfileName)
void
setMyLogger(MyLoggingFacade myLoggingFacade)
void
setVerbose(boolean verbose)
void
warn(String x)
Write a warning to the log.
-
-
-
Field Detail
-
VERBOSE_OPTION
public static final String VERBOSE_OPTION
- See Also:
- Constant Field Values
-
VERBOSE_LONG_OPTION
public static final String VERBOSE_LONG_OPTION
- See Also:
- Constant Field Values
-
DEBUG_OPTION
public static final String DEBUG_OPTION
- See Also:
- Constant Field Values
-
DEBUG_LONG_OPTION
public static final String DEBUG_LONG_OPTION
- See Also:
- Constant Field Values
-
LOG_FILE_OPTION
public static final String LOG_FILE_OPTION
- See Also:
- Constant Field Values
-
LOG_FILE_LONG_OPTION
public static final String LOG_FILE_LONG_OPTION
- See Also:
- Constant Field Values
-
HELP_OPTION
public static final String HELP_OPTION
- See Also:
- Constant Field Values
-
HELP_LONG_OPTION
public static final String HELP_LONG_OPTION
- See Also:
- Constant Field Values
-
CONFIG_FILE_OPTION
public static final String CONFIG_FILE_OPTION
- See Also:
- Constant Field Values
-
CONFIG_FILE_LONG_OPTION
public static final String CONFIG_FILE_LONG_OPTION
- See Also:
- Constant Field Values
-
DEFAULT_LOG_FILE
public static final String DEFAULT_LOG_FILE
- See Also:
- Constant Field Values
-
CONFIG_NAME_OPTION
public static final String CONFIG_NAME_OPTION
- See Also:
- Constant Field Values
-
CONFIG_NAME_LONG_OPTION
public static final String CONFIG_NAME_LONG_OPTION
- See Also:
- Constant Field Values
-
CONFIG_NAME_ACTION
public static final int CONFIG_NAME_ACTION
- See Also:
- Constant Field Values
-
NO_ACTION
public static final int NO_ACTION
The default action for this tool at startup. Do nothing.- See Also:
- Constant Field Values
-
-
Method Detail
-
getEnvironment
public AbstractEnvironment getEnvironment() throws Exception
- Throws:
Exception
-
setEnvironment
public void setEnvironment(AbstractEnvironment environment)
-
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:
-
getLoader
public abstract ConfigurationLoader<? extends AbstractEnvironment> getLoader() throws Exception
- Throws:
Exception
-
getBufferedReader
public BufferedReader getBufferedReader()
-
setBufferedReader
public void setBufferedReader(BufferedReader bufferedReader)
-
readline
protected String readline() throws IOException
- Throws:
IOException
-
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 yourgetLoader()
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 thesay(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.
-
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) throws Exception
Returns true if execution should continue, false if not. Call this first to see if execution should proceed.- Parameters:
args
-- Returns:
- Throws:
Exception
-
hasOption
protected boolean hasOption(String shortForm, String longForm)
Checks if the long or short form is part of the command line options.- Parameters:
shortForm
-longForm
-- Returns:
-
parseCommandLine
protected void parseCommandLine(String[] args)
-
setCommandLine
public void setCommandLine(String[] args)
So you can construct the argument list and have this tool use its built in configuration.- Parameters:
args
-
-
getCommandLine
public org.apache.commons.cli.CommandLine getCommandLine()
-
getAction
public int getAction()
This application allows you to specify numeric values that determine the behavior of this application. You may access the result in a switch statement. You set them in thegetOptions(String[])
method and access them in thedoIt()
method. Use of this facility, is optional.- Returns:
-
setAction
public void setAction(int action)
-
checkAction
protected void checkAction(String flag, String longFlag, int action)
Converts command line arguments into integer action values. This makes writing switch statements for choosing actions very easy. Pass in the short and long options, the action that should be assigned and the command line. If the command option is present, the action will be set.The only supplied default is
NO_ACTION
- Parameters:
flag
-longFlag
-action
-
-
getOptions
protected org.apache.commons.cli.Options getOptions()
Override this to set up your options. You should also use this to check the action and set it, .e.g.,
Options options = super.getOptions();
options.addOptions(SHORT_FORM, LONG_FORM, ... // This is in the commons documentation
checkAction(SHORT_FORM, LONG_FORM, ACTION_CODE);
which would set an integer value suitable for using with thegetAction()
call in a switch statement.- Returns:
-
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)
-
getMyLogger
public MyLoggingFacade getMyLogger()
-
debug
public void debug(String x)
Description copied from interface:Logable
Write a debug message to the log.
-
isDebugOn
public boolean isDebugOn()
Description copied from interface:Logable
Query if debugging is enabled for this logger.
-
setDebugOn
public void setDebugOn(boolean setOn)
Description copied from interface:Logable
Enable/disable debugging for this logger- Specified by:
setDebugOn
in interfaceLogable
-
info
public void info(String x)
Description copied from interface:Logable
Write an informational message to the log
-
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.
-
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.
-
run
public void run() throws Throwable
Calls theinitialize()
method and then runs the maindoIt()
method. Call this if you set the target and source in the constructor.- Throws:
Throwable
-
run
public void run(String[] args) throws Throwable
Main call. This will grab the command line arguments, runinitialize()
and then invokedoIt()
.- Parameters:
args
-- Throws:
Throwable
-
-