org.springframework.shell.core
Class AbstractShell

java.lang.Object
  extended by org.springframework.shell.event.AbstractShellStatusPublisher
      extended by 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
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
AbstractShell()
           
 
Method Summary
 void blockCommentBegin()
           
 void blockCommentFinish()
           
 String date()
           
 boolean executeCommand(String line)
          Runs the specified command.
protected  boolean executeScriptLine(String line)
          Execute the single line from a script.
protected abstract  Collection<URL> findResources(String path)
          Returns any classpath resources with the given path
 void flash(Level level, String message, String slot)
          Simple implementation of flash(Level, String, String) that simply displays the message via the logger.
 void flashCustom()
           
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.
 void inlineComment()
           
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.
 String props()
           
 void script(File script, boolean lineNumbers)
           
 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 version(String extra)
           
 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

findResources

protected abstract Collection<URL> findResources(String path)
Returns any classpath resources with the given path

Parameters:
path - the path for which to search (never null)
Returns:
null if the search can't be performed
Since:
1.2.0

getHomeAsString

protected abstract String getHomeAsString()

getExecutionStrategy

protected abstract ExecutionStrategy getExecutionStrategy()

getParser

protected abstract Parser getParser()

script

public void script(File script,
                   boolean lineNumbers)

executeScriptLine

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

inlineComment

public void inlineComment()

blockCommentBegin

public void blockCommentBegin()

blockCommentFinish

public void blockCommentFinish()

props

public String props()

date

public String date()

flashCustom

public void flashCustom()
                 throws Exception
Throws:
Exception

version

public String version(String extra)

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.