henplus
Class AbstractCommand

java.lang.Object
  extended by henplus.AbstractCommand
All Implemented Interfaces:
Command
Direct Known Subclasses:
AboutCommand, AbstractPropertyCommand, AliasCommand, ConnectCommand, DescribeCommand, DriverCommand, DumpCommand, EchoCommand, ExitCommand, ExportCommand, HelpCommand, ImportCommand, KeyBindCommand, ListUserObjectsCommand, LoadCommand, PluginCommand, SamplePlugin, SetCommand, ShellCommand, SpoolCommand, SQLCommand, StatusCommand, SystemInfoCommand, TableDiffCommand, TreeCommand

public abstract class AbstractCommand
extends Object
implements Command

Implementation of a Command with default settings. Override what is necessary in your Command. It makes sense to derive plug-ins from this AbstractCommand - this makes the plug-in more robust with regard to newly added methods.

Author:
Henner Zeller

Field Summary
 
Fields inherited from interface henplus.Command
EXEC_FAILED, SUCCESS, SYNTAX_ERROR
 
Constructor Summary
AbstractCommand()
           
 
Method Summary
protected  int argumentCount(String command)
          convenience method: returns the number of elements in this string, separated by whitespace.
 Iterator complete(CommandDispatcher disp, String partialCommand, String lastWord)
          Returns a list of strings that are possible at this stage.
 String getLongDescription(String cmd)
          returns a longer string describing this action.
 Option getOption(String arg0)
           
protected  Options getOptions()
           
 String getShortDescription()
          return a short string describing the purpose of the commands handled by this Command-implementation.
 String getSynopsis(String cmd)
          retuns a synopsis-string.
 void handleCommandline(CommandLine line)
          After parsing the parameters, this method is called.
 boolean isComplete(String command)
          returns, whether the command is complete.
 boolean participateInCommandCompletion()
          returns 'false', if the commands supported by this Commands should not be part of the toplevel command completion.
 void registerOptions(Options r)
          Override this method if you want to register command-specific options
 boolean requiresValidSession(String cmd)
          returns true, if this command requires a valid SQLSession, i.e.
 void setOptions(Options options)
          Options are set, after the commandline has been parsed.
 void shutdown()
          shutdown this command.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface henplus.Command
execute, getCommandList
 

Constructor Detail

AbstractCommand

public AbstractCommand()
Method Detail

getShortDescription

public String getShortDescription()
Description copied from interface: Command
return a short string describing the purpose of the commands handled by this Command-implementation. This is the string listed in the bare 'help' overview (like 'describe a database object') Should contain no newline, no leading spaces.

Specified by:
getShortDescription in interface Command

getLongDescription

public String getLongDescription(String cmd)
Description copied from interface: Command
returns a longer string describing this action. This should return a String describing details of the given command. This String should start with a TAB-character in each new line (the first line is a new line). The last line should not end with newline.

Specified by:
getLongDescription in interface Command
Parameters:
cmd - The command the long description is asked for. This is one of the possible commands returned by Command.getCommandList().

getSynopsis

public String getSynopsis(String cmd)
Description copied from interface: Command
retuns a synopsis-string. The synopsis string returned should follow the following conventions:

Should contain no newline, no leading spaces. This synopsis is printed in the detailed help of a command or if the execute()-method returned a SYNTAX_ERROR.

Specified by:
getSynopsis in interface Command
Parameters:
cmd - the command the synopsis is for. This is one of the possible commands returned by Command.getCommandList().

participateInCommandCompletion

public boolean participateInCommandCompletion()
Description copied from interface: Command
returns 'false', if the commands supported by this Commands should not be part of the toplevel command completion. So if the user presses TAB on an empty string to get the full list of possible commands, this command should not show up. In HenPlus, this returns 'false' for the SQL-commands ('select', 'update', 'drop' ..), since this would clobber the toplevel list of available commands. If unsure, return 'true'.

Specified by:
participateInCommandCompletion in interface Command

complete

public Iterator complete(CommandDispatcher disp,
                         String partialCommand,
                         String lastWord)
Description copied from interface: Command
Returns a list of strings that are possible at this stage. Used for the readline-completion in interactive mode. Based on the partial command and the lastWord you have to determine the words that are available at this stage. Return 'null', if you don't know a possible completion.

Specified by:
complete in interface Command
Parameters:
disp - the CommandDispatcher - you might want to access other values through it.
partialCommand - The command typed so far
lastWord - the last word returned by readline.

shutdown

public void shutdown()
Description copied from interface: Command
shutdown this command. This is called on exit of the CommandDispatcher and allows you to do some cleanup (close connections, flush files..)

Specified by:
shutdown in interface Command

isComplete

public boolean isComplete(String command)
Description copied from interface: Command
returns, whether the command is complete.

This method is called, whenever the input encounters a newline or a semicolon to decide if this separator is to separate different commands or if it is part of the command itself.

The delimiter (newline or semicolon) is contained (at the end) in the String passed to this method. This method returns false, if the delimiter is part of the command and will not be regarded as delimiter between commands -- the reading part of the command dispatcher will go on reading characters and not execute the command.

This method will return true for most simple commands like 'help'. For commands that have a more complicated syntax, this might not be true.

  • 'select * from foobar' is not complete after a return, since we can expect a where clause. If it has a semicolon at the end, we know, that is is complete. So newline is not a delimiter while ';' is (return command.endsWith(";")).
  • definitions of stored procedures are even more complicated: it depends on the syntax whether a semicolon is part of the command or can be regarded as delimiter. Here, neither ';' nor newline can be regarded as delimiter per-se. Only the Command implementation can decide upon this. In Oracle, a single '/' on one line is used to denote this command-complete.
Note, this method should only apply a very lazy syntax check so it does not get confused and uses too much cycles unecessarily..

Specified by:
isComplete in interface Command
Parameters:
command - the partial command read so far given to decide by the command whether it is complete or not.

requiresValidSession

public boolean requiresValidSession(String cmd)
Description copied from interface: Command
returns true, if this command requires a valid SQLSession, i.e. if the Command.execute(SQLSession,String,String) method makes use of the session (e.g. to get some Database connection) or not. Return 'true' if unsure (you should be sure..). This is to thwart attempts to execute a command without session.

Specified by:
requiresValidSession in interface Command
Parameters:
cmd - the subcommand this is requested for; one of the commands returned by Command.getCommandList().

argumentCount

protected int argumentCount(String command)
convenience method: returns the number of elements in this string, separated by whitespace.


getOptions

protected Options getOptions()

setOptions

public void setOptions(Options options)
Description copied from interface: Command
Options are set, after the commandline has been parsed.

Specified by:
setOptions in interface Command

getOption

public Option getOption(String arg0)

registerOptions

public void registerOptions(Options r)
Override this method if you want to register command-specific options

Specified by:
registerOptions in interface Command
Parameters:
r -

handleCommandline

public void handleCommandline(CommandLine line)
Description copied from interface: Command
After parsing the parameters, this method is called. there can be some default options left. These are set to the commands through this method. This is only for compatibility with the old commandline options, please use named options only!

Specified by:
handleCommandline in interface Command
Parameters:
line - TODO


? 1997..2006 Henner Zeller