ProcessRunner.java 5.4 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProcessRunner
implements Runnable {
    public static final int PROCESS_RUNNING = -1;
    public static final int PROCESS_ABORTED = -2;
    private static final Logger log;
    private static final int POLL_OUTPUT_DELAY = 100;
    private final String cmdLine;
    private final InputStream stdin;
    private final OutputStream stdout;
    private final OutputStream stderr;
    private final Object lock = new Object();
    private boolean running = true;
    private int rc = -1;

    public ProcessRunner(String cmdLine, InputStream stdin, OutputStream stdout, OutputStream stderr) {
        this.cmdLine = cmdLine;
        this.stdin = stdin;
        this.stdout = stdout == null ? System.out : stdout;
        this.stderr = stderr == null ? System.err : stderr;
    }

    public ProcessRunner(String cmdLine) {
        this(cmdLine, null, null, null);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public void run(long waitTime) {
        Thread t = new Thread(this);
        t.setDaemon(true);
        t.start();
        if (waitTime < 0) {
            return;
        }
        Object object = this.lock;
        synchronized (object) {
            while (this.running) {
                try {
                    this.lock.wait(waitTime);
                    if (!this.running) continue;
                    t.interrupt();
                    waitTime = 0;
                }
                catch (InterruptedException ie) {}
            }
            return;
        }
    }

    public int getReturnCode() {
        return this.rc;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void run() {
        block24 : {
            InputStream procOut = null;
            InputStream procErr = null;
            Process p = null;
            try {
                p = Runtime.getRuntime().exec(this.cmdLine);
                procOut = p.getInputStream();
                procErr = p.getErrorStream();
                this.writeStdIn(p, this.stdin);
                while (p != null) {
                    Thread.sleep(100);
                    this.spoolAvailable(procOut, this.stdout);
                    this.spoolAvailable(procErr, this.stderr);
                    try {
                        this.rc = p.exitValue();
                        p = null;
                    }
                    catch (IllegalThreadStateException e) {}
                }
                log.info("run: Process ended with rc={}", (Object)new Integer(this.rc));
            }
            catch (IOException ioe) {
                log.info("run: IO problem during execution: {}", (Object)ioe.toString());
            }
            catch (InterruptedException ie) {
                if (p == null) break block24;
                p.destroy();
                try {
                    this.rc = p.exitValue();
                }
                catch (IllegalThreadStateException itse) {
                    log.info("run: Exit value of destroyed process unavailable");
                }
                p = null;
            }
            finally {
                this.tryClose(procOut);
                this.tryClose(procErr);
                Object ie = this.lock;
                synchronized (ie) {
                    this.lock.notifyAll();
                    this.running = false;
                }
            }
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void writeStdIn(Process proc, InputStream stdin) throws IOException {
        if (stdin != null) {
            int bufLen = 1024;
            byte[] b = new byte[1024];
            OutputStream procIn = null;
            try {
                int numRead;
                procIn = proc.getOutputStream();
                while ((numRead = stdin.read(b)) > 0) {
                    procIn.write(b, 0, numRead);
                }
            }
            finally {
                this.tryClose(procIn);
            }
        }
    }

    private void spoolAvailable(InputStream source, OutputStream sink) throws IOException {
        int num;
        int bufLen = 1024;
        byte[] buf = new byte[1024];
        for (int avail = source.available(); avail > 0; avail -= num) {
            num = avail > 1024 ? 1024 : avail;
            num = source.read(buf, 0, num);
            sink.write(buf, 0, num);
        }
    }

    private void tryClose(OutputStream out) {
        if (out != null) {
            try {
                out.close();
            }
            catch (IOException ioe) {
                // empty catch block
            }
        }
    }

    private void tryClose(InputStream in) {
        if (in != null) {
            try {
                in.close();
            }
            catch (IOException ioe) {
                // empty catch block
            }
        }
    }

    static {
        Class class_ = ProcessRunner.class;
        log = LoggerFactory.getLogger((Class)class_);
    }
}