expectj
Class Spawn

java.lang.Object
  extended by expectj.Spawn

public class Spawn
extends Object

This class is used for talking to processes / ports. This will also interact with the process to read and write to it.

Author:
Sachin Shekar Shetty

Method Summary
 void expect(String pattern)
          Wait for a pattern to appear on standard out.
 void expect(String pattern, long timeOutSeconds)
          Wait for a pattern to appear on standard out.
 void expectClose()
          Wait the default timeout for the spawned process to finish.
 void expectClose(long timeOutSeconds)
          Wait for the spawned process to finish.
 void expectErr(String pattern)
          Wait for a pattern to appear on standard error.
 void expectErr(String pattern, long timeOutSeconds)
          Wait for a pattern to appear on standard error.
 String getCurrentStandardErrContents()
          Returns everything that has been received on the spawn's stderr during this session.
 String getCurrentStandardOutContents()
          Returns everything that has been received on the spawn's stdout during this session.
 int getExitValue()
          Retrieve the exit code of a finished process.
 void interact()
          Allows the user to interact with the spawned process.
 boolean isClosed()
          This method can be use use to check the target process status before invoking send(String)
 void send(String string)
          Writes a string to the standard input of the spawned process.
 void stop()
          This method kills the process represented by SpawnedProcess object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

expect

public void expect(String pattern,
                   long timeOutSeconds)
            throws IOException,
                   TimeoutException
Wait for a pattern to appear on standard out.

Parameters:
pattern - The case-insensitive substring to match against.
timeOutSeconds - The timeout in seconds before the match fails.
Throws:
IOException - on IO trouble waiting for pattern
TimeoutException - on timeout waiting for pattern

expectClose

public void expectClose(long timeOutSeconds)
                 throws TimeoutException,
                        ExpectJException
Wait for the spawned process to finish.

Parameters:
timeOutSeconds - The number of seconds to wait before giving up, or -1 to wait forever.
Throws:
ExpectJException - if we're interrupted while waiting for the spawn to finish.
TimeoutException - if the spawn didn't finish inside of the timeout.
See Also:
expectClose()

expectClose

public void expectClose()
                 throws ExpectJException,
                        TimeoutException
Wait the default timeout for the spawned process to finish.

Throws:
ExpectJException - If something fails.
TimeoutException - if the spawn didn't finish inside of the default timeout.
See Also:
expectClose(long), ExpectJ.ExpectJ(long)

expectErr

public void expectErr(String pattern,
                      long timeOutSeconds)
               throws IOException,
                      TimeoutException
Wait for a pattern to appear on standard error.

Parameters:
pattern - The case-insensitive substring to match against.
timeOutSeconds - The timeout in seconds before the match fails.
Throws:
TimeoutException - on timeout waiting for pattern
IOException - on IO trouble waiting for pattern
See Also:
expect(String, long)

expect

public void expect(String pattern)
            throws IOException,
                   TimeoutException
Wait for a pattern to appear on standard out.

Parameters:
pattern - The case-insensitive substring to match against.
Throws:
TimeoutException - on timeout waiting for pattern
IOException - on IO trouble waiting for pattern

expectErr

public void expectErr(String pattern)
               throws IOException,
                      TimeoutException
Wait for a pattern to appear on standard error.

Parameters:
pattern - The case-insensitive substring to match against.
Throws:
TimeoutException - on timeout waiting for pattern
IOException - on IO trouble waiting for pattern
See Also:
expect(String)

isClosed

public boolean isClosed()
This method can be use use to check the target process status before invoking send(String)

Returns:
true if the process has already exited.

getExitValue

public int getExitValue()
                 throws ExpectJException
Retrieve the exit code of a finished process.

Returns:
the exit code of the process if the process has already exited.
Throws:
ExpectJException - if the spawn is still running.

send

public void send(String string)
          throws IOException
Writes a string to the standard input of the spawned process.

Parameters:
string - The string to send. Don't forget to terminate it with \n if you want it linefed.
Throws:
IOException - on IO trouble talking to spawn

interact

public void interact()
Allows the user to interact with the spawned process.


stop

public void stop()
This method kills the process represented by SpawnedProcess object.


getCurrentStandardOutContents

public String getCurrentStandardOutContents()
Returns everything that has been received on the spawn's stdout during this session.

Returns:
the available contents of Standard Out

getCurrentStandardErrContents

public String getCurrentStandardErrContents()
Returns everything that has been received on the spawn's stderr during this session.

Returns:
the available contents of Standard Err


Copyright © 2010. All Rights Reserved.