source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/Processing.java@ 26248

Last change on this file since 26248 was 16869, checked in by kjdon, 16 years ago

added license message

  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 KB
Line 
1/*
2 * Processing.java
3 * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.util;
20
21import java.io.InputStreamReader;
22
23import org.apache.log4j.*;
24
25public class Processing {
26
27 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.Processing.class.getName());
28
29 /** 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
30 * @param process the Process to test
31 * @return true if it is still executing, false otherwise
32 */
33 static public boolean processRunning(Process process) {
34 boolean process_running = false;
35
36 try {
37 process.exitValue(); // This will throw an exception if the process hasn't ended yet.
38 }
39 catch(IllegalThreadStateException itse) {
40 process_running = true;
41 }
42 catch(Exception exception) {
43 exception.printStackTrace();
44 }
45 return process_running;
46 }
47
48 static public int runProcess(String command) {
49
50 logger.error("executing command "+command);
51 int exit_value = -1;
52 try {
53 Process prcs = Runtime.getRuntime().exec(command);
54
55 InputStreamReader error_stream = new InputStreamReader( prcs.getErrorStream(), "UTF-8" );
56 InputStreamReader output_stream = new InputStreamReader( prcs.getInputStream(), "UTF-8" );
57
58 StringBuffer error_buffer = new StringBuffer();
59 StringBuffer output_buffer = new StringBuffer();
60
61 while(processRunning(prcs)) {
62 // Hopefully this doesn't block if the process is trying to write to STDOUT.
63 if((error_stream!=null)) {// && error_stream.ready()) {
64 while (error_stream.ready()) {
65 error_buffer.append((char)error_stream.read());
66 }
67 //error_buffer = get_stream_char(error_stream,error_buffer,bos);
68 }
69 //Hopefully this won't block if the process is trying to write to STDERR
70 /*else if*/ while (output_stream.ready()) {
71 output_buffer.append((char)output_stream.read());
72 //output_buffer = get_stream_char(output_stream,output_buffer,bos);
73 }
74 //else {
75 try {
76 Thread.sleep(100);
77 }
78 catch(Exception exception) {
79 }
80 // }
81 }
82
83 // Of course, just because the process is finished doesn't
84 // mean the incoming streams are empty. Unfortunately I've
85 // got no chance of preserving order, so I'll process the
86 // error stream first, then the out stream
87 while(error_stream.ready()) {
88 error_buffer.append((char)error_stream.read());
89 //error_buffer = get_stream_char(error_stream,error_buffer,bos);
90 }
91
92 while(output_stream.ready()) {
93 output_buffer.append((char)output_stream.read());
94 //output_buffer = get_stream_char(output_stream,output_buffer,bos);
95 }
96
97 // do something with the messages
98 logger.error("err>"+error_buffer.toString());
99 logger.error("out>"+output_buffer.toString());
100 // Ensure that any messages still remaining in the string buffers are fired off.
101 // Now display final message based on exit value
102
103 prcs.waitFor();
104 error_stream.close();
105 output_stream.close();
106 error_buffer = null;
107 output_buffer = null;
108
109 exit_value = prcs.exitValue();
110 } catch (Exception e) {
111 logger.error(e);
112 return 1;
113 }
114 return exit_value;
115
116
117 }
118
119
120}
Note: See TracBrowser for help on using the repository browser.