FileDocCategorySizeDatePackage
ProcessExecutor.javaAPI DocGlassfish v2 API14050Fri May 04 22:34:44 BST 2007com.sun.enterprise.diagnostics.collect

ProcessExecutor

public class ProcessExecutor extends Object
ProcessExecutor executes any OS specific commands as a sub process but does not wait more than a specified timeout value for the process to complete. This is a wrapper over java.lang.Runtime.getRuntime().exec()

Fields Summary
static final Logger
logger
private String[]
command
private long
timeout
private ProcessRunner
runner
Constructors Summary
public ProcessExecutor(String[] cmd, long timeout)
Create a new process executor object.

param
cmd Command to be executed. See java.lang.Runtime.exec(String[]) for more information.
param
timeout time to wait for the process to complete. This is in milliseconds
throws
IllegalArgumentException if cmd is null or zero length array or timeout is <= 0


                                                                   
         
        if (cmd == null || cmd.length == 0) {
            throw new IllegalArgumentException(
                    "process.null_or_empty_command");
        }
        if (timeout < 0) {
            throw new IllegalArgumentException(
                "process.invalid_timeout_value " + new Long(timeout));
        }
        this.command = cmd;
        this.timeout= timeout;
    
Methods Summary
public java.lang.Stringexecute()
Execute the command.

throws
IllegalStateException if process already executed
throws
ProcessExecutorException if process execution times out or if there is any error in execution

        if (runner != null) {
            throw new IllegalStateException(
                    "process.already_executed");
        }
        runner = new ProcessRunner(command, timeout);
        Thread runnerThread = new Thread(runner);
        runnerThread.start();
        try {
            runnerThread.join(timeout);
        } catch (InterruptedException ie) {
            logger.log(Level.FINEST, "process.waiter_interrupted",
                    getCommandString());
        }
        if (runnerThread.isAlive()) {
            if (!runner.completed) {
                logger.log(Level.FINEST, "process.interrupting",
                        new Object[] {new Long(timeout), getCommandString()});
                runnerThread.interrupt();
                try {
                    // Wait for 500 ms for thread to terminate
                    runnerThread.join(500);
                } catch (InterruptedException ie) {
                    // Ignore this exception. Interrupted while waiting for
                    // runner thread to respond to interrupt() call
                }
                if (!runner.completed && !runner.interrupted) {
                    // Thread did not finish, force the status to interrupted
                    runner.interrupted = true;
                }
            }
        }
        if (runner.interrupted || runner.exception != null) {
            if (runner.exception == null) {
                // Thread did not complete but there is no exception, assume
                // it did not finish because of timeout
                runner.makeTimeoutException();
            }
            throw runner.exception;
        }
        return runner.stdout.toString();
    
public java.lang.StringexecuteAgain()
Execute the process again. All results of previous execution are lost.

        runner = null;
        return execute();
    
public java.lang.StringgetCommandString()
Get command as a string.

        String cmdString = null;
        if (runner != null) {
            cmdString = runner.getCommandString();
        }
        if (cmdString == null) {
            StringBuffer buf = new StringBuffer();
            for (int i = 0; i < command.length; i++) {
                buf.append(command[i] + " ");
            }
            cmdString = buf.toString();
        }
        return cmdString;
    
public intgetExitCode()
Get exit code of the process.

throws
IllegalStateException if the process execution has not finished yet (or has not been started yet)

        int exitCode = 0;
        if (runner != null && runner.completed) {
            exitCode = runner.exitCode;
        } else {
            throw new IllegalStateException(
                    "process.not_yet_executed");
        }
        return exitCode;
    
public java.lang.StringgetStderr()
Get standard error of the process.

        String stderr = null;
        if (runner != null) {
            stderr = runner.stderr.toString();
        }
        return stderr;
    
public java.lang.StringgetStdout()
Get standard output of the process.

        String stdout = null;
        if (runner != null) {
            stdout = runner.stdout.toString();
        }
        return stdout;
    
public longgetTimeout()
Get timeout value (in milliseconds)

        return timeout;
    
public booleanisCompleted()
Is process execution complete.

returns true if process execution was completed, false otherwise

        boolean completed = false;
        if (runner != null) {
            completed = runner.completed;
        }
        return completed;
    
public booleanisInterrupted()
Is (was) process execution interrupted.

returns true if the process execution was interrupted (typically because it did not finish in specified timeout), false otherwise

        boolean interrupted = false;
        if (runner != null) {
            interrupted = runner.interrupted;
        }
        return interrupted;
    
public voidsetTimeout(long timeout)
Set timeout to specified value.

param
timeout time to wait for process to complete execution (in milliseconds)
throws
IllegalArgumentException if specified timeout <= 0

        if (timeout >= 0) {
            this.timeout = timeout;
        } else {
            throw new IllegalArgumentException(
                    "process.invalid_timeout_value " + new Long(timeout));
        }