info.rolandkrueger.roklib.cli
Class ConsoleThread

java.lang.Object
  extended by java.lang.Thread
      extended by info.rolandkrueger.roklib.cli.ConsoleThread
All Implemented Interfaces:
Runnable

public class ConsoleThread
extends Thread

This is a class that provides a shell similar to those of programs like gdb or bc. This class is running in a thread. It simply reads one line after the other from an input stream and hands it over to a given handler. This handler, which must implement the interface CommandHandler, can then process the command string. The input stream from which the user commands are read and the output stream to which the console's output is written are configurable. If these streams are not specified they default to the standard input and standard ouput. Thus, it is possible to read in commands from a Swing frame, for instance.

Author:
Roland Krueger
See Also:
CommandHandler, ConsoleCommandObject

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
private  CommandHandler mCommandHandler
           
private  String mGreetingMsg
           
private  InputStream mInputStream
           
private  PrintStream mOutput
           
private  Object mPrompt
           
private  BufferedReader mReader
           
private  boolean mShutdownRequested
           
private static Logger sLogger
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
private ConsoleThread()
          Default constructor.
  ConsoleThread(CommandHandler handler, String greetingMsg, Object prompt, OutputStream outStream, InputStream inStream)
          Constructs a new command console with configurable input and output streams.
  ConsoleThread(CommandHandler handler, String greetingMsg, String prompt)
          Constructs a new command console with stdin and stdout as the default input and output streams.
 
Method Summary
 InputStream getInputStream()
          Returns the input stream from which the user's input is read from.
 OutputStream getOutputStream()
          Returns the output stream where the console's output is written to.
 Object getPrompt()
          Returns the object that represents the console prompt.
 void run()
           
 void setInputStream(InputStream inStream)
          Sets the input stream from which the user's input is read from.
 void setOutputStream(OutputStream outStream)
          Sets the output stream where the console's output is written to.
 void setPrompt(Object newPrompt)
          Changes the console's prompt to a new prompt object.
 void shutdown()
          Closes the console and stops the associated thread.
private  void startThread()
          Starts the console thread.
 
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

sLogger

private static Logger sLogger

mShutdownRequested

private boolean mShutdownRequested

mCommandHandler

private CommandHandler mCommandHandler

mGreetingMsg

private String mGreetingMsg

mPrompt

private Object mPrompt

mReader

private BufferedReader mReader

mInputStream

private InputStream mInputStream

mOutput

private PrintStream mOutput
Constructor Detail

ConsoleThread

private ConsoleThread()
Default constructor.


ConsoleThread

public ConsoleThread(CommandHandler handler,
                     String greetingMsg,
                     Object prompt,
                     OutputStream outStream,
                     InputStream inStream)
Constructs a new command console with configurable input and output streams.

Parameters:
handler - Class that gets passed the user input to process it further.
greetingMsg - A greeting message that is shown when the console starts up.
prompt - The command prompt. Can be something like '$' or '>' or a mutable object. The toString() method of the prompt-object is used to determine the string to be used as command prompt. For example, use a date object here that will return the current date and time in its toString() method.
outStream - Output stream that will be used to write the command prompt to. Note that the output of specific commands will not be written to this output stream if this is not configured by the respective command objects. See org.pi4.simplesim.util.CommandObject for further information.
inStream - Input stream that is used to read the user's input from.

ConsoleThread

public ConsoleThread(CommandHandler handler,
                     String greetingMsg,
                     String prompt)
Constructs a new command console with stdin and stdout as the default input and output streams.

Parameters:
handler - Class that gets passed the user input to process it further.
greetingMsg - A greeting message that is shown when the console starts up.
prompt - The command prompt. Can be something like '$' or '>' or a mutable object. The toString() method of the prompt-object is used to determine the string to be used as command prompt. For example, use a date object here that will return the current date and time in its toString() method.
Method Detail

startThread

private void startThread()
Starts the console thread.


run

public void run()
Specified by:
run in interface Runnable
Overrides:
run in class Thread

setPrompt

public void setPrompt(Object newPrompt)
Changes the console's prompt to a new prompt object. The toString() method of the prompt object is used to determine the string to be used as the command prompt.

Parameters:
newPrompt - the new command line prompt to be used by this console.

getPrompt

public Object getPrompt()
Returns the object that represents the console prompt. This may be a string with a character sequence, such as ">", for example. To print out the prompt on the console, the prompt object's toString method is used.

Returns:
the object that represents the console prompt

shutdown

public void shutdown()
Closes the console and stops the associated thread.


getOutputStream

public OutputStream getOutputStream()
Returns the output stream where the console's output is written to.

Returns:
the output stream where the console's output is written to.

getInputStream

public InputStream getInputStream()
Returns the input stream from which the user's input is read from.

Returns:
the input stream from which the user's input is read from.

setOutputStream

public void setOutputStream(OutputStream outStream)
Sets the output stream where the console's output is written to.

Parameters:
outStream - the output stream where the console's output is written to.

setInputStream

public void setInputStream(InputStream inStream)
Sets the input stream from which the user's input is read from.

Parameters:
inStream - the input stream from which the user's input is read from.


Copyright © 2007-2011. All Rights Reserved.