edu.wisc.ssec.mcidasv.jython
Class Runner

java.lang.Object
  extended by java.lang.Thread
      extended by edu.wisc.ssec.mcidasv.jython.Runner
All Implemented Interfaces:
Runnable

public class Runner
extends Thread

This class represents a specialized Thread that creates and executes Commands. A BlockingQueue is used to maintain thread safety and to cause a Runner to wait when the queue is at capacity or has no Commands to execute.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
private  Console console
           
private  Interpreter interpreter
          The Jython interpreter that will actually run the queued commands.
private  boolean interrupted
          Not in use yet.
private static org.slf4j.Logger logger
           
private  BlockingQueue<Command> queue
          Queue of Commands awaiting execution.
private static int QUEUE_CAPACITY
          The maximum number of Commands that can be queued.
private  OutputStreamDemux STD_ERR
          Acts like a global error stream that redirects data to whichever Console matches the current thread name.
private  OutputStreamDemux STD_OUT
          Acts like a global output stream that redirects data to whichever Console matches the current thread name.
private  org.python.core.PySystemState systemState
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Runner(Console console)
           
Runner(Console console, List<String> commands)
           
 
Method Summary
protected  org.python.core.PyStringMap copyLocals()
          Fetches, copies, and returns the interpreter's local namespace.
 void queueBatch(String source, List<String> batch)
          Queues up a series of Jython statements.
private  void queueCommand(Command command)
          Queues up a command for execution.
 void queueFile(String name, String path)
          Queues up a Jython file to be run by interpreter.
 void queueLine(String line)
          Queues up a line of Jython for execution.
 void queueObject(String name, Object object)
          Queues the addition of an object to interpreter's local namespace.
 void queueRemoval(String name)
          Queues the removal of an object from interpreter's local namespace.
 void run()
          Takes commands out of the queue and executes them.
protected  void setCallbackHandler(ConsoleCallback newCallback)
          Registers a new callback handler.
 String toString()
           
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, 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, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

logger

private static final org.slf4j.Logger logger

QUEUE_CAPACITY

private static final int QUEUE_CAPACITY
The maximum number of Commands that can be queued.

See Also:
Constant Field Values

STD_OUT

private final OutputStreamDemux STD_OUT
Acts like a global output stream that redirects data to whichever Console matches the current thread name.


STD_ERR

private final OutputStreamDemux STD_ERR
Acts like a global error stream that redirects data to whichever Console matches the current thread name.


queue

private final BlockingQueue<Command> queue
Queue of Commands awaiting execution.


console

private final Console console

systemState

private final org.python.core.PySystemState systemState

interpreter

private final Interpreter interpreter
The Jython interpreter that will actually run the queued commands.


interrupted

private boolean interrupted
Not in use yet.

Constructor Detail

Runner

public Runner(Console console)
Parameters:
console -

Runner

public Runner(Console console,
              List<String> commands)
Parameters:
console -
commands -
Method Detail

setCallbackHandler

protected void setCallbackHandler(ConsoleCallback newCallback)
Registers a new callback handler. Currently this only forwards the new handler to Interpreter.setCallbackHandler(ConsoleCallback).

Parameters:
newCallback - The callback handler to register.

copyLocals

protected org.python.core.PyStringMap copyLocals()
Fetches, copies, and returns the interpreter's local namespace.

Returns:
Copy of the interpreter's local namespace.

run

public void run()
Takes commands out of the queue and executes them. We get a lot of mileage out of BlockingQueue; it's thread-safe and will block if the queue is at capacity or empty.

Please note that this method needs to be the first method that gets called after creating a Runner.

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

queueBatch

public void queueBatch(String source,
                       List<String> batch)
Queues up a series of Jython statements. Currently each command is treated as though the current user just entered it; the command appears in the input along with whatever output the command generates.

Parameters:
source - Batched command source. Anything but null is acceptable.
batch - The actual commands to execute.

queueLine

public void queueLine(String line)
Queues up a line of Jython for execution.

Parameters:
line - Text of the command.

queueObject

public void queueObject(String name,
                        Object object)
Queues the addition of an object to interpreter's local namespace.

Parameters:
name - Object name as it will appear to interpreter.
object - Object to put in interpreter's local namespace.

queueRemoval

public void queueRemoval(String name)
Queues the removal of an object from interpreter's local namespace.

Parameters:
name - Name of the object to be removed, as it appears to Jython.
See Also:
queueObject(String, Object)

queueFile

public void queueFile(String name,
                      String path)
Queues up a Jython file to be run by interpreter.

Parameters:
name - __name__ attribute to use for loading path.
path - The path to the Jython file.

queueCommand

private void queueCommand(Command command)
Queues up a command for execution.

Parameters:
command - Command to place in the execution queue.

toString

public String toString()
Overrides:
toString in class Thread