jApp.jdic
Class Shell

java.lang.Object
  extended by jApp.jdic.Shell

public class Shell
extends java.lang.Object

Implements a simple wrapper around the RunTime.exec() method that allows easy capture of command results (that is 'stdout' and 'stderr' and feeding the command via 'stdin'.

Author:
Kustaa Nyholm

Constructor Summary
Shell()
           
 
Method Summary
 java.lang.String execute(java.lang.String cmd, java.lang.String input)
          Executes a given command line with default time out of five seconds.
 java.lang.String execute(java.lang.String cmd, java.lang.String input, int timeout)
          Executes a given command line, aborting after given timeout.
 java.lang.String getCommand()
          Gets the last command that has been executed (succesfully or not in other word the command line that returned the exit code you can query with getExitCode()
 java.lang.Exception getException()
          Gets the exception that was thrown during, before or after the execution of the command line within the execute method, if any.
 int getExitCode()
          Gets the exit code from the last command executed, should be checked after each invokation of execute to verify success.
 java.lang.String sudoExecute(java.lang.String cmd, java.lang.String input, java.lang.String password)
          Executes a given command line with root rights and default time out of five seconds.
 java.lang.String sudoExecute(java.lang.String cmd, java.lang.String input, java.lang.String password, int timeout)
          Executes a given command line with root rights.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Shell

public Shell()
Method Detail

sudoExecute

public java.lang.String sudoExecute(java.lang.String cmd,
                                    java.lang.String input,
                                    java.lang.String password)
                             throws java.lang.Exception
Executes a given command line with root rights and default time out of five seconds.

Parameters:
cmd - the command line to execute
input - the input to feed to the command via 'stdin', can be null
Returns:
the combined output of 'stdout' and 'stderr' from the command
Throws:
java.lang.Exception
See Also:
execute(String, String,int)

sudoExecute

public java.lang.String sudoExecute(java.lang.String cmd,
                                    java.lang.String input,
                                    java.lang.String password,
                                    int timeout)
                             throws java.lang.Exception
Executes a given command line with root rights.

Parameters:
cmd - the command line to execute
input - the input to feed to the command via 'stdin', can be null
timeout - in milliseconds after which the process is aborted, use zero for no timeout
Returns:
the combined output of 'stdout' and 'stderr' from the command
Throws:
java.lang.Exception
See Also:
execute(String, String,int)

execute

public java.lang.String execute(java.lang.String cmd,
                                java.lang.String input)
Executes a given command line with default time out of five seconds.

Parameters:
cmd - the command line to execute
input - the input to feed to the command via 'stdin', can be null
Returns:
the combined output of 'stdout' and 'stderr' from the command
See Also:
execute(String, String,int)

execute

public java.lang.String execute(java.lang.String cmd,
                                java.lang.String input,
                                int timeout)
Executes a given command line, aborting after given timeout. After calling this method you should check for error by calling the getExitCode(). A non zero positive value indicates an error, typically Unix/Linux processes can only use values in the range 0..255 inclusive. If an exception was thrown during the call execute method the getExitCode() returns -1 and you can get the exception with the getException() call

Parameters:
cmd - the command line to execute
input - the input to feed to the command via 'stdin', can be null
timeout - in milliseconds after which the process is aborted, use zero for no timeout
Returns:
the combined output of 'stdout' and 'stderr' from the command
See Also:
getExitCode(), getCommand(), getException()

getExitCode

public int getExitCode()
Gets the exit code from the last command executed, should be checked after each invokation of execute to verify success. Typically 0 value indicates success, no Unix/Linux script/program should not be able to return a code outside 0..255, a return code of -1 indicates an exception in attempting to execute the command line in other words an exception was thrown before or during execution of the command and it was not possible to capture the actual exit code from the command.

Returns:
the exit code

getCommand

public java.lang.String getCommand()
Gets the last command that has been executed (succesfully or not in other word the command line that returned the exit code you can query with getExitCode()

Returns:
the command line

getException

public java.lang.Exception getException()
Gets the exception that was thrown during, before or after the execution of the command line within the execute method, if any. If no exception was thrown, returns null.

Returns:
the exception or null