Package adams.core.command
Class AbstractExternalCommand
- java.lang.Object
-
- adams.core.logging.LoggingObject
-
- adams.core.logging.CustomLoggingLevelObject
-
- adams.core.option.AbstractOptionHandler
-
- adams.core.command.AbstractExternalCommand
-
- All Implemented Interfaces:
CleanUpHandler
,ExternalCommand
,Destroyable
,GlobalInfoSupporter
,LoggingLevelHandler
,LoggingSupporter
,OptionHandler
,QuickInfoSupporter
,SizeOfHandler
,Stoppable
,StoppableWithFeedback
,FlowContextHandler
,Serializable
- Direct Known Subclasses:
AbstractAsyncCapableExternalCommand
,AbstractExternalCommandWithOptions
public abstract class AbstractExternalCommand extends AbstractOptionHandler implements ExternalCommand
Ancestor for external commands.- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
m_Executed
the command was executed.protected Actor
m_FlowContext
the flow context.protected String[]
m_LastCommand
the last command that was executed.protected List
m_Output
for buffering output.protected OutputFormatter
m_OutputFormatter
the output formatter in use.protected OutputType
m_OutputType
the type of output to forward.protected boolean
m_Running
whether the command is still running.protected StdErrProcessor
m_StdErrProcessor
the handler for processing output on stderr.protected StdOutProcessor
m_StdOutProcessor
the handler for processing output on stderr.protected boolean
m_Stopped
whether the execution was stopped.-
Fields inherited from class adams.core.option.AbstractOptionHandler
m_OptionManager
-
Fields inherited from class adams.core.logging.LoggingObject
m_Logger, m_LoggingIsEnabled, m_LoggingLevel
-
-
Constructor Summary
Constructors Constructor Description AbstractExternalCommand()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addFormattedOutput(Object output)
Gets called by the output formatter class.protected List<String>
buildCommand()
Assembles the command to run.protected String
check()
Hook method for performing checks before executing the command.void
cleanUp()
Cleans up data structures, frees up memory.protected String
commandResultToError(CommandResult res)
Generates an error message from the command result.void
defineOptions()
Adds options to the internal list of options.protected Object
doAsyncExecute()
Executes the command in asynchronous fashion.protected Object
doBlockingExecute()
Executes the command in blocking fashion (ie waits till it finishes).String
execute()
Executes the command.Class
generates()
Returns what output type the command generates via its output formatter.protected OutputFormatter
getDefaultOutputFormatter()
Returns the default output formatter.protected OutputType
getDefaultOutputType()
Returns the default output type.protected StdErrProcessor
getDefaultStdErrProcessor()
Returns the default handler for processing output on stderr.protected StdOutProcessor
getDefaultStdOutProcessor()
Returns the default handler for processing output on stdout.Actor
getFlowContext()
Returns the flow context, if any.String[]
getLastCommand()
Returns the last command that was executed.OutputFormatter
getOutputFormatter()
Returns the formatter for the output that is being forwarded.OutputType
getOutputType()
Returns the type of output to forward.String
getQuickInfo()
Returns a quick info about the object, which can be displayed in the GUI.StdErrProcessor
getStdErrProcessor()
Returns the handler for processing the output received on stderr.StdOutProcessor
getStdOutProcessor()
Returns the handler for processing the output received on stdout.boolean
hasLastCommand()
Checks whether a command has been executed (and recorded).boolean
hasOutput()
Whether there is any pending output.protected void
initialize()
Initializes the members.boolean
isExecuted()
Returns whether the command was executed.boolean
isFinished()
Returns whether the command finished.boolean
isRunning()
Returns whether the command is currently running.boolean
isStopped()
Whether the execution has been stopped.protected void
log(CommandResult res)
For logging the result of a command.protected void
log(String[] cmd)
Logs the command for execution.protected void
log(List<String> cmd)
Logs the command for execution.Object
output()
Returns the next line in the output.String
outputFormatterTipText()
Returns the tip text for this property.String
outputTypeTipText()
Returns the tip text for this property.protected void
reset()
Resets the scheme.void
setFlowContext(Actor value)
Sets the flow context.void
setOutputFormatter(OutputFormatter value)
Sets the formatter for the output that is being forwarded.void
setOutputType(OutputType value)
Sets the type of output to forward.void
setStdErrProcessor(StdErrProcessor value)
Sets the handler for processing the output received on stderr.void
setStdOutProcessor(StdOutProcessor value)
Sets the handler for processing the output received on stdout.String
stdErrProcessorTipText()
Returns the tip text for this property.String
stdOutProcessorTipText()
Returns the tip text for this property.void
stopExecution()
Stops the execution.-
Methods inherited from class adams.core.option.AbstractOptionHandler
cleanUpOptions, destroy, finishInit, getDefaultLoggingLevel, getOptionManager, globalInfo, loggingLevelTipText, newOptionManager, setLoggingLevel, toCommandLine, toString
-
Methods inherited from class adams.core.logging.LoggingObject
configureLogger, getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled, sizeOf
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface adams.core.Destroyable
destroy
-
Methods inherited from interface adams.core.command.ExternalCommand
isUsingBlocking
-
Methods inherited from interface adams.core.logging.LoggingLevelHandler
getLoggingLevel
-
Methods inherited from interface adams.core.option.OptionHandler
cleanUpOptions, getOptionManager, toCommandLine
-
-
-
-
Field Detail
-
m_OutputType
protected OutputType m_OutputType
the type of output to forward.
-
m_StdErrProcessor
protected StdErrProcessor m_StdErrProcessor
the handler for processing output on stderr.
-
m_StdOutProcessor
protected StdOutProcessor m_StdOutProcessor
the handler for processing output on stderr.
-
m_OutputFormatter
protected OutputFormatter m_OutputFormatter
the output formatter in use.
-
m_Executed
protected boolean m_Executed
the command was executed.
-
m_Running
protected boolean m_Running
whether the command is still running.
-
m_Stopped
protected boolean m_Stopped
whether the execution was stopped.
-
m_Output
protected List m_Output
for buffering output.
-
m_FlowContext
protected Actor m_FlowContext
the flow context.
-
m_LastCommand
protected transient String[] m_LastCommand
the last command that was executed.
-
-
Method Detail
-
defineOptions
public void defineOptions()
Adds options to the internal list of options.- Specified by:
defineOptions
in interfaceOptionHandler
- Overrides:
defineOptions
in classAbstractOptionHandler
-
initialize
protected void initialize()
Initializes the members.- Overrides:
initialize
in classAbstractOptionHandler
-
reset
protected void reset()
Resets the scheme.- Overrides:
reset
in classAbstractOptionHandler
-
getDefaultOutputType
protected OutputType getDefaultOutputType()
Returns the default output type.- Returns:
- the default
-
setOutputType
public void setOutputType(OutputType value)
Sets the type of output to forward.- Specified by:
setOutputType
in interfaceExternalCommand
- Parameters:
value
- the type
-
getOutputType
public OutputType getOutputType()
Returns the type of output to forward.- Specified by:
getOutputType
in interfaceExternalCommand
- Returns:
- the type
-
outputTypeTipText
public String outputTypeTipText()
Returns the tip text for this property.- Specified by:
outputTypeTipText
in interfaceExternalCommand
- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
getDefaultOutputFormatter
protected OutputFormatter getDefaultOutputFormatter()
Returns the default output formatter.- Returns:
- the default
-
setOutputFormatter
public void setOutputFormatter(OutputFormatter value)
Sets the formatter for the output that is being forwarded.- Specified by:
setOutputFormatter
in interfaceExternalCommand
- Parameters:
value
- the formatter
-
getOutputFormatter
public OutputFormatter getOutputFormatter()
Returns the formatter for the output that is being forwarded.- Specified by:
getOutputFormatter
in interfaceExternalCommand
- Returns:
- the formatter
-
outputFormatterTipText
public String outputFormatterTipText()
Returns the tip text for this property.- Specified by:
outputFormatterTipText
in interfaceExternalCommand
- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
getDefaultStdOutProcessor
protected StdOutProcessor getDefaultStdOutProcessor()
Returns the default handler for processing output on stdout.- Returns:
- the handler
-
setStdOutProcessor
public void setStdOutProcessor(StdOutProcessor value)
Sets the handler for processing the output received on stdout.- Specified by:
setStdOutProcessor
in interfaceExternalCommand
- Parameters:
value
- the handler
-
getStdOutProcessor
public StdOutProcessor getStdOutProcessor()
Returns the handler for processing the output received on stdout.- Specified by:
getStdOutProcessor
in interfaceExternalCommand
- Returns:
- the handler
-
stdOutProcessorTipText
public String stdOutProcessorTipText()
Returns the tip text for this property.- Specified by:
stdOutProcessorTipText
in interfaceExternalCommand
- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
getDefaultStdErrProcessor
protected StdErrProcessor getDefaultStdErrProcessor()
Returns the default handler for processing output on stderr.- Returns:
- the handler
-
setStdErrProcessor
public void setStdErrProcessor(StdErrProcessor value)
Sets the handler for processing the output received on stderr.- Specified by:
setStdErrProcessor
in interfaceExternalCommand
- Parameters:
value
- the handler
-
getStdErrProcessor
public StdErrProcessor getStdErrProcessor()
Returns the handler for processing the output received on stderr.- Specified by:
getStdErrProcessor
in interfaceExternalCommand
- Returns:
- the handler
-
stdErrProcessorTipText
public String stdErrProcessorTipText()
Returns the tip text for this property.- Specified by:
stdErrProcessorTipText
in interfaceExternalCommand
- Returns:
- tip text for this property suitable for displaying in the GUI or for listing the options.
-
getQuickInfo
public String getQuickInfo()
Returns a quick info about the object, which can be displayed in the GUI.
Default implementation returns null.- Specified by:
getQuickInfo
in interfaceQuickInfoSupporter
- Returns:
- null if no info available, otherwise short string
-
setFlowContext
public void setFlowContext(Actor value)
Sets the flow context.- Specified by:
setFlowContext
in interfaceFlowContextHandler
- Parameters:
value
- the actor
-
getFlowContext
public Actor getFlowContext()
Returns the flow context, if any.- Specified by:
getFlowContext
in interfaceFlowContextHandler
- Returns:
- the actor, null if none available
-
check
protected String check()
Hook method for performing checks before executing the command.- Returns:
- null if successful, otherwise error message
-
log
protected void log(String[] cmd)
Logs the command for execution.- Parameters:
cmd
- the command
-
log
protected void log(List<String> cmd)
Logs the command for execution.- Parameters:
cmd
- the command
-
log
protected void log(CommandResult res)
For logging the result of a command.- Parameters:
res
- the result to log
-
commandResultToError
protected String commandResultToError(CommandResult res)
Generates an error message from the command result.- Parameters:
res
- the result to turn into an error message- Returns:
- the error message
-
buildCommand
protected List<String> buildCommand()
Assembles the command to run.- Returns:
- the command
-
doAsyncExecute
protected Object doAsyncExecute()
Executes the command in asynchronous fashion. Async commands must set them_Running
flag to false themselves.- Returns:
- the result of the command, either a CommandResult or a String object (= error message)
-
doBlockingExecute
protected Object doBlockingExecute()
Executes the command in blocking fashion (ie waits till it finishes). Them_Running
flag is set to false automatically.- Returns:
- the result of the command, either a CommandResult or a String object (= error message)
-
execute
public String execute()
Executes the command.- Specified by:
execute
in interfaceExternalCommand
- Returns:
- null if successful, otherwise error message
-
addFormattedOutput
public void addFormattedOutput(Object output)
Gets called by the output formatter class.- Specified by:
addFormattedOutput
in interfaceExternalCommand
- Parameters:
output
- the formatted output to collect
-
hasLastCommand
public boolean hasLastCommand()
Checks whether a command has been executed (and recorded).- Specified by:
hasLastCommand
in interfaceExternalCommand
- Returns:
- true if executed/recorded
-
getLastCommand
public String[] getLastCommand()
Returns the last command that was executed.- Specified by:
getLastCommand
in interfaceExternalCommand
- Returns:
- the last command, null if not available
-
isExecuted
public boolean isExecuted()
Returns whether the command was executed.- Specified by:
isExecuted
in interfaceExternalCommand
- Returns:
- true if executed
-
isRunning
public boolean isRunning()
Returns whether the command is currently running.- Specified by:
isRunning
in interfaceExternalCommand
- Returns:
- true if running
-
isFinished
public boolean isFinished()
Returns whether the command finished.- Specified by:
isFinished
in interfaceExternalCommand
- Returns:
- true if finished
-
hasOutput
public boolean hasOutput()
Whether there is any pending output.- Specified by:
hasOutput
in interfaceExternalCommand
- Returns:
- true if output pending
-
output
public Object output()
Returns the next line in the output.- Specified by:
output
in interfaceExternalCommand
- Returns:
- the line, null if none available
-
stopExecution
public void stopExecution()
Stops the execution.- Specified by:
stopExecution
in interfaceStoppable
-
isStopped
public boolean isStopped()
Whether the execution has been stopped.- Specified by:
isStopped
in interfaceStoppableWithFeedback
- Returns:
- true if stopped
-
generates
public Class generates()
Returns what output type the command generates via its output formatter.- Specified by:
generates
in interfaceExternalCommand
- Returns:
- the type
-
cleanUp
public void cleanUp()
Cleans up data structures, frees up memory.- Specified by:
cleanUp
in interfaceCleanUpHandler
- Specified by:
cleanUp
in interfaceExternalCommand
-
-