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