Package com.sun.grid.util.expect

# Class Expect

java.lang.Object
com.sun.grid.util.expect.Expect

public class Expect extends Object
Simple helper class for handling output of a process. It works similar as the tcl expect command.

### Example

        char [] password = null;
        String username = null;
        long timeout = 60 * 1000;
        String [] cmd = new String[] { "su", "username", "-c" , "ls" };

        Expect expect = new Expect(cmd);

        expect.addHandler(new ExpectPasswordHandler("password: ", password));

        expect.addHandler(new ExpectHandler() {

            // print every line after the password question to System.out
            public void handle(Expect expect, ExpectBuffer buffer) {

                String line = buffer.consumeLine();
                if(line != null) {
                   System.out.println(line);
                }

            }
        });

        if(expect.exec(timeout) == 0 ) {
           System.out.println("ls as user " + username + " successfully executed");
        }

## Constructor Summary

Constructors
Constructor
Description
`Expect()`
`Expect(String[] cmd)`
Creates a new instance of Expect

## Method Summary

Modifier and Type
Method
Description
`void`
`add(ExpectHandler handler)`
Add a new expect handler.
`List`
`command()`
`List`
`env()`
`int`
`exec(long timeout)`
Execute the command and call the registered handlers.
`void`
`flush()`
Flush stdin
`void`
`print(boolean b)`
print a boolean to stdin
`void`
`print(char c)`
print a char to stdin.
`void`
`print(char[] s)`
print a char arrary to stdin !!!Attention!!!: For security reasons this method should not be used to send passwords to the process, sice the content of `s` is written to the logger.
`void`
`print(double d)`
print a double to stdin
`void`
`print(float x)`
print a float to stdin.
`void`
`print(int i)`
print an integer to stdin
`void`
`print(long l)`
print a long to stdin
`void`
`print(Object obj)`
print an object to stdin
`void`
`print(String s)`
print a string to stdin
`void`
`println()`
print linefeed to stdin.
`void`
`println(boolean x)`
print a boolean to stdin (with linefeed)
`void`
`println(char x)`
print a char to stdin.
`void`
`println(char[] x)`
print a char arrary to stdin (with line feed) !!!Attention!!!: For security reasons this method should not be used to send passwords to the process, sice the content of `x` is written to the logger.
`void`
`println(double x)`
print a double to stdin (with linefeed)
`void`
`println(float x)`
print a float to stdin (with linefeed).
`void`
`println(int x)`
print an integer to stdin (with linefeed)
`void`
`println(long x)`
print a long to stdin (with linefeed)
`void`
`println(Object x)`
print an object to stdin (with linefeed)
`void`
`println(String x)`
print a string to stdin (with line separator)
`void`
`printlnPassword(char[] s)`
print a password to stdin (with linefeed)
`void`
`printPassword(char[] s)`
print a password to stdin

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### Expect

public Expect()

### Expect

public Expect(String[] cmd)
Creates a new instance of Expect

Parameters:
  `cmd` - the command which will be executed
See Also:
  - `Runtime.exec(String[])`

## Method Details

### command

public List command()

### env

public List env()

### add

public void add(ExpectHandler handler)
Add a new expect handler.

Parameters:
  `handler` - the expect handler

### exec

public int exec(long timeout) throws IOException, InterruptedException
Execute the command and call the registered handlers.

Parameters:
  `timeout` - timout for the command in milliseconds
Returns:
  the exit code of the command
Throws:
  `IOException` - if the execution of the command failed
  `InterruptedException` - if the current thread has been interrupted before the command has been finished.

### flush

public void flush()
Flush stdin

### println

public void println(String x)
print a string to stdin (with line separator)

Parameters:
  `x` - the string

### print

public void print(String s)
print a string to stdin

Parameters:
  `s` - the string

### println

public void println(Object x)
print an object to stdin (with linefeed)

Parameters:
  `x` - the object

### print

public void print(Object obj)
print an object to stdin

Parameters:
  `obj` - the object

### println

public void println(int x)
print an integer to stdin (with linefeed)

Parameters:
  `x` - the integer

### print

public void print(int i)
print an integer to stdin

Parameters:
  `i` - the integer

### print

public void print(boolean b)
print a boolean to stdin

Parameters:
  `b` - the boolean

### println

public void println(boolean x)
print a boolean to stdin (with linefeed)

Parameters:
  `x` - the boolean

### println

public void println(long x)
print a long to stdin (with linefeed)

Parameters:
  `x` - the long

### print

public void print(long l)
print a long to stdin

Parameters:
  `l` - the long

### println

public void println(double x)
print a double to stdin (with linefeed)

Parameters:
  `x` - the double

### println

public void println(char[] x)
print a char arrary to stdin (with line feed) !!!Attention!!!: For security reasons this method should not be used to send passwords to the process, sice the content of `x` is written to the logger.

Parameters:
  `x` - the char array

### print

public void print(char[] s)
print a char arrary to stdin !!!Attention!!!: For security reasons this method should not be used to send passwords to the process, sice the content of `s` is written to the logger.

Parameters:
  `s` - the char array

### printPassword

public void printPassword(char[] s)
print a password to stdin

Parameters:
  `s` - the password

### printlnPassword

public void printlnPassword(char[] s)
print a password to stdin (with linefeed)

Parameters:
  `s` - the password

### print

public void print(double d)
print a double to stdin

Parameters:
  `d` - the double

### println

public void println(char x)
print a char to stdin.

Parameters:
  `x` - the char

### print

public void print(char c)
print a char to stdin.

Parameters:
  `c` - the char

### println

public void println(float x)
print a float to stdin (with linefeed).

Parameters:
  `x` - the float

### print

public void print(float x)
print a float to stdin.

Parameters:
  `x` - the float

### println

public void println()
print linefeed to stdin.