Class AbstractCLI
- java.lang.Object
- 
- ubic.gemma.core.util.AbstractCLI
 
- 
- All Implemented Interfaces:
- CLI
 - Direct Known Subclasses:
- AbstractAuthenticatedCLI,- GenerateDatabaseUpdateCli,- IndexGemmaCLI,- InitializeDatabaseCli,- MeshTermFetcherCli,- UpdateDatabaseCli
 
 public abstract class AbstractCLI extends Object implements CLI Basic implementation of theCLIinterface.To use this, in your concrete subclass, implement buildOptions(org.apache.commons.cli.Options)andprocessOptions(org.apache.commons.cli.CommandLine)to handle any application-specific options (they can be no-ops) anddoWork()to perform the actual work of the CLI.Use AbstractAuthenticatedCLIif you need to authenticate the user.- Author:
- pavlidis
 
- 
- 
Field SummaryFields Modifier and Type Field Description protected static intABORTEDExit code used when adoWork()is aborted.protected static intFAILUREExit code used for a faileddoWork()execution.protected static intFAILURE_FROM_ERROR_OBJECTSExit code used for a successful doWork execution that resulted in failed error objects.protected static org.apache.commons.logging.Loglogprotected static intSUCCESSExit code used for a successfuldoWork()execution.
 - 
Constructor SummaryConstructors Constructor Description AbstractCLI()
 - 
Method SummaryAll Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected voidaddAutoOption(org.apache.commons.cli.Options options)Add the-autooption.protected voidaddAutoOption(org.apache.commons.cli.Options options, Class<? extends AuditEventType> autoSeekEventType)Add the-autooption for a specificAuditEventType.protected voidaddBatchOption(org.apache.commons.cli.Options options)Add the -batchFormat and -batchOutputFile options.protected voidaddDateOption(org.apache.commons.cli.Options options)Add the-mdateoption.protected voidaddErrorObject(Object errorObject, Exception exception)Add an error object based on an exception.protected voidaddErrorObject(Object errorObject, String message)Add an error object without a cause stacktrace.protected voidaddErrorObject(Object errorObject, String message, Throwable throwable)Add an error object with a stacktrace to indicate failure in a batch processing.protected voidaddSuccessObject(Object successObject)protected voidaddSuccessObject(Object successObject, String message)Add a success object to indicate success in a batch processing.protected voidaddThreadsOption(org.apache.commons.cli.Options options)Add the-threadsoption.booleanallowPositionalArguments()Indicate if this CLI allows positional arguments.protected abstract voidbuildOptions(org.apache.commons.cli.Options options)Build option implementation.protected voidbuildStandardOptions(org.apache.commons.cli.Options options)protected ExecutorServicecreateBatchTaskExecutorService()Create anExecutorServiceto be used for running batch tasks.protected abstract voiddoWork()Command line implementation.protected voidexecuteBatchTasks(Collection<? extends Runnable> tasks)Execute the given batch tasks and await their completions.intexecuteCommand(String... args)Execute the given command given CLI arguments.protected Class<? extends AuditEventType>getAutoSeekEventType()protected ExecutorServicegetBatchTaskExecutor()Obtain an executor for running batch tasks.protected <T> TgetBean(Class<T> clz)Deprecated.protected <T> TgetBean(String name, Class<T> clz)Deprecated.UseAutowiredto specify your dependencies, this is just a wrapper around the currentBeanFactory.protected DategetLimitingDate()protected intgetNumThreads()org.apache.commons.cli.OptionsgetOptions()Obtain the options for the CLI.protected booleanisAutoSeek()protected abstract voidprocessOptions(org.apache.commons.cli.CommandLine commandLine)Process command line options.protected voidprocessStandardOptions(org.apache.commons.cli.CommandLine commandLine)Somewhat annoying: This causes subclasses to be unable to safely use 'h', 'p', 'u' and 'P' etc.protected voidpromptConfirmationOrAbort(String message)Prompt the user for a confirmation or raise an exception to abort thedoWork()method.protected voidsetAllowPositionalArguments(boolean allowPositionalArguments)Allow positional arguments to be specified.- 
Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 - 
Methods inherited from interface ubic.gemma.core.util.CLIgetCommandGroup, getCommandName, getShortDesc
 
- 
 
- 
- 
- 
Field Detail- 
logprotected static final org.apache.commons.logging.Log log 
 - 
SUCCESSprotected static final int SUCCESS Exit code used for a successfuldoWork()execution.- See Also:
- Constant Field Values
 
 - 
FAILUREprotected static final int FAILURE Exit code used for a faileddoWork()execution.- See Also:
- Constant Field Values
 
 - 
FAILURE_FROM_ERROR_OBJECTSprotected 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
 
 - 
ABORTEDprotected static final int ABORTED Exit code used when adoWork()is aborted.- See Also:
- Constant Field Values
 
 
- 
 - 
Method Detail- 
getBean@Deprecated protected <T> T getBean(String name, Class<T> clz) Deprecated.UseAutowiredto specify your dependencies, this is just a wrapper around the currentBeanFactory.Convenience method to obtain instance of any bean by name.- Type Parameters:
- T- the bean class type
- Parameters:
- clz- class
- name- name
- Returns:
- bean
 
 - 
getBean@Deprecated protected <T> T getBean(Class<T> clz) Deprecated.
 - 
getOptionspublic org.apache.commons.cli.Options getOptions() Description copied from interface:CLIObtain the options for the CLI.- Specified by:
- getOptionsin interface- CLI
 
 - 
allowPositionalArgumentspublic boolean allowPositionalArguments() Description copied from interface:CLIIndicate if this CLI allows positional arguments.- Specified by:
- allowPositionalArgumentsin interface- CLI
 
 - 
executeCommandpublic int executeCommand(String... args) Execute the given command given CLI arguments.Parse and process CLI arguments, invoke the command doWork()implementation, and print basic statistics about time usage.Any exception raised by doWork results in a value of FAILURE, and any error set in the internal error objects will result in a value ofFAILURE_FROM_ERROR_OBJECTS.- Specified by:
- executeCommandin interface- CLI
- Returns:
- an exit code
 
 - 
addAutoOptionprotected void addAutoOption(org.apache.commons.cli.Options options) Add the-autooption.The auto option value can be retrieved with isAutoSeek().
 - 
addAutoOptionprotected void addAutoOption(org.apache.commons.cli.Options options, Class<? extends AuditEventType> autoSeekEventType)Add the-autooption for a specificAuditEventType.The event type can be retrieved with getAutoSeekEventType().
 - 
addDateOptionprotected void addDateOption(org.apache.commons.cli.Options options) Add the-mdateoption.The limiting date can be retrieved with getLimitingDate().
 - 
addThreadsOptionprotected void addThreadsOption(org.apache.commons.cli.Options options) Add the-threadsoption.This is used to configure the internal batch processing thread pool which can be used with executeBatchTasks(Collection). You may also usegetNumThreads()to retrieve the number of threads to use.
 - 
addBatchOptionprotected void addBatchOption(org.apache.commons.cli.Options options) Add the -batchFormat and -batchOutputFile options.These options allow the user to control how and where batch processing results are summarized. 
 - 
setAllowPositionalArgumentsprotected void setAllowPositionalArguments(boolean allowPositionalArguments) Allow positional arguments to be specified. The default is false and an error will be produced if positional arguments are supplied by the user.Those arguments can be retrieved in processOptions(CommandLine)by usingCommandLine.getArgList().
 - 
isAutoSeekprotected boolean isAutoSeek() 
 - 
getAutoSeekEventTypeprotected Class<? extends AuditEventType> getAutoSeekEventType() 
 - 
getNumThreadsprotected int getNumThreads() 
 - 
getLimitingDateprotected Date getLimitingDate() 
 - 
buildStandardOptionsprotected void buildStandardOptions(org.apache.commons.cli.Options options) 
 - 
buildOptionsprotected 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.
 - 
processStandardOptionsprotected void processStandardOptions(org.apache.commons.cli.CommandLine commandLine) throws org.apache.commons.cli.ParseExceptionSomewhat annoying: This causes subclasses to be unable to safely use 'h', 'p', 'u' and 'P' etc. for their own purposes.- Throws:
- org.apache.commons.cli.ParseException
 
 - 
processOptionsprotected abstract void processOptions(org.apache.commons.cli.CommandLine commandLine) throws org.apache.commons.cli.ParseExceptionProcess command line options.Implement this to provide processing of options. It is called after buildOptions(Options)and right beforedoWork().- Throws:
- org.apache.commons.cli.ParseException- in case of unrecoverable failure (i.e. missing option or invalid value), an exception can be raised and will result in an exit code of- FAILURE.
 
 - 
doWorkprotected abstract void doWork() throws ExceptionCommand 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 a- FAILUREexit code, otherwise use- addErrorObject(java.lang.Object, java.lang.String, java.lang.Throwable)to indicate an error and resume processing
 
 - 
promptConfirmationOrAbortprotected void promptConfirmationOrAbort(String message) throws Exception Prompt the user for a confirmation or raise an exception to abort thedoWork()method.- Throws:
- Exception
 
 - 
addSuccessObjectprotected void addSuccessObject(Object successObject, String message) Add a success object to indicate success in a batch processing.- Parameters:
- successObject- object that was processed
- message- success message
 
 - 
addSuccessObjectprotected void addSuccessObject(Object successObject) - See Also:
- addSuccessObject(Object, String)
 
 - 
addErrorObjectprotected 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 intended to be used when an Exceptionis caught.- Parameters:
- errorObject- object that was processed
- message- error message
- throwable- throwable to produce a stacktrace
 
 - 
addErrorObjectprotected void addErrorObject(@Nullable Object errorObject, String message) Add an error object without a cause stacktrace.- See Also:
- addErrorObject(Object, String)
 
 - 
addErrorObjectprotected void addErrorObject(@Nullable Object errorObject, Exception exception) Add an error object based on an exception.
 - 
createBatchTaskExecutorServiceprotected ExecutorService createBatchTaskExecutorService() Create anExecutorServiceto be used for running batch tasks.
 - 
getBatchTaskExecutorprotected final ExecutorService getBatchTaskExecutor() Obtain an executor for running batch tasks.The CLI will await any pending batch tasks before exiting. You may only submit batch tasks inside doWork().Errors will be reported with addErrorObject(Object, Exception). However, reporting successes is the responsibility of the caller.
 - 
executeBatchTasksprotected void executeBatchTasks(Collection<? extends Runnable> tasks) throws InterruptedException Execute the given batch tasks and await their completions.Errors will be reported with addErrorObject(Object, Exception). However, reporting successes is the responsibility of the caller.- Throws:
- InterruptedException
 
 
- 
 
-