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

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

public class CommandFactory
extends 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.


Field Summary
protected  Map<String,String> commandMapping
          The mapping from command name to command class name.
protected  Map<String,Command> instanceMap
          Map containing already instantiated commands.
protected  PrintStream out
          The PrintStream to that all Commands print their outputs.
protected  Object[] toCommand
          The Objects given to the Commands to execute commands on.
 
Constructor Summary
CommandFactory(Object[] toCommand, PrintStream out, Map<String,String> commandMapping)
          Creates a new instance of CommandFactory.
 
Method Summary
 void addCommand(String name, String cmdClass)
          Add the given Command cmd to this command factory.
 Command createCommand(String commandLine)
          Creates the Command instance corresponding to the given command name.
 PrintStream getPrintStream()
          Get the PrintStream, to that all Commands created by this factory, print their output to.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

toCommand

protected Object[] toCommand
The Objects given to the Commands to execute commands on.


commandMapping

protected Map<String,String> commandMapping
The mapping from command name to command class name.


instanceMap

protected Map<String,Command> instanceMap
Map containing already instantiated commands. Key: name of command, Value: instance of Command .


out

protected PrintStream out
The PrintStream to that all Commands print their outputs.

Constructor Detail

CommandFactory

public CommandFactory(Object[] toCommand,
                      PrintStream out,
                      Map<String,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(String name,
                       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 PrintStream getPrintStream()
Get the PrintStream, to that all Commands created by this factory, print their output to.

Returns:
The PrintStream.

createCommand

public Command createCommand(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.


Copyright © 2004-2008 Karsten Loesing, Sven Kaffille - Distributed and Mobile Systems Group, Lehrstuhl für Praktische Informatik, Universität Bamberg. All Rights Reserved.