| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2008 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.tools; |
| 17 | |
| 18 | import java.io.BufferedReader; |
| 19 | import java.io.InputStream; |
| 20 | import java.io.InputStreamReader; |
| 21 | import java.util.List; |
| 22 | |
| 23 | import net.sf.wraplog.Logger; |
| 24 | |
| 25 | /** |
| 26 | * Thread to get out or err of a Process, and append them to a list. |
| 27 | * |
| 28 | * @author Thomas Aglassinger |
| 29 | */ |
| 30 | public class ProcessOutputThread extends Thread |
| 31 | { |
| 32 | private static Logger logger = Logger.getLogger(ProcessOutputThread.class); |
| 33 | private String lastLineWritten; |
| 34 | private int level; |
| 35 | private ConsoleOutputListener listener; |
| 36 | private Process process; |
| 37 | private BufferedReader reader; |
| 38 | private int streamType; |
| 39 | private List target; |
| 40 | |
| 41 | public ProcessOutputThread( |
| 42 | final Process newProcess, |
| 43 | final InputStream stream, |
| 44 | final List newTarget, |
| 45 | final int newLevel, |
| 46 | final Object newWaiter, |
| 47 | final ConsoleOutputListener newListener, |
| 48 | final int newStreamType) { |
| 49 | assert newProcess != null; |
| 50 | assert stream != null; |
| 51 | assert newTarget != null; |
| 52 | assert newWaiter != null; |
| 53 | assert (newStreamType == ConsoleOutputListener.STREAM_TYPE_ERR) |
| 54 | || (newStreamType == ConsoleOutputListener.STREAM_TYPE_OUT) : "newStreamType = " + newStreamType; |
| 55 | |
| 56 | reader = new BufferedReader(new InputStreamReader(stream)); |
| 57 | process = newProcess; |
| 58 | target = newTarget; |
| 59 | level = newLevel; |
| 60 | listener = newListener; |
| 61 | streamType = newStreamType; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Gets the last line written to output stream. If this is an error stream, it will most likely |
| 66 | * be the error message written by the process. |
| 67 | */ |
| 68 | public String getLastLineWritten() { |
| 69 | return lastLineWritten; |
| 70 | } |
| 71 | |
| 72 | public final void run() { |
| 73 | try { |
| 74 | boolean done = false; |
| 75 | String line; |
| 76 | |
| 77 | do { |
| 78 | line = reader.readLine(); |
| 79 | if (line != null) { |
| 80 | logger.log(level, line); |
| 81 | synchronized (target) { |
| 82 | target.add(line); |
| 83 | if (line.length() > 0) { |
| 84 | lastLineWritten = line; |
| 85 | } |
| 86 | if (listener != null) { |
| 87 | listener.lineWritten(process, line, streamType); |
| 88 | } |
| 89 | } |
| 90 | } else { |
| 91 | done = true; |
| 92 | } |
| 93 | } while (!done); |
| 94 | } catch (Throwable error) { |
| 95 | logger.error("cannot read from stream [level=" + level + "]", error); |
| 96 | } finally { |
| 97 | try { |
| 98 | reader.close(); |
| 99 | } catch (Throwable error2) { |
| 100 | logger.error("cannot close reader", error2); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |