001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008 Sun Microsystems, Inc.
015 * Portions Copyright 2015 ForgeRock AS.
016 */
017
018package org.opends.guitools.controlpanel.util;
019
020import java.io.BufferedReader;
021import java.io.InputStream;
022import java.io.InputStreamReader;
023import java.io.PrintStream;
024
025/**
026 * Class used to write the output and error of a given process in a printstream.
027 *
028 */
029public class ProcessReader
030{
031  private BufferedReader reader;
032  private Thread readerThread;
033  private Throwable lastException;
034  private boolean interrupt;
035  private boolean done;
036
037  /**
038   * The constructor.
039   * @param process process whose output/error we want to write to the print
040   * stream.
041   * @param printStream the print stream.
042   * @param isError whether we must write the error (or the output) must be
043   * written to the stream.
044   */
045  public ProcessReader(Process process, final PrintStream printStream,
046      boolean isError)
047  {
048    InputStream is;
049    if (isError)
050    {
051      is = process.getErrorStream();
052    }
053    else
054    {
055      is = process.getInputStream();
056    }
057    reader = new BufferedReader(new InputStreamReader(is));
058
059    readerThread = new Thread(new Runnable()
060    {
061      /** {@inheritDoc} */
062      public void run()
063      {
064        String line;
065        try
066        {
067          while (!interrupt && (null != (line = reader.readLine())))
068          {
069            printStream.println(line);
070          }
071        }
072        catch (Throwable t)
073        {
074          lastException = t;
075        }
076        done = true;
077      }
078    });
079  }
080
081  /**
082   * Starts reading the output (or error) of the process.
083   *
084   */
085  public void startReading()
086  {
087    readerThread.start();
088  }
089
090  /**
091   * Interrupts the reading of the output (or error) of the process.  The method
092   * does not return until the reading is over.
093   *
094   */
095  public void interrupt()
096  {
097    interrupt = true;
098    while (!done)
099    {
100      try
101      {
102        readerThread.interrupt();
103      }
104      catch (Throwable t)
105      {
106      }
107    }
108  }
109
110  /**
111   * Returns the last exception that occurred reading the output (or the error)
112   * of the process.
113   * @return the last exception that occurred reading the output (or the error)
114   * of the process.
115   */
116  public Throwable getLastException()
117  {
118    return lastException;
119  }
120}