Class AbstractCLI
- java.lang.Object
-
- ubic.gemma.core.util.AbstractCLI
-
- All Implemented Interfaces:
CLI
- Direct Known Subclasses:
AbstractSpringAwareCLI
,IndexGemmaCLI
,MeshTermFetcherCli
public abstract class AbstractCLI extends Object implements CLI
Base Command Line Interface. Provides some default functionality.To use this, in your concrete subclass, implement a main method. You must implement buildOptions and processOptions to handle any application-specific options (they can be no-ops).
To facilitate testing of your subclass, your main method must call a non-static 'doWork' method, that will be exposed for testing. In that method call processCommandline. You should return any non-null return value from processCommandLine.
- Author:
- pavlidis
-
-
Field Summary
Fields Modifier and Type Field Description protected static String
AUTO_OPTION_NAME
protected boolean
autoSeek
Automatically identify which entities to run the tool on.protected Class<? extends AuditEventType>
autoSeekEventType
The event type to look for the lack of, when using auto-seek.static int
FAILURE
Exit code used for a failed doWork execution.static int
FAILURE_FROM_ERROR_OBJECTS
Exit code used for a successful doWork execution that resulted in failed error objects.static String
FOOTER
static String
HEADER
protected String
host
protected static org.apache.commons.logging.Log
log
protected String
mDate
Date used to identify which entities to run the tool on (e.g., those which were run less recently than mDate).protected int
numThreads
protected int
port
static int
SUCCESS
Exit code used for a successful doWork execution.protected static String
THREADS_OPTION
-
Constructor Summary
Constructors Constructor Description AbstractCLI()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
addAutoOption(org.apache.commons.cli.Options options)
You must implement the handling for this option.protected void
addDateOption(org.apache.commons.cli.Options options)
protected void
addErrorObject(Object errorObject, Exception exception)
Add an error object based on an exception.protected void
addErrorObject(Object errorObject, String message)
Add an error object without a cause stacktrace.protected void
addErrorObject(Object errorObject, String message, Throwable throwable)
Add an error object with a stacktrace to indicate failure in a batch processing.protected void
addHostAndPortOptions(org.apache.commons.cli.Options options, boolean hostRequired, boolean portRequired)
Convenience method to add a standard pair of options to intake a host name and port number.protected void
addSuccessObject(Object successObject)
protected void
addSuccessObject(Object successObject, String message)
Add a success object to indicate success in a batch processing.protected void
addThreadsOption(org.apache.commons.cli.Options options)
Convenience method to add an option for parallel processing option.protected abstract void
buildOptions(org.apache.commons.cli.Options options)
Build option implementation.protected void
buildStandardOptions(org.apache.commons.cli.Options options)
protected abstract void
doWork()
Command line implementation.protected <T> List<T>
executeBatchTasks(Collection<? extends Callable<T>> tasks)
Execute batch tasks using a preconfiguredExecutorService
and return all the resulting tasks results.int
executeCommand(String[] args)
Run the command.protected double
getDoubleOptionValue(org.apache.commons.cli.CommandLine commandLine, char option)
protected double
getDoubleOptionValue(org.apache.commons.cli.CommandLine commandLine, String option)
protected String
getFileNameOptionValue(org.apache.commons.cli.CommandLine commandLine, char c)
protected String
getFileNameOptionValue(org.apache.commons.cli.CommandLine commandLine, String c)
protected int
getIntegerOptionValue(org.apache.commons.cli.CommandLine commandLine, char option)
protected int
getIntegerOptionValue(org.apache.commons.cli.CommandLine commandLine, String option)
protected Date
getLimitingDate()
protected void
printHelp(org.apache.commons.cli.Options options)
protected abstract void
processOptions(org.apache.commons.cli.CommandLine commandLine)
Process command line options.protected void
processStandardOptions(org.apache.commons.cli.CommandLine commandLine)
Somewhat annoying: This causes subclasses to be unable to safely use 'h', 'p', 'u' and 'P' etc.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface ubic.gemma.core.util.CLI
getCommandGroup, getCommandName, getShortDesc
-
-
-
-
Field Detail
-
SUCCESS
public static final int SUCCESS
Exit code used for a successful doWork execution.- See Also:
- Constant Field Values
-
FAILURE
public static final int FAILURE
Exit code used for a failed doWork execution.- See Also:
- Constant Field Values
-
FAILURE_FROM_ERROR_OBJECTS
public static final int FAILURE_FROM_ERROR_OBJECTS
Exit code used for a successful doWork execution that resulted in failed error objects.- See Also:
- Constant Field Values
-
FOOTER
public static final String FOOTER
- See Also:
- Constant Field Values
-
AUTO_OPTION_NAME
protected static final String AUTO_OPTION_NAME
- See Also:
- Constant Field Values
-
THREADS_OPTION
protected static final String THREADS_OPTION
- See Also:
- Constant Field Values
-
log
protected static final org.apache.commons.logging.Log log
-
HEADER
public static final String HEADER
- See Also:
- Constant Field Values
-
autoSeek
protected boolean autoSeek
Automatically identify which entities to run the tool on. To enable call addAutoOption.
-
autoSeekEventType
protected Class<? extends AuditEventType> autoSeekEventType
The event type to look for the lack of, when using auto-seek.
-
mDate
protected String mDate
Date used to identify which entities to run the tool on (e.g., those which were run less recently than mDate). To enable call addDateOption.
-
numThreads
protected int numThreads
-
host
protected String host
-
port
protected int port
-
-
Method Detail
-
executeCommand
public int executeCommand(String[] args)
Run the command.Parse and process CLI arguments, invoke the command doWork implementation, and print basic statistics about time usage.
- Specified by:
executeCommand
in interfaceCLI
- Parameters:
args
- Arguments to pass to#processCommandLine(Options, String[])
- Returns:
- Exit code intended to be used with
System.exit(int)
to indicate a success or failure to the end-user. Any exception raised by doWork results in a value ofFAILURE
, and any error set in the internal error objects will result in a value ofFAILURE_FROM_ERROR_OBJECTS
.
-
addAutoOption
protected void addAutoOption(org.apache.commons.cli.Options options)
You must implement the handling for this option.
-
addDateOption
protected void addDateOption(org.apache.commons.cli.Options options)
-
addHostAndPortOptions
protected void addHostAndPortOptions(org.apache.commons.cli.Options options, boolean hostRequired, boolean portRequired)
Convenience method to add a standard pair of options to intake a host name and port number. *- Parameters:
hostRequired
- Whether the host name is requiredportRequired
- Whether the port is required
-
addThreadsOption
protected void addThreadsOption(org.apache.commons.cli.Options options)
Convenience method to add an option for parallel processing option.
-
buildOptions
protected abstract void buildOptions(org.apache.commons.cli.Options options)
Build option implementation.Implement this method to add options to your command line, using the OptionBuilder.
This is called right after
buildStandardOptions(Options)
so the options will be added after standard options.
-
buildStandardOptions
protected void buildStandardOptions(org.apache.commons.cli.Options options)
-
doWork
protected abstract void doWork() throws Exception
Command line implementation.This is called after
buildOptions(Options)
andprocessOptions(CommandLine)
, so the implementation can assume that all its arguments have already been initialized.- Throws:
Exception
- in case of unrecoverable failure, an exception is thrown and will result in aFAILURE
exit code, otherwise useaddErrorObject(java.lang.Object, java.lang.String, java.lang.Throwable)
-
getDoubleOptionValue
protected final double getDoubleOptionValue(org.apache.commons.cli.CommandLine commandLine, char option)
-
getDoubleOptionValue
protected final double getDoubleOptionValue(org.apache.commons.cli.CommandLine commandLine, String option)
-
getFileNameOptionValue
protected final String getFileNameOptionValue(org.apache.commons.cli.CommandLine commandLine, char c)
-
getFileNameOptionValue
protected final String getFileNameOptionValue(org.apache.commons.cli.CommandLine commandLine, String c)
-
getIntegerOptionValue
protected final int getIntegerOptionValue(org.apache.commons.cli.CommandLine commandLine, char option)
-
getIntegerOptionValue
protected final int getIntegerOptionValue(org.apache.commons.cli.CommandLine commandLine, String option)
-
getLimitingDate
protected Date getLimitingDate()
-
printHelp
protected void printHelp(org.apache.commons.cli.Options options)
-
processOptions
protected abstract void processOptions(org.apache.commons.cli.CommandLine commandLine) throws Exception
Process command line options.Implement this to provide processing of options. It is called after
buildOptions(Options)
and right beforedoWork()
.
-
addSuccessObject
protected void addSuccessObject(Object successObject, String message)
Add a success object to indicate success in a batch processing.This is further used in
summarizeProcessing()
to summarize the execution of the command.- Parameters:
successObject
- object that was processedmessage
- success message
-
addSuccessObject
protected void addSuccessObject(Object successObject)
- See Also:
addSuccessObject(Object, String)
-
addErrorObject
protected void addErrorObject(@Nullable Object errorObject, String message, Throwable throwable)
Add an error object with a stacktrace to indicate failure in a batch processing.This is further used in
summarizeProcessing()
to summarize the execution of the command.This is intended to be used when an
Exception
is caught.- Parameters:
errorObject
- object that was processedmessage
- error messagethrowable
- throwable to produce a stacktrace
-
addErrorObject
protected void addErrorObject(@Nullable Object errorObject, String message)
Add an error object without a cause stacktrace.- See Also:
addErrorObject(Object, String)
-
addErrorObject
protected void addErrorObject(@Nullable Object errorObject, Exception exception)
Add an error object based on an exception.
-
executeBatchTasks
protected <T> List<T> executeBatchTasks(Collection<? extends Callable<T>> tasks) throws InterruptedException
Execute batch tasks using a preconfiguredExecutorService
and return all the resulting tasks results.- Throws:
InterruptedException
-
processStandardOptions
protected void processStandardOptions(org.apache.commons.cli.CommandLine commandLine)
Somewhat annoying: This causes subclasses to be unable to safely use 'h', 'p', 'u' and 'P' etc. for their own purposes.
-
-