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-2016 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/** Class used to write the output and error of a given process in a printstream. */ 026public class ProcessReader 027{ 028 private BufferedReader reader; 029 private Thread readerThread; 030 private Throwable lastException; 031 private boolean interrupt; 032 private boolean done; 033 034 /** 035 * The constructor. 036 * @param process process whose output/error we want to write to the print 037 * stream. 038 * @param printStream the print stream. 039 * @param isError whether we must write the error (or the output) must be 040 * written to the stream. 041 */ 042 public ProcessReader(Process process, final PrintStream printStream, 043 boolean isError) 044 { 045 InputStream is; 046 if (isError) 047 { 048 is = process.getErrorStream(); 049 } 050 else 051 { 052 is = process.getInputStream(); 053 } 054 reader = new BufferedReader(new InputStreamReader(is)); 055 056 readerThread = new Thread(new Runnable() 057 { 058 @Override 059 public void run() 060 { 061 String line; 062 try 063 { 064 while (!interrupt && (null != (line = reader.readLine()))) 065 { 066 printStream.println(line); 067 } 068 } 069 catch (Throwable t) 070 { 071 lastException = t; 072 } 073 done = true; 074 } 075 }); 076 } 077 078 /** Starts reading the output (or error) of the process. */ 079 public void startReading() 080 { 081 readerThread.start(); 082 } 083 084 /** 085 * Interrupts the reading of the output (or error) of the process. The method 086 * does not return until the reading is over. 087 */ 088 public void interrupt() 089 { 090 interrupt = true; 091 while (!done) 092 { 093 try 094 { 095 readerThread.interrupt(); 096 } 097 catch (Throwable t) 098 { 099 } 100 } 101 } 102 103 /** 104 * Returns the last exception that occurred reading the output (or the error) 105 * of the process. 106 * @return the last exception that occurred reading the output (or the error) 107 * of the process. 108 */ 109 public Throwable getLastException() 110 { 111 return lastException; 112 } 113}