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 booleanm_Executedthe command was executed.protected Actorm_FlowContextthe flow context.protected String[]m_LastCommandthe last command that was executed.protected Listm_Outputfor buffering output.protected OutputFormatterm_OutputFormatterthe output formatter in use.protected OutputTypem_OutputTypethe type of output to forward.protected booleanm_Runningwhether the command is still running.protected StdErrProcessorm_StdErrProcessorthe handler for processing output on stderr.protected StdOutProcessorm_StdOutProcessorthe handler for processing output on stderr.protected booleanm_Stoppedwhether 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 voidaddFormattedOutput(Object output)Gets called by the output formatter class.protected List<String>buildCommand()Assembles the command to run.protected Stringcheck()Hook method for performing checks before executing the command.voidcleanUp()Cleans up data structures, frees up memory.protected StringcommandResultToError(CommandResult res)Generates an error message from the command result.voiddefineOptions()Adds options to the internal list of options.protected ObjectdoAsyncExecute()Executes the command in asynchronous fashion.protected ObjectdoBlockingExecute()Executes the command in blocking fashion (ie waits till it finishes).Stringexecute()Executes the command.Classgenerates()Returns what output type the command generates via its output formatter.protected OutputFormattergetDefaultOutputFormatter()Returns the default output formatter.protected OutputTypegetDefaultOutputType()Returns the default output type.protected StdErrProcessorgetDefaultStdErrProcessor()Returns the default handler for processing output on stderr.protected StdOutProcessorgetDefaultStdOutProcessor()Returns the default handler for processing output on stdout.ActorgetFlowContext()Returns the flow context, if any.String[]getLastCommand()Returns the last command that was executed.OutputFormattergetOutputFormatter()Returns the formatter for the output that is being forwarded.OutputTypegetOutputType()Returns the type of output to forward.StringgetQuickInfo()Returns a quick info about the object, which can be displayed in the GUI.StdErrProcessorgetStdErrProcessor()Returns the handler for processing the output received on stderr.StdOutProcessorgetStdOutProcessor()Returns the handler for processing the output received on stdout.booleanhasLastCommand()Checks whether a command has been executed (and recorded).booleanhasOutput()Whether there is any pending output.protected voidinitialize()Initializes the members.booleanisExecuted()Returns whether the command was executed.booleanisFinished()Returns whether the command finished.booleanisRunning()Returns whether the command is currently running.booleanisStopped()Whether the execution has been stopped.protected voidlog(CommandResult res)For logging the result of a command.protected voidlog(String[] cmd)Logs the command for execution.protected voidlog(List<String> cmd)Logs the command for execution.Objectoutput()Returns the next line in the output.StringoutputFormatterTipText()Returns the tip text for this property.StringoutputTypeTipText()Returns the tip text for this property.protected voidreset()Resets the scheme.voidsetFlowContext(Actor value)Sets the flow context.voidsetOutputFormatter(OutputFormatter value)Sets the formatter for the output that is being forwarded.voidsetOutputType(OutputType value)Sets the type of output to forward.voidsetStdErrProcessor(StdErrProcessor value)Sets the handler for processing the output received on stderr.voidsetStdOutProcessor(StdOutProcessor value)Sets the handler for processing the output received on stdout.StringstdErrProcessorTipText()Returns the tip text for this property.StringstdOutProcessorTipText()Returns the tip text for this property.voidstopExecution()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:
defineOptionsin interfaceOptionHandler- Overrides:
defineOptionsin classAbstractOptionHandler
-
initialize
protected void initialize()
Initializes the members.- Overrides:
initializein classAbstractOptionHandler
-
reset
protected void reset()
Resets the scheme.- Overrides:
resetin 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:
setOutputTypein interfaceExternalCommand- Parameters:
value- the type
-
getOutputType
public OutputType getOutputType()
Returns the type of output to forward.- Specified by:
getOutputTypein interfaceExternalCommand- Returns:
- the type
-
outputTypeTipText
public String outputTypeTipText()
Returns the tip text for this property.- Specified by:
outputTypeTipTextin 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:
setOutputFormatterin interfaceExternalCommand- Parameters:
value- the formatter
-
getOutputFormatter
public OutputFormatter getOutputFormatter()
Returns the formatter for the output that is being forwarded.- Specified by:
getOutputFormatterin interfaceExternalCommand- Returns:
- the formatter
-
outputFormatterTipText
public String outputFormatterTipText()
Returns the tip text for this property.- Specified by:
outputFormatterTipTextin 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:
setStdOutProcessorin interfaceExternalCommand- Parameters:
value- the handler
-
getStdOutProcessor
public StdOutProcessor getStdOutProcessor()
Returns the handler for processing the output received on stdout.- Specified by:
getStdOutProcessorin interfaceExternalCommand- Returns:
- the handler
-
stdOutProcessorTipText
public String stdOutProcessorTipText()
Returns the tip text for this property.- Specified by:
stdOutProcessorTipTextin 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:
setStdErrProcessorin interfaceExternalCommand- Parameters:
value- the handler
-
getStdErrProcessor
public StdErrProcessor getStdErrProcessor()
Returns the handler for processing the output received on stderr.- Specified by:
getStdErrProcessorin interfaceExternalCommand- Returns:
- the handler
-
stdErrProcessorTipText
public String stdErrProcessorTipText()
Returns the tip text for this property.- Specified by:
stdErrProcessorTipTextin 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:
getQuickInfoin interfaceQuickInfoSupporter- Returns:
- null if no info available, otherwise short string
-
setFlowContext
public void setFlowContext(Actor value)
Sets the flow context.- Specified by:
setFlowContextin interfaceFlowContextHandler- Parameters:
value- the actor
-
getFlowContext
public Actor getFlowContext()
Returns the flow context, if any.- Specified by:
getFlowContextin 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_Runningflag 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_Runningflag 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:
executein interfaceExternalCommand- Returns:
- null if successful, otherwise error message
-
addFormattedOutput
public void addFormattedOutput(Object output)
Gets called by the output formatter class.- Specified by:
addFormattedOutputin interfaceExternalCommand- Parameters:
output- the formatted output to collect
-
hasLastCommand
public boolean hasLastCommand()
Checks whether a command has been executed (and recorded).- Specified by:
hasLastCommandin interfaceExternalCommand- Returns:
- true if executed/recorded
-
getLastCommand
public String[] getLastCommand()
Returns the last command that was executed.- Specified by:
getLastCommandin interfaceExternalCommand- Returns:
- the last command, null if not available
-
isExecuted
public boolean isExecuted()
Returns whether the command was executed.- Specified by:
isExecutedin interfaceExternalCommand- Returns:
- true if executed
-
isRunning
public boolean isRunning()
Returns whether the command is currently running.- Specified by:
isRunningin interfaceExternalCommand- Returns:
- true if running
-
isFinished
public boolean isFinished()
Returns whether the command finished.- Specified by:
isFinishedin interfaceExternalCommand- Returns:
- true if finished
-
hasOutput
public boolean hasOutput()
Whether there is any pending output.- Specified by:
hasOutputin interfaceExternalCommand- Returns:
- true if output pending
-
output
public Object output()
Returns the next line in the output.- Specified by:
outputin interfaceExternalCommand- Returns:
- the line, null if none available
-
stopExecution
public void stopExecution()
Stops the execution.- Specified by:
stopExecutionin interfaceStoppable
-
isStopped
public boolean isStopped()
Whether the execution has been stopped.- Specified by:
isStoppedin interfaceStoppableWithFeedback- Returns:
- true if stopped
-
generates
public Class generates()
Returns what output type the command generates via its output formatter.- Specified by:
generatesin interfaceExternalCommand- Returns:
- the type
-
cleanUp
public void cleanUp()
Cleans up data structures, frees up memory.- Specified by:
cleanUpin interfaceCleanUpHandler- Specified by:
cleanUpin interfaceExternalCommand
-
-