Package edu.uiuc.ncsa.security.util.cli
Class CommonCommands
- java.lang.Object
-
- edu.uiuc.ncsa.security.util.cli.CommonCommands
-
- Direct Known Subclasses:
ParserCommands
,StoreCommands
public abstract class CommonCommands extends Object implements Commands
Utilities that are used by any reasonable implementation of the Commands interface. You will probably want to extend this for your command processor.Created by Jeff Gaynor
on 10/30/13 at 4:14 PM
-
-
Field Summary
Fields Modifier and Type Field Description static String
BATCH_FILE_LINE_CONTINUES
If a line ends with this (after the comment is removed), then glow it on to the next input line.static String
BATCH_FILE_MODE_FLAG
static String
BATCH_MODE_FLAG
protected boolean
batchFile
protected String
CL_INPUT_FILE_FLAG
protected String
CL_OUTPUT_FILE_FLAG
protected static String
DEFAULT_NULL_VALUE_PLACEHOLDER
protected String
defaultIndent
static String
INDENT
protected MyLoggingFacade
logger
String
placeHolder
This is used wherever a missing value is.
-
Constructor Summary
Constructors Modifier Constructor Description protected
CommonCommands(MyLoggingFacade logger)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
bootstrap()
void
clear_env(InputLine inputLine)
protected void
clearEnvHelp()
void
debug(String x)
Write a debug message to the log.void
echo(InputLine inputLine)
protected void
echoHelp()
void
error(String x)
Write an error message to the log.void
get_env(InputLine inputline)
BufferedReader
getBufferedReader()
CLIDriver
getDriver()
HelpUtil
getHelpUtil()
protected String
getInput(String prompt, String defaultValue)
Creates the input prompt and shows the supplied default value.IOInterface
getIOInterface()
String
getPlaceHolder()
Gets the placeholder for missing values.protected String
getPropertyHelp(String propertyName, String prompt, String defaultValue)
Use this to get a property and also allow for getting help.protected String
getValue(String x)
Returns the value if it is not empty of a placeholder if it is.protected boolean
gracefulExit(boolean exitNow, String msg)
Exit gracefully.int
indentWidth()
void
info(String x)
Write an informational message to the logprotected boolean
isBatch()
returns true if this is either a batch file or in batch mode.boolean
isBatchFile()
boolean
isBatchMode()
If this is invoked from the command line with the batch mode flag, then this should process commands strictly without user intervention, failing if, for instance, some parameters are missing rather than prompting for them.boolean
isDebugOn()
Query if debugging is enabled for this logger.protected boolean
isEmpty(String x)
protected boolean
isOk(String x)
boolean
isPrintOuput()
If this is set true, then no output is generated.boolean
isVerbose()
protected void
logit(String x)
void
print_env(InputLine inputLine)
void
print_help()
protected void
printEnvHelp()
protected void
printGetEnvHelp()
protected void
printReadEnvHelp()
protected void
printSaveEnvHelp()
protected void
printSetEnvHelp()
void
read_env(InputLine inputline)
protected String
readFile(String filename)
read a text file and return a single string of the content.protected net.sf.json.JSONObject
readJSON(String filename)
protected String
readline()
protected String
readline(String prompt)
void
save_env(InputLine inputLine)
protected void
say()
Linefeed.protected void
say(String x)
Prints with the default indent and a linefeed.protected void
say2(String x)
Output the string without any linefeed.protected void
sayi(String x)
prints with the current indent and a linefeed.protected void
sayii(String x)
Double indent -- useful for lists.protected void
sayv(String x)
Use this for verbose mode.void
set_env(InputLine inputline)
void
set_output_on(InputLine inputLine)
void
set_verbose_on(InputLine inputLine)
So batch files can change whether or not they are verbosevoid
setBatchFile(boolean batchFile)
void
setBatchMode(boolean batchMode)
void
setBufferedReader(BufferedReader bufferedReader)
void
setDebugOn(boolean setOn)
Enable/disable debugging for this loggervoid
setDriver(CLIDriver driver)
void
setHelpUtil(HelpUtil helpUtil)
void
setIOInterface(IOInterface ioInterface)
protected void
setOutputOnHelp()
void
setPlaceHolder(String placeHolder)
void
setPrintOuput(boolean printOuput)
void
setVerbose(boolean verbose)
protected void
setVerboseHelp()
protected boolean
showHelp(InputLine inputLine)
returns "true if the command has the flag --help in it.void
version(InputLine inputLine)
protected void
versionHelp()
void
warn(String x)
Write a warning to the log.
-
-
-
Field Detail
-
BATCH_MODE_FLAG
public static String BATCH_MODE_FLAG
-
logger
protected MyLoggingFacade logger
-
defaultIndent
protected String defaultIndent
-
INDENT
public static final String INDENT
- See Also:
- Constant Field Values
-
DEFAULT_NULL_VALUE_PLACEHOLDER
protected static String DEFAULT_NULL_VALUE_PLACEHOLDER
-
placeHolder
public String placeHolder
This is used wherever a missing value is.
-
CL_OUTPUT_FILE_FLAG
protected String CL_OUTPUT_FILE_FLAG
-
CL_INPUT_FILE_FLAG
protected String CL_INPUT_FILE_FLAG
-
BATCH_FILE_MODE_FLAG
public static String BATCH_FILE_MODE_FLAG
-
BATCH_FILE_LINE_CONTINUES
public static String BATCH_FILE_LINE_CONTINUES
If a line ends with this (after the comment is removed), then glow it on to the next input line. In effect this lets you split commands across multiple lines, e.g.ls \//My comment -la \ foobar
is the same as entering the single linels -la foobar
Notice that the lines are concatenated and the comment is stripped out.
-
batchFile
protected boolean batchFile
-
-
Constructor Detail
-
CommonCommands
protected CommonCommands(MyLoggingFacade logger) throws Throwable
- Throws:
Throwable
-
-
Method Detail
-
isBatchMode
public boolean isBatchMode()
If this is invoked from the command line with the batch mode flag, then this should process commands strictly without user intervention, failing if, for instance, some parameters are missing rather than prompting for them. Typically, theCLIDriver
instance sets this at the time of invocation.- Returns:
-
setBatchMode
public void setBatchMode(boolean batchMode)
-
debug
public void debug(String x)
Description copied from interface:Logable
Write a debug message to the log.
-
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.
-
info
public void info(String x)
Description copied from interface:Logable
Write an informational 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
-
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.
-
indentWidth
public int indentWidth()
-
getBufferedReader
public BufferedReader getBufferedReader()
-
setBufferedReader
public void setBufferedReader(BufferedReader bufferedReader)
-
getIOInterface
public IOInterface getIOInterface()
- Specified by:
getIOInterface
in interfaceCommands
-
setIOInterface
public void setIOInterface(IOInterface ioInterface)
- Specified by:
setIOInterface
in interfaceCommands
-
readline
protected String readline(String prompt) throws IOException
- Throws:
IOException
-
readline
protected String readline()
-
print_help
public void print_help() throws Exception
- Specified by:
print_help
in interfaceCommands
- Throws:
Exception
-
say
protected void say(String x)
Prints with the default indent and a linefeed.- Parameters:
x
-
-
say
protected void say()
Linefeed.
-
sayi
protected void sayi(String x)
prints with the current indent and a linefeed.- Parameters:
x
-
-
sayii
protected void sayii(String x)
Double indent -- useful for lists.- Parameters:
x
-
-
say2
protected void say2(String x)
Output the string without any linefeed. This is used for prompts.- Parameters:
x
-
-
showHelp
protected boolean showHelp(InputLine inputLine)
returns "true if the command has the flag --help in it. This is a cue from the user to show the help for a given function. So it the function is called "X" and its help is in the function "showXHelp" then a value of true from this should simply invoke "showXHelp" and return.- Parameters:
inputLine
-- Returns:
-
isOk
protected boolean isOk(String x)
-
getInput
protected String getInput(String prompt, String defaultValue) throws IOException
Creates the input prompt and shows the supplied default value. This returns the default if the default value is chosen and the input value otherwise. If supplied the default value is a null, then this is shown too.- Parameters:
prompt
-defaultValue
-- Returns:
- Throws:
IOException
-
getPropertyHelp
protected String getPropertyHelp(String propertyName, String prompt, String defaultValue) throws IOException
Use this to get a property and also allow for getting help. This is justgetInput(String, String)
but with the optional lookup of help for the propertyName if the user enters --help or /help. It will then re-prompt for the input.- Parameters:
propertyName
-prompt
-defaultValue
-- Returns:
- Throws:
IOException
-
isEmpty
protected boolean isEmpty(String x)
-
getPlaceHolder
public String getPlaceHolder()
Gets the placeholder for missing values. E.g. if a value (like a last name) is missing this will be displayed.- Returns:
-
setPlaceHolder
public void setPlaceHolder(String placeHolder)
-
getValue
protected String getValue(String x)
Returns the value if it is not empty of a placeholder if it is.- Parameters:
x
-- Returns:
-
getDriver
public CLIDriver getDriver()
-
setDriver
public void setDriver(CLIDriver driver)
-
printReadEnvHelp
protected void printReadEnvHelp()
-
printSetEnvHelp
protected void printSetEnvHelp()
-
printGetEnvHelp
protected void printGetEnvHelp()
-
printSaveEnvHelp
protected void printSaveEnvHelp()
-
printEnvHelp
protected void printEnvHelp()
-
clearEnvHelp
protected void clearEnvHelp()
-
gracefulExit
protected boolean gracefulExit(boolean exitNow, String msg)
Exit gracefully. That is to say, if the exitNow flag is true, do a system shutdown in batch mode and otherwise, do nothing. If the flag is true and the mode is interactive, then print the message and return true, prompting the system to return;- Parameters:
exitNow
-msg
-- Returns:
-
isBatchFile
public boolean isBatchFile()
-
setBatchFile
public void setBatchFile(boolean batchFile)
-
isBatch
protected boolean isBatch()
returns true if this is either a batch file or in batch mode. In that case only output is generated on errors if verbosity is on.- Returns:
-
sayv
protected void sayv(String x)
Use this for verbose mode.- Parameters:
x
-
-
logit
protected void logit(String x)
-
isVerbose
public boolean isVerbose()
-
setVerboseHelp
protected void setVerboseHelp()
-
set_verbose_on
public void set_verbose_on(InputLine inputLine) throws Exception
So batch files can change whether or not they are verbose- Parameters:
inputLine
-- Throws:
Exception
-
setVerbose
public void setVerbose(boolean verbose)
-
isPrintOuput
public boolean isPrintOuput()
If this is set true, then no output is generated. This is usedul in batch mode or with a batch file.- Returns:
-
setPrintOuput
public void setPrintOuput(boolean printOuput)
-
setOutputOnHelp
protected void setOutputOnHelp()
-
versionHelp
protected void versionHelp()
-
version
public void version(InputLine inputLine)
-
echoHelp
protected void echoHelp()
-
echo
public void echo(InputLine inputLine)
-
readFile
protected String readFile(String filename) throws Exception
read a text file and return a single string of the content. This is intended for small files which will be processed into something else (e.g. turned into a JSON object, key set etc.)- Parameters:
filename
-- Returns:
- Throws:
Exception
-
readJSON
protected net.sf.json.JSONObject readJSON(String filename) throws Exception
- Throws:
Exception
-
getHelpUtil
public HelpUtil getHelpUtil()
- Specified by:
getHelpUtil
in interfaceCommands
-
setHelpUtil
public void setHelpUtil(HelpUtil helpUtil)
-
-