1 | package org.greenstone.gsdl3.util;
|
---|
2 |
|
---|
3 | import java.io.InputStreamReader;
|
---|
4 |
|
---|
5 | import org.apache.log4j.*;
|
---|
6 |
|
---|
7 | public class Processing {
|
---|
8 |
|
---|
9 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.Processing.class.getName());
|
---|
10 |
|
---|
11 | /** Determine if the given process is still executing. It does this by attempting to throw an exception - not the most efficient way, but the only one as far as I know
|
---|
12 | * @param process the Process to test
|
---|
13 | * @return true if it is still executing, false otherwise
|
---|
14 | */
|
---|
15 | static public boolean processRunning(Process process) {
|
---|
16 | boolean process_running = false;
|
---|
17 |
|
---|
18 | try {
|
---|
19 | process.exitValue(); // This will throw an exception if the process hasn't ended yet.
|
---|
20 | }
|
---|
21 | catch(IllegalThreadStateException itse) {
|
---|
22 | process_running = true;
|
---|
23 | }
|
---|
24 | catch(Exception exception) {
|
---|
25 | exception.printStackTrace();
|
---|
26 | }
|
---|
27 | return process_running;
|
---|
28 | }
|
---|
29 |
|
---|
30 | static public int runProcess(String command) {
|
---|
31 |
|
---|
32 | logger.error("executing command "+command);
|
---|
33 | int exit_value = -1;
|
---|
34 | try {
|
---|
35 | Process prcs = Runtime.getRuntime().exec(command);
|
---|
36 |
|
---|
37 | InputStreamReader error_stream = new InputStreamReader( prcs.getErrorStream(), "UTF-8" );
|
---|
38 | InputStreamReader output_stream = new InputStreamReader( prcs.getInputStream(), "UTF-8" );
|
---|
39 |
|
---|
40 | StringBuffer error_buffer = new StringBuffer();
|
---|
41 | StringBuffer output_buffer = new StringBuffer();
|
---|
42 |
|
---|
43 | while(processRunning(prcs)) {
|
---|
44 | // Hopefully this doesn't block if the process is trying to write to STDOUT.
|
---|
45 | if((error_stream!=null)) {// && error_stream.ready()) {
|
---|
46 | while (error_stream.ready()) {
|
---|
47 | error_buffer.append((char)error_stream.read());
|
---|
48 | }
|
---|
49 | //error_buffer = get_stream_char(error_stream,error_buffer,bos);
|
---|
50 | }
|
---|
51 | //Hopefully this won't block if the process is trying to write to STDERR
|
---|
52 | /*else if*/ while (output_stream.ready()) {
|
---|
53 | output_buffer.append((char)output_stream.read());
|
---|
54 | //output_buffer = get_stream_char(output_stream,output_buffer,bos);
|
---|
55 | }
|
---|
56 | //else {
|
---|
57 | try {
|
---|
58 | Thread.sleep(100);
|
---|
59 | }
|
---|
60 | catch(Exception exception) {
|
---|
61 | }
|
---|
62 | // }
|
---|
63 | }
|
---|
64 |
|
---|
65 | // Of course, just because the process is finished doesn't
|
---|
66 | // mean the incoming streams are empty. Unfortunately I've
|
---|
67 | // got no chance of preserving order, so I'll process the
|
---|
68 | // error stream first, then the out stream
|
---|
69 | while(error_stream.ready()) {
|
---|
70 | error_buffer.append((char)error_stream.read());
|
---|
71 | //error_buffer = get_stream_char(error_stream,error_buffer,bos);
|
---|
72 | }
|
---|
73 |
|
---|
74 | while(output_stream.ready()) {
|
---|
75 | output_buffer.append((char)output_stream.read());
|
---|
76 | //output_buffer = get_stream_char(output_stream,output_buffer,bos);
|
---|
77 | }
|
---|
78 |
|
---|
79 | // do something with the messages
|
---|
80 | logger.error("err>"+error_buffer.toString());
|
---|
81 | logger.error("out>"+output_buffer.toString());
|
---|
82 | // Ensure that any messages still remaining in the string buffers are fired off.
|
---|
83 | // Now display final message based on exit value
|
---|
84 |
|
---|
85 | prcs.waitFor();
|
---|
86 | error_stream.close();
|
---|
87 | output_stream.close();
|
---|
88 | error_buffer = null;
|
---|
89 | output_buffer = null;
|
---|
90 |
|
---|
91 | exit_value = prcs.exitValue();
|
---|
92 | } catch (Exception e) {
|
---|
93 | logger.error(e);
|
---|
94 | return 1;
|
---|
95 | }
|
---|
96 | return exit_value;
|
---|
97 |
|
---|
98 |
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | }
|
---|