// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) deadcode 
// Source File Name:   ProcessManager.java

package com.symantec.itools.lang;

import com.symantec.itools.io.InterleavedReader;
import java.io.IOException;
import java.io.PrintStream;

public class ProcessManager
{
    final class Waiter
        implements Runnable
    {

        public void run()
        {
            try
            {
                result = process.waitFor();
                return;
            }
            catch(Throwable _ex)
            {
                return;
            }
        }

        final int getResult()
        {
            return result;
        }

        private Process process;
        private int result;

        Waiter(Process process1)
        {
            this(process1, -1);
        }

        Waiter(Process process1, int i)
        {
            process = process1;
            result = i;
        }
    }


    public ProcessManager()
    {
    }

    public int monitorLaunchedProcess(Process process1)
    {
        process = process1;
        InterleavedReader interleavedreader = new InterleavedReader();
        java.io.InputStream inputstream = process.getInputStream();
        java.io.InputStream inputstream1 = process.getErrorStream();
        interleavedreader.addInputStream(inputstream);
        interleavedreader.addInputStream(inputstream1);
        String s;
        try
        {
            while((s = interleavedreader.readLine()) != null) 
            {
                java.io.InputStream inputstream2 = interleavedreader.lastStreamRead();
                if(inputstream2 == inputstream)
                    processCommandOutput(s);
                else
                if(inputstream2 == inputstream1)
                    processCommandError(s);
            }
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
            processCommandError("IOException: " + ioexception);
        }
        interleavedreader.removeInputStream(inputstream);
        interleavedreader.removeInputStream(inputstream1);
        try
        {
            Waiter waiter = new Waiter(process1);
            Thread thread = new Thread(waiter, "WaitOnSpawnedProcess");
            thread.start();
            thread.join();
            return waiter.getResult();
        }
        catch(InterruptedException interruptedexception)
        {
            process.destroy();
            interruptedexception.printStackTrace();
            int i = process.exitValue();
            process.destroy();
            process = null;
            return i;
        }
    }

    public void destroyProcess()
    {
        process.destroy();
    }

    protected void processCommandOutput(String s)
    {
        System.out.println(s);
    }

    protected void processCommandError(String s)
    {
        System.out.println(s);
    }

    protected Process process;
}
