Partial implementation of XML parse for FindBugs output
[cfb.git] / prod / net / jaekl / cfb / util / Command.java
1 package net.jaekl.cfb.util;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7
8 public class Command {
9         public static class Result
10         {
11                 private int m_retCode;
12                 private String m_stdout;
13                 private String m_stderr;
14                 
15                 Result(int retCode, String stdout, String stderr) {
16                         m_retCode = retCode;
17                         m_stdout = stdout;
18                         m_stderr = stderr;
19                 }
20                 
21                 public int getRetCode() { return m_retCode; }
22                 public String getStdout() { return m_stdout; }
23                 public String getStderr() { return m_stderr; }
24         }
25         
26         private static class StreamGobbler extends Thread {
27                 
28                 private StringBuilder m_sb;
29                 private BufferedReader m_br;
30                 
31                 public StreamGobbler(InputStream is) {
32                         m_sb = new StringBuilder();
33                         m_br = new BufferedReader(new InputStreamReader(is));
34                 }
35                 
36                 @Override
37                 public void run() {
38                         String line;
39                         try {
40                                 line = m_br.readLine();
41                                 while (null != line) {
42                                         m_sb.append(line).append(System.lineSeparator());
43                                         line = m_br.readLine();
44                                 }
45                         } catch (IOException exc) {
46                                 m_sb.append(Util.stringify(exc));
47                         }
48                         finally {
49                                 try {
50                                         m_br.close();
51                                 } catch (IOException exc) {
52                                         m_sb.append(Util.stringify(exc));
53                                 }
54                         }
55                 }
56                 
57                 public String getOutput() { return m_sb.toString(); }
58         }
59         
60         public Command() 
61         {
62                 // no-op
63         }
64         
65         public Result exec(String cmd) throws IOException 
66         {
67                 int retCode = 0;
68                 String stdout = "";
69                 String stderr = "";
70                 
71                 Process proc = doRuntimeExec(cmd);
72                 assert( null != proc );
73                 
74                 StreamGobbler stdoutGobbler = new StreamGobbler(proc.getInputStream());
75                 StreamGobbler stderrGobbler = new StreamGobbler(proc.getErrorStream());
76                 
77                 stdoutGobbler.start();
78                 stderrGobbler.start();
79                 
80                 try {
81                         retCode = proc.waitFor();
82                         stdoutGobbler.join();
83                         stderrGobbler.join();
84                         
85                         stdout = stdoutGobbler.getOutput();
86                         stderr = stdoutGobbler.getOutput();
87                         
88                 } catch (InterruptedException exc) {
89                         stderr += Util.stringify(exc);
90                 }
91                 
92                 return new Result(retCode, stdout, stderr);
93         }
94         
95         Process doRuntimeExec(String cmd) throws IOException {
96                 return Runtime.getRuntime().exec(cmd);
97         }
98 }