de.uniba.wiai.lspi.util.console
Class CommandFactory

java.lang.Object
  extended by de.uniba.wiai.lspi.util.console.CommandFactory

public class CommandFactory
extends java.lang.Object

Factory responsible to create instances of Command given the commands name. The factory must be supplied with a mapping from the commands' names to the commands' classes, which are subclasses of Command. The mapping is provided through a Hashtable containing keys (Strings) representing command names and values (Strings) representing class names.

For example:
MyClass toCommand = new MyClass(...);
Hashtable myMapping = new Hashtable();
myMapping.put("exit", MyExitCommand.class.getName());
...
CommandFactory factory = new CommandFactory(toCommand, myMapping);

On creation the factory is provided with one or more Objects, on which the commands will be executed. Additionally the mapping from command names to command classes implementing the command must be provided to the CommandFactory. This factory is used by ConsoleThread.

Version:
1.0.5
Author:
sven

Constructor Summary
CommandFactory(java.lang.Object[] toCommand, java.io.PrintStream out, java.util.Map<java.lang.String,java.lang.String> commandMapping)
          Creates a new instance of CommandFactory.
 
Method Summary
 void addCommand(java.lang.String name, java.lang.String cmdClass)
          Add the given Command cmd to this command factory.
 Command createCommand(java.lang.String commandLine)
          Creates the Command instance corresponding to the given command name.
 java.io.PrintStream getPrintStream()
          Get the PrintStream, to that all Commands created by this factory, print their output to.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CommandFactory

public CommandFactory(java.lang.Object[] toCommand,
                      java.io.PrintStream out,
                      java.util.Map<java.lang.String,java.lang.String> commandMapping)
Creates a new instance of CommandFactory.

Parameters:
out - The PrintStream, to that all Commands created by this factory, print their output to.
toCommand - The Objects to execute commands on.
commandMapping - The mapping from command name to command class. This must not be null or have a size of zero!
Method Detail

addCommand

public void addCommand(java.lang.String name,
                       java.lang.String cmdClass)
Add the given Command cmd to this command factory.

Parameters:
name - The name of the command to add.
cmdClass - The class name of Command to add.

getPrintStream

public java.io.PrintStream getPrintStream()
Get the PrintStream, to that all Commands created by this factory, print their output to.

Returns:
The PrintStream.

createCommand

public Command createCommand(java.lang.String commandLine)
                      throws ConsoleException
Creates the Command instance corresponding to the given command name. Therefore the given command line is parsed with help of CommandParser, the command name and parameters extracted from it, the correspondig Command created and the parameters passed to the command. Then the command is returned.

Parameters:
commandLine - The command line entered into the console (See ConsoleThread).
Returns:
The instance of the command corresponding to given commandLine.
Throws:
ConsoleException - Any Exception during creation of command.