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

java.lang.Object
  extended by java.lang.Thread
      extended by de.uniba.wiai.lspi.util.console.ConsoleThread
All Implemented Interfaces:
Runnable

public class ConsoleThread
extends Thread


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
protected  String consoleName
          The name of this console.
protected  Command exitCommand
          The Command that exits the console.
protected  CommandFactory factory
          The factory responsible for Creation of commands.
protected  PrintStream out
          The PrintStream to print the consoles output to.
protected  PrintStream systemOut
          The old PrintStream to print the standard output to.
protected  OutputStream systemOutputStream
          Reference to the standard output stream.
protected  String welcome
          The text displayed, when the Thread is started.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
protected ConsoleThread(String name, CommandFactory f)
          Creates a new instance of ConsoleThread
protected ConsoleThread(String name, CommandFactory f, OutputStream systemOut)
          Creates a new instance of ConsoleThread.
 
Method Summary
 CommandFactory getCommandFactory()
          Get a reference to the CommandFactory used by this console.
static ConsoleThread getConsole()
          Method to obtain a reference to the console currently active in this JVM.
static ConsoleThread getConsole(String name, CommandFactory factory)
          Factory method to get a reference to the console singleton.
static ConsoleThread getConsole(String name, CommandFactory factory, OutputStream systemOut)
          Factory method to create a console.
 PrintStream getPrintStream()
          Get a reference to the PrintStream this console prints its output to.
 OutputStream getSystemOutputStream()
          Get a reference to the OutputStream calls to System.out are delegated to.
 void run()
          The run method.
 void setExitCommand(String commandName)
          Set the Command that exits this console.
 void setWelcomeText(String text)
          Set a costum welcome text for the console.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

consoleName

protected String consoleName
The name of this console. Displayed before any input.


factory

protected CommandFactory factory
The factory responsible for Creation of commands.


welcome

protected String welcome
The text displayed, when the Thread is started.


exitCommand

protected Command exitCommand
The Command that exits the console. Its execute method is invoked and the console exits.


out

protected PrintStream out
The PrintStream to print the consoles output to.


systemOut

protected PrintStream systemOut
The old PrintStream to print the standard output to. Is null if this console does not change the standard output via constructor ConsoleThread(String, CommandFactory).


systemOutputStream

protected OutputStream systemOutputStream
Reference to the standard output stream.

Constructor Detail

ConsoleThread

protected ConsoleThread(String name,
                        CommandFactory f)
Creates a new instance of ConsoleThread

Parameters:
name - The name of this console. Displayed before any input.
f - The CommandFactory responsible for creating Commands.

ConsoleThread

protected ConsoleThread(String name,
                        CommandFactory f,
                        OutputStream systemOut)
Creates a new instance of ConsoleThread. The standard output System.out is redirected to systemOut.

Parameters:
name - The name of this console. Displayed before any input.
f - The CommandFactory responsible for creating Commands.
systemOut - The OutputStream to redirect standard ouput to. If you do not want to have any output send to System.out, you can use DummyOutputStream. If you want to save output send to System.out in Memory to e.g. display it later you can use MemoryOutputStream. To write output to a file you can use FileOutputStream.
Method Detail

getConsole

public static ConsoleThread getConsole()
Method to obtain a reference to the console currently active in this JVM. Returns null, if there is none.

Returns:
Reference to the singleton console thread.

getConsole

public static ConsoleThread getConsole(String name,
                                       CommandFactory factory)
Factory method to get a reference to the console singleton. Creates a new instance if there is no console in the JVM. If there is currently one console the arguments provided to this method have no effect.

Parameters:
name -
factory -
Returns:
Reference to the singleton console thread.

getConsole

public static ConsoleThread getConsole(String name,
                                       CommandFactory factory,
                                       OutputStream systemOut)
Factory method to create a console. Creates a new instance if there is no console in the JVM. If there is currently one console the arguments provided to this method have no effect.

Parameters:
name -
factory -
systemOut -
Returns:
Reference to the singleton console thread.

getPrintStream

public PrintStream getPrintStream()
Get a reference to the PrintStream this console prints its output to.

Returns:
Reference to the PrintStream this console prints its output to.

getSystemOutputStream

public OutputStream getSystemOutputStream()
Get a reference to the OutputStream calls to System.out are delegated to. Returns null if System.out has not been redirected.

Returns:
Reference to the OutputStream calls to System.out are delegated to. Returns null if System.out has not been redirected.

getCommandFactory

public CommandFactory getCommandFactory()
Get a reference to the CommandFactory used by this console.

Returns:
Reference to the CommandFactory used by this console.

setWelcomeText

public void setWelcomeText(String text)
Set a costum welcome text for the console.

Parameters:
text - The welcome text to set.

run

public void run()
The run method. Loops until the exitCommand has been invoked.

Specified by:
run in interface Runnable
Overrides:
run in class Thread

setExitCommand

public void setExitCommand(String commandName)
                    throws ConsoleException
Set the Command that exits this console. Uses the CommandFactory to create an instance of the Command.

Parameters:
commandName - The name of the command.
Throws:
ConsoleException - Exception during creation of the command.


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