1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 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 org.apache.commons.logging.Log; |
24 | import org.apache.commons.logging.LogFactory; |
25 | |
26 | /** |
27 | * Thread to get out or err of a Process, and append them to a list. |
28 | * |
29 | * @author Thomas Aglassinger |
30 | */ |
31 | public class ProcessOutputThread extends Thread |
32 | { |
33 | private static Log logger = LogFactory.getLog(ProcessOutputThread.class); |
34 | private String lastLineWritten; |
35 | private ConsoleOutputListener listener; |
36 | private boolean logOutputAsError; |
37 | private String nameOfStreamToWriteTo; |
38 | private Process process; |
39 | private BufferedReader reader; |
40 | private int streamType; |
41 | private List target; |
42 | |
43 | |
44 | public ProcessOutputThread( |
45 | final Process newProcess, |
46 | final InputStream stream, |
47 | final List newTarget, |
48 | final boolean newLogOutputAsError, |
49 | final Object newWaiter, |
50 | final ConsoleOutputListener newListener, |
51 | final int newStreamType) { |
52 | assert newProcess != null; |
53 | assert stream != null; |
54 | assert newTarget != null; |
55 | assert newWaiter != null; |
56 | assert (newStreamType == ConsoleOutputListener.STREAM_TYPE_ERR) |
57 | || (newStreamType == ConsoleOutputListener.STREAM_TYPE_OUT) : "newStreamType = " + newStreamType; |
58 | |
59 | reader = new BufferedReader(new InputStreamReader(stream)); |
60 | process = newProcess; |
61 | target = newTarget; |
62 | logOutputAsError = newLogOutputAsError; |
63 | listener = newListener; |
64 | streamType = newStreamType; |
65 | if (logOutputAsError) { |
66 | nameOfStreamToWriteTo = "stderr"; |
67 | } else { |
68 | nameOfStreamToWriteTo = "stdout"; |
69 | } |
70 | } |
71 | |
72 | /** |
73 | * Gets the last line written to output stream. If this is an error stream, it will most likely |
74 | * be the error message written by the process. |
75 | */ |
76 | public String getLastLineWritten() { |
77 | return lastLineWritten; |
78 | } |
79 | |
80 | public final void run() { |
81 | try { |
82 | boolean done = false; |
83 | String line; |
84 | |
85 | do { |
86 | line = reader.readLine(); |
87 | if (line != null) { |
88 | if (logOutputAsError) { |
89 | logger.error(line); |
90 | } else if (logger.isDebugEnabled()) { |
91 | logger.debug(line); |
92 | } |
93 | synchronized (target) { |
94 | target.add(line); |
95 | if (line.length() > 0) { |
96 | lastLineWritten = line; |
97 | } |
98 | if (listener != null) { |
99 | listener.lineWritten(process, line, streamType); |
100 | } |
101 | } |
102 | } else { |
103 | done = true; |
104 | } |
105 | } while (!done); |
106 | } catch (Throwable error) { |
107 | logger.error("cannot read from stream " + nameOfStreamToWriteTo, error); |
108 | } finally { |
109 | try { |
110 | reader.close(); |
111 | } catch (Throwable error2) { |
112 | logger.error("cannot close reader", error2); |
113 | } |
114 | } |
115 | } |
116 | } |