Class AbstractCLI

    • Field Detail

      • log

        protected static final org.apache.commons.logging.Log log
      • FAILURE_FROM_ERROR_OBJECTS

        protected 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
    • Constructor Detail

      • AbstractCLI

        public AbstractCLI()
    • Method Detail

      • getBean

        @Deprecated
        protected <T> T getBean​(String name,
                                Class<T> clz)
        Deprecated.
        Use Autowired to specify your dependencies, this is just a wrapper around the current BeanFactory.
        Convenience method to obtain instance of any bean by name.
        Type Parameters:
        T - the bean class type
        Parameters:
        clz - class
        name - name
        Returns:
        bean
      • getOptions

        public org.apache.commons.cli.Options getOptions()
        Description copied from interface: CLI
        Obtain the options for the CLI.
        Specified by:
        getOptions in interface CLI
      • allowPositionalArguments

        public boolean allowPositionalArguments()
        Description copied from interface: CLI
        Indicate if this CLI allows positional arguments.
        Specified by:
        allowPositionalArguments in interface CLI
      • executeCommand

        public 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 of FAILURE_FROM_ERROR_OBJECTS.

        Specified by:
        executeCommand in interface CLI
        Returns:
        an exit code
      • addAutoOption

        protected void addAutoOption​(org.apache.commons.cli.Options options)
        Add the -auto option.

        The auto option value can be retrieved with isAutoSeek().

      • addDateOption

        protected void addDateOption​(org.apache.commons.cli.Options options)
        Add the -mdate option.

        The limiting date can be retrieved with getLimitingDate().

      • addThreadsOption

        protected void addThreadsOption​(org.apache.commons.cli.Options options)
        Add the -threads option.

        This is used to configure the internal batch processing thread pool which can be used with executeBatchTasks(Collection). You may also use getNumThreads() to retrieve the number of threads to use.

      • addBatchOption

        protected 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.

      • setAllowPositionalArguments

        protected 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 using CommandLine.getArgList().

      • isAutoSeek

        protected boolean isAutoSeek()
      • getNumThreads

        protected int getNumThreads()
      • getLimitingDate

        protected Date getLimitingDate()
      • buildStandardOptions

        protected void buildStandardOptions​(org.apache.commons.cli.Options options)
      • 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.

      • processStandardOptions

        protected void processStandardOptions​(org.apache.commons.cli.CommandLine commandLine)
                                       throws org.apache.commons.cli.ParseException
        Somewhat 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
      • processOptions

        protected abstract void processOptions​(org.apache.commons.cli.CommandLine commandLine)
                                        throws org.apache.commons.cli.ParseException
        Process command line options.

        Implement this to provide processing of options. It is called after buildOptions(Options) and right before doWork().

        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.
      • promptConfirmationOrAbort

        protected void promptConfirmationOrAbort​(String message)
                                          throws Exception
        Prompt the user for a confirmation or raise an exception to abort the doWork() method.
        Throws:
        Exception
      • addSuccessObject

        protected 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
      • 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 intended to be used when an Exception is caught.

        Parameters:
        errorObject - object that was processed
        message - error message
        throwable - throwable to produce a stacktrace
      • createBatchTaskExecutorService

        protected ExecutorService createBatchTaskExecutorService()
        Create an ExecutorService to be used for running batch tasks.
      • getBatchTaskExecutor

        protected 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.