org.mycore.frontend.cli
Class MCRCommand

java.lang.Object
  extended by org.mycore.frontend.cli.MCRCommand

public class MCRCommand
extends Object

Represents a command understood by the command line interface. A command has an external input syntax that the user uses to invoke the command and points to a method in a class that implements the command.

Version:
$Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
Author:
Frank Lützenkirchen, Jens Kupferschmidt
See Also:
MCRCommandLineInterface

Field Summary
protected  String className
          The class providing the implementation method
protected  String help
          The help text String
protected  MessageFormat messageFormat
          The input format used for invoking this command
protected  Method method
          The java method that implements this command
protected  String methodName
          The method implementing this command
protected  int numParameters
          The number of invocation parameters
protected  Class[] parameterTypes
          The types of the invocation parameters
protected  String suffix
          The beginning of the message format up to the first parameter
 
Constructor Summary
MCRCommand(String format, String methodSignature, String helpText)
          Creates a new MCRCommand.
 
Method Summary
protected  Object[] buildInvocationParameters(Object[] commandParameters)
          Transforms the parameters found by the MessageFormat parse method into such that can be used to invoke the method implementing this command
protected  String getHelpText()
          The method return the helpt text of this command.
protected  Method getMethod(ClassLoader classLoader)
          Returns the method implementing the command behavior.
 List<String> invoke(String input)
          Tries to invoke the method that implements the behavior of this command given the user input from the command line.
 List<String> invoke(String input, ClassLoader classLoader)
           
protected  Object[] parseCommandLine(String commandLine)
          Parses an input string and tries to match it with the message format used to invoke this command.
 String showSyntax()
          Returns the input syntax to be used for invoking this command from the command prompt.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

messageFormat

protected MessageFormat messageFormat
The input format used for invoking this command


method

protected Method method
The java method that implements this command


parameterTypes

protected Class[] parameterTypes
The types of the invocation parameters


numParameters

protected int numParameters
The number of invocation parameters


className

protected String className
The class providing the implementation method


methodName

protected String methodName
The method implementing this command


suffix

protected String suffix
The beginning of the message format up to the first parameter


help

protected String help
The help text String

Constructor Detail

MCRCommand

public MCRCommand(String format,
                  String methodSignature,
                  String helpText)
Creates a new MCRCommand.

Parameters:
format - the command syntax, e.g. "save document {0} to directory {1}"
methodSignature - the method to invoke, e.g. "miless.commandline.DocumentCommands.saveDoc int String"
helpText - the helpt text for this command
Method Detail

getMethod

protected Method getMethod(ClassLoader classLoader)
                    throws ClassNotFoundException,
                           NoSuchMethodException
Returns the method implementing the command behavior.

Returns:
The method to be invoked for executing the command
Throws:
ClassNotFoundException - when the class that implements the method was not found
NoSuchMethodException - When the method specified in the constructor was not found

getHelpText

protected String getHelpText()
The method return the helpt text of this command.

Returns:
the help text as String

parseCommandLine

protected Object[] parseCommandLine(String commandLine)
Parses an input string and tries to match it with the message format used to invoke this command.

Parameters:
commandLine - The input from the command line
Returns:
null, if the input does not match the message format; otherwise an array holding the parameter values from the command line

buildInvocationParameters

protected Object[] buildInvocationParameters(Object[] commandParameters)
Transforms the parameters found by the MessageFormat parse method into such that can be used to invoke the method implementing this command

Parameters:
commandParameters - The parameters as returned by the parseCommandLine method
Returns:
The parameters that can be used to invoke the implementing method

invoke

public List<String> invoke(String input)
                    throws IllegalAccessException,
                           InvocationTargetException,
                           ClassNotFoundException,
                           NoSuchMethodException
Tries to invoke the method that implements the behavior of this command given the user input from the command line. This is only done when the command line syntax matches the syntax used by this command.

Parameters:
input - The command entered by the user at the command prompt
Returns:
null, if the command syntax did not match and the command was not invoked, otherwise a List of commands is returned which may be empty or otherwise contains commands that should be processed next
Throws:
IllegalAccessException - when the method can not be invoked
InvocationTargetException - when an exception is thrown by the invoked method
ClassNotFoundException - when the class providing the method could not be found
NoSuchMethodException - when the method specified does not exist

invoke

public List<String> invoke(String input,
                           ClassLoader classLoader)
                    throws IllegalAccessException,
                           InvocationTargetException,
                           ClassNotFoundException,
                           NoSuchMethodException
Throws:
IllegalAccessException
InvocationTargetException
ClassNotFoundException
NoSuchMethodException

showSyntax

public final String showSyntax()
Returns the input syntax to be used for invoking this command from the command prompt.

Returns:
the input syntax for this command