Class ConsoleCommand

java.lang.Object
org.apache.juneau.microservice.console.ConsoleCommand
Direct Known Subclasses:
ConfigCommand, EchoCommand, ExitCommand, HelpCommand, RestartCommand

public abstract class ConsoleCommand extends Object
Implements a command that can be invoked from the console of the microservice.

Console commands allow you to interact with your microservice through the system console.

Console commands are associated with the microservice through the following:

  • The "Console/commands" configuration value.
    This is a comma-delimited list of fully-qualified names of classes implementing this interface.
    When associated this way, the implementation class must have a no-arg constructor.
  • Specifying commands via the Microservice.Builder.consoleCommands(Class...) method.
    This allows you to override the default implementation above and augment or replace the list with your own implementation objects.

For example, the HelpCommand is used to provide help on other commands.

Running class 'JettyMicroservice' using config file 'examples.cfg'. Server started on port 10000 List of available commands: exit -- Shut down service restart -- Restarts service help -- Commands help echo -- Echo command > help help NAME help -- Commands help SYNOPSIS help [command] DESCRIPTION When called without arguments, prints the descriptions of all available commands. Can also be called with one or more arguments to get detailed information on a command. EXAMPLES List all commands: > help List help on the help command: > help help >

The arguments are available as an Args object which allows for easy accessed to parsed command lines. Some simple examples of valid command lines:

// mycommand args.get("0"); // "mycommand" // mycommand arg1 arg2 args.get("0"); // "mycommand" args.get("1"); // "arg1" args.get("2"); // "arg2" // mycommand -optArg1 foo bar -optArg2 baz qux args.get("0"); // "mycommand" args.get("optArg1", String[].class); // ["foo","bar"] args.get("optArg2", String[].class); // ["baz","qux"] // mycommand -optArg1 "foo bar" -optArg2 'baz qux' args.get("0"); // "mycommand" args.get("optArg1", String[].class); // ["foo bar"] args.get("optArg2", String[].class); // ["baz qux"]

See Also:
  • Constructor Details

  • Method Details

    • getName

      public abstract String getName()
      Returns the name of the command.

      Example: "help" for the help command.

      Returns:
      The name of the command.
      Must not be null or contain spaces.
    • getSynopsis

      public String getSynopsis()
      Returns the usage synopsis of the command.

      Example: "help [command ...]"

      The default implementation just returns the name, which implies the command takes no additional arguments.

      Returns:
      The synopsis of the command.
    • getInfo

      public String getInfo()
      Returns a one-line localized description of the command.

      The locale should be the system locale.

      Returns:
      The localized description of the command.
      Can be null if there is no information.
    • getDescription

      Returns localized details of the command.

      The locale should be the system locale.

      Returns:
      The localized details of the command.
      Can be null if there is no additional description.
    • getExamples

      public String getExamples()
      Returns localized examples of the command.

      The locale should be the system locale.

      Returns:
      The localized examples of the command.
      Can be null if there is no examples.
    • execute

      public abstract boolean execute(Scanner in, PrintWriter out, Args args) throws Exception
      Executes a command.
      Parameters:
      in - The console reader.
      out - The console writer.
      args - The command arguments. The first argument is always the command itself.
      Returns:
      true if the console read thread should exit.
      Normally you want to return true if your action is causing the microservice to exit or restart.
      Throws:
      Exception - Any thrown exception will simply be sent to STDERR.