com.ca.jcs.util
Class ExecHelper

java.lang.Object
  extended by com.ca.jcs.util.ExecHelper

public class ExecHelper
extends Object

Spawns a process and then allows easy access to the stderr output it generates as a string. Stdout output can be accessed in one of two ways:

  1. If stdoutRecordSeparator is not null, then getStdoutRecordQueue() will return a blocking queue from which the client can retrieve records as they become available. When EOF is returned the queue has been drained completely.
If stdoutRecordSeparator is null, then getStdoutOutput() will return all of the text on the spawned process' stdout in a single string.


Nested Class Summary
protected  class ExecHelper.StreamFeeder
           
protected  class ExecHelper.StreamGobbler
           
 
Field Summary
protected  String cmd
           
static String EOF
           
static String FEEDER_CLS
           
protected  Condition feederStartCondition
           
protected  Lock feederStartLock
           
static String GOBBLE_CLS
           
protected  Logger log
           
static int numGobblers
           
protected  String postfix
           
protected  String prefix
           
protected  String prompt
           
protected  String response
           
protected  BlockingQueue<String> stderrQueue
           
protected  String stdinEofMsg
           
protected  BlockingQueue<String> stdinQueue
           
protected  com.ca.commons.functional.Fn<? super String,String> stdinStringMapper
           
protected  BlockingQueue<String> stdoutRecordQueue
           
protected  String stdoutRecordSeparator
           
 
Constructor Summary
ExecHelper()
          Create a simple exec helper where stdout and stderr text are returned as separate strings.
ExecHelper(BlockingQueue<String> stdinQueue, com.ca.commons.functional.Fn<? super String,String> mapper, String eofMsg, String stdoutRecordSeparator, int maxSize, Logger log)
          Same as ExecHelper(String, int, org.slf4j.Logger) except allows input to be read from a blocking queue and for processing of text as it is read from this queue, before being written to the input stream of the spawned process.
ExecHelper(Logger log)
          Create a simple exec helper where stdout and stderr text are returned as separate strings and with logger provided.
ExecHelper(String stdoutRecordSeparator, int maxSize, Logger log)
          Create an exec helper where stderr text is returned as a simple string, but stdout text is broken up into separate records based on stdoutRecordSeparator and placed on a blocking queue with maximum size maxSize. which can be retrieved by getStdoutRecordQueue().
 
Method Summary
 String getStderrOutput()
           
 BlockingQueue<String> getStderrQueue()
           
 BlockingQueue<String> getStdinQueue()
           
 com.ca.commons.functional.Fn<? super String,String> getStdinStringMapper()
           
 String getStdoutOutput()
           
 BlockingQueue<String> getStdoutRecordQueue()
           
 String getStdoutRecordSeparator()
           
 int run(List<String> exec)
           
 int run(String exec)
           
protected  int runStreams(Process proc)
           
 void setPrompt(String prompt)
           
 void setResponse(String response)
           
protected  void signalFeederStart()
           
protected  void waitForFeederStartSignal()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EOF

public static final String EOF
See Also:
Constant Field Values

prefix

protected String prefix

postfix

protected String postfix

stdoutRecordSeparator

protected String stdoutRecordSeparator

stdinQueue

protected BlockingQueue<String> stdinQueue

stdinStringMapper

protected com.ca.commons.functional.Fn<? super String,String> stdinStringMapper

stdinEofMsg

protected String stdinEofMsg

stderrQueue

protected BlockingQueue<String> stderrQueue

stdoutRecordQueue

protected BlockingQueue<String> stdoutRecordQueue

response

protected String response

prompt

protected String prompt

log

protected Logger log

cmd

protected String cmd

feederStartLock

protected final Lock feederStartLock

feederStartCondition

protected final Condition feederStartCondition

numGobblers

public static int numGobblers

GOBBLE_CLS

public static final String GOBBLE_CLS

FEEDER_CLS

public static final String FEEDER_CLS
Constructor Detail

ExecHelper

public ExecHelper()
Create a simple exec helper where stdout and stderr text are returned as separate strings.


ExecHelper

public ExecHelper(Logger log)
Create a simple exec helper where stdout and stderr text are returned as separate strings and with logger provided.


ExecHelper

public ExecHelper(String stdoutRecordSeparator,
                  int maxSize,
                  Logger log)
Create an exec helper where stderr text is returned as a simple string, but stdout text is broken up into separate records based on stdoutRecordSeparator and placed on a blocking queue with maximum size maxSize. which can be retrieved by getStdoutRecordQueue().


ExecHelper

public ExecHelper(BlockingQueue<String> stdinQueue,
                  com.ca.commons.functional.Fn<? super String,String> mapper,
                  String eofMsg,
                  String stdoutRecordSeparator,
                  int maxSize,
                  Logger log)
Same as ExecHelper(String, int, org.slf4j.Logger) except allows input to be read from a blocking queue and for processing of text as it is read from this queue, before being written to the input stream of the spawned process.

Method Detail

setResponse

public void setResponse(String response)

setPrompt

public void setPrompt(String prompt)

run

public int run(List<String> exec)
        throws Exception
Throws:
Exception

run

public int run(String exec)
        throws Exception
Throws:
Exception

getStdoutOutput

public String getStdoutOutput()

getStdinQueue

public BlockingQueue<String> getStdinQueue()

getStdinStringMapper

public com.ca.commons.functional.Fn<? super String,String> getStdinStringMapper()

getStdoutRecordSeparator

public String getStdoutRecordSeparator()

getStdoutRecordQueue

public BlockingQueue<String> getStdoutRecordQueue()

getStderrQueue

public BlockingQueue<String> getStderrQueue()

getStderrOutput

public String getStderrOutput()

runStreams

protected int runStreams(Process proc)
                  throws InterruptedException
Throws:
InterruptedException

waitForFeederStartSignal

protected void waitForFeederStartSignal()
                                 throws InterruptedException
Throws:
InterruptedException

signalFeederStart

protected void signalFeederStart()


Created 2011-07-14 13:27 EST