org.springframework.shell.core

## Class AbstractShell

- java.lang.Object
- - org.springframework.shell.event.AbstractShellStatusPublisher
  - - org.springframework.shell.core.AbstractShell

- All Implemented Interfaces:
    Shell, ShellPromptAccessor, ShellStatusProvider
  Direct Known Subclasses:
    JLineShell

      public abstract class AbstractShell
      extends AbstractShellStatusPublisher
      implements Shell

  Provides a base `Shell` implementation.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String` | `completionKeys`
    `protected ExitShellRequest` | `exitShellRequest`
    `protected boolean` | `inBlockComment`
    `protected Logger` | `logger`
    `protected static String` | `ROO_PROMPT`
    `static String` | `shellPrompt`

    - ### Fields inherited from class org.springframework.shell.event.AbstractShellStatusPublisher

      `shellStatus, shellStatusListeners`

    - ### Fields inherited from interface org.springframework.shell.core.Shell

      `WINDOW_TITLE_SLOT`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AbstractShell()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `blockCommentBegin()`
    `void` | `blockCommentFinish()`
    `CommandResult` | `executeCommand(String line)` Runs the specified command.
    `boolean` | `executeScriptLine(String line)` Execute the single line from a script.
    `void` | `flash(Level level, String message, String slot)` Simple implementation of `flash(Level, String, String)` that simply displays the message via the logger.
    `protected abstract ExecutionStrategy` | `getExecutionStrategy()`
    `ExitShellRequest` | `getExitShellRequest()`
    `File` | `getHome()` Obtains the home directory for the current shell instance.
    `protected abstract String` | `getHomeAsString()`
    `protected abstract Parser` | `getParser()`
    `String` | `getShellPrompt()`
    `protected void` | `handleExecutionResult(Object result)` Handles the result of execution of a command.
    `protected void` | `logCommandIfRequired(String line, boolean successful)` Allows a subclass to log the execution of a well-formed command.
    `protected void` | `logCommandToOutput(String processedLine)` Allows a subclass to actually write the resulting logged command to some form of output.
    `void` | `setPromptPath(String path)` Base implementation of the `Shell.setPromptPath(String)` method, designed for simple shell implementations.
    `void` | `setPromptPath(String path, boolean overrideStyle)` Default implementation of `Shell#setPromptPath(String, boolean))` method to satisfy STS compatibility.
    `String` | `versionInfo()`

    - ### Methods inherited from class org.springframework.shell.event.AbstractShellStatusPublisher

      `addShellStatusListener, getShellStatus, removeShellStatusListener, setShellStatus, setShellStatus`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface org.springframework.shell.core.Shell

      `isDevelopmentMode, promptLoop, setDevelopmentMode`

    - ### Methods inherited from interface org.springframework.shell.event.ShellStatusProvider

      `addShellStatusListener, getShellStatus, removeShellStatusListener`

- - ### Field Detail

    - #### ROO_PROMPT

          protected static final String ROO_PROMPT

      See Also:
        Constant Field Values

    - #### completionKeys

          public static String completionKeys

    - #### shellPrompt

          public static String shellPrompt

    - #### logger

          protected final Logger logger

    - #### inBlockComment

          protected boolean inBlockComment

    - #### exitShellRequest

          protected ExitShellRequest exitShellRequest

  - ### Constructor Detail

    - #### AbstractShell

          public AbstractShell()

  - ### Method Detail

    - #### getHomeAsString

          protected abstract String getHomeAsString()

    - #### getExecutionStrategy

          protected abstract ExecutionStrategy getExecutionStrategy()

    - #### getParser

          protected abstract Parser getParser()

    - #### executeScriptLine

          public boolean executeScriptLine(String line)

      Execute the single line from a script.

      This method can be overridden by sub-classes to pre-process script lines.

    - #### executeCommand

          public CommandResult executeCommand(String line)

      Description copied from interface: `Shell`
      Runs the specified command. Control will return to the caller after the command is run.
      Specified by:
        `executeCommand` in interface `Shell`
      Parameters:
        `line` - to execute (required)
      Returns:
        true if the command was successful, false if there was an exception

    - #### logCommandIfRequired

          protected void logCommandIfRequired(String line,
                                  boolean successful)

      Allows a subclass to log the execution of a well-formed command. This is invoked after a command has completed, and indicates whether the command returned normally or returned an exception. Note that attempted commands that are not well-formed (eg they are missing a mandatory argument) will never be presented to this method, as the command execution is never actually attempted in those cases. This method is only invoked if an attempt is made to execute a particular command.

      Implementations should consider specially handling the "script" commands, and also indicating whether a command was successful or not. Implementations that wish to behave consistently with other `AbstractShell` subclasses are encouraged to simply override `logCommandToOutput(String)` instead, and only override this method if you actually need to fine-tune the output logic.

      Parameters:
        `line` - the parsed line (any comments have been removed; never null)
        `successful` - if the command was successful or not

    - #### logCommandToOutput

          protected void logCommandToOutput(String processedLine)

      Allows a subclass to actually write the resulting logged command to some form of output. This frees subclasses from needing to implement the logic within `logCommandIfRequired(String, boolean)`.

      Implementations should invoke `getExitShellRequest()` to monitor any attempts to exit the shell and release resources such as output log files.

      Parameters:
        `processedLine` - the line that should be appended to some type of output (excluding the \n character)

    - #### setPromptPath

          public void setPromptPath(String path)

      Base implementation of the `Shell.setPromptPath(String)` method, designed for simple shell implementations. Advanced implementations (eg those that support ANSI codes etc) will likely want to override this method and set the `shellPrompt` variable directly.
      Specified by:
        `setPromptPath` in interface `Shell`
      Parameters:
        `path` - to set (can be null or empty; must NOT be formatted in any special way eg ANSI codes)

    - #### setPromptPath

          public void setPromptPath(String path,
                           boolean overrideStyle)

      Default implementation of `Shell#setPromptPath(String, boolean))` method to satisfy STS compatibility.
      Specified by:
        `setPromptPath` in interface `Shell`
      Parameters:
        `path` - to set (can be null or empty)
        `overrideStyle` -

    - #### getExitShellRequest

          public ExitShellRequest getExitShellRequest()

      Specified by:
        `getExitShellRequest` in interface `Shell`
      Returns:
        null if no exit was requested, otherwise the last exit code indicated to the shell to use

    - #### blockCommentBegin

          @CliCommand(value="/*",
                      help="Start of block comment")
          public void blockCommentBegin()

    - #### blockCommentFinish

          @CliCommand(value="*/",
                      help="End of block comment")
          public void blockCommentFinish()

    - #### versionInfo

          public String versionInfo()

    - #### getShellPrompt

          public String getShellPrompt()

      Specified by:
        `getShellPrompt` in interface `ShellPromptAccessor`
      Returns:
        the shell prompt (never null; the result may include special characters such as ANSI escape codes if the implementation is using them)

    - #### getHome

          public File getHome()

      Obtains the home directory for the current shell instance.

      Note: calls the `getHomeAsString()` method to allow subclasses to provide the home directory location as string using different environment-specific strategies.

      If the path indicated by `getHomeAsString()` exists and refers to a directory, that directory is returned.

      If the path indicated by `getHomeAsString()` exists and refers to a file, an exception is thrown.

      If the path indicated by `getHomeAsString()` does not exist, it will be created as a directory. If this fails, an exception will be thrown.

      Specified by:
        `getHome` in interface `Shell`
      Returns:
        the home directory for the current shell instance (which is guaranteed to exist and be a directory)

    - #### flash

          public void flash(Level level,
                   String message,
                   String slot)

      Simple implementation of `flash(Level, String, String)` that simply displays the message via the logger. It is strongly recommended shell implementations override this method with a more effective approach.
      Specified by:
        `flash` in interface `Shell`
      Parameters:
        `level` - the importance of the message (cannot be null)
        `message` - to display (cannot be null, but may be empty)
        `slot` - the identification slot for the message (cannot be null or empty)

    - #### handleExecutionResult

          protected void handleExecutionResult(Object result)

      Handles the result of execution of a command. Given result is expected to be not `null`. If result is a `Iterable` object, it will be iterated through to print the output of toString. For other type of objects, simply output of toString is shown. Subclasses can alter this implementation to handle the result differently.
      Parameters:
        `result` - not `null` result of execution of a command.