-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell.java
More file actions
343 lines (278 loc) · 10.9 KB
/
Shell.java
File metadata and controls
343 lines (278 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package javaxt.io;
import java.io.*;
//******************************************************************************
//** Shell Class
//******************************************************************************
/**
* Used to execute command line applications and return the corresponding
* output streams (standard output and error output streams).
*
******************************************************************************/
public class Shell {
private java.io.File executable;
private String[] inputs;
private java.util.List<String> output = new java.util.LinkedList<String>();
private java.util.List<String> errors = new java.util.LinkedList<String>();
private long startTime;
private long ellapsedTime;
private Process process;
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of Shell.
* @param executable Path to the executable to run
* @param parameters Command line args passed to the executable
*/
public Shell(java.io.File executable, String[] parameters) {
this.executable = executable;
this.ellapsedTime = -1;
if (parameters==null){
parameters = new String[0];
}
inputs = new String[parameters.length+1];
inputs[0] = executable.toString();
for (int i=0; i<parameters.length; i++){
inputs[i+1] = parameters[i];
}
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of Shell.
* @param executable Path to the executable to run
* @param parameters Command line args passed to the executable
*/
public Shell(javaxt.io.File executable, String[] parameters) {
this(executable.toFile(), parameters);
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of Shell.
* @param cmd Command to execute. Example: "cmd /c dir C:\\temp"
*/
public Shell(String cmd){
this.executable = null;
this.ellapsedTime = -1;
String arr[] = cmd.trim().split(" ");
java.util.Vector<String> parameters = new java.util.Vector<String>();
for (int i=0; i<arr.length; i++){
String str = arr[i].trim();
if (str.length()>0) parameters.add(str);
}
inputs = new String[parameters.size()];
for (int i=0; i<parameters.size(); i++){
inputs[i] = parameters.get(i);
}
java.io.File file = new java.io.File(inputs[0]);
if (file.exists() && file.isFile()){
executable = file;
}
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of Shell.
* @param cmdarray Command to execute, along with the command line args in a
* String array.
*/
public Shell(String[] cmdarray){
this.executable = null;
this.ellapsedTime = -1;
inputs = cmdarray;
java.io.File file = new java.io.File(inputs[0]);
if (file.exists() && file.isFile()){
executable = file;
}
}
//**************************************************************************
//** getOutput
//**************************************************************************
/** Used to retrieve the standard output stream. Returns a List that can be
* parsed while the executable is still running or after is has been run.
* The difference lies in when the run method is invoked. The following is
* an example of how to process the output stream while the app is running.
* The getOutput() is called BEFORE the run() method is invoked.
<pre>
File exe = new File("C:\\Program Files\\PostgreSQL\\8.4\\bin\\shp2pgsql.exe");
String[] options = new String[]{"-W", "UTF-8", "-s", "4326", "C:\country.shp", "t_country"};
javaxt.io.Shell cmd = new javaxt.io.Shell(exe, options);
java.util.List<String> output = cmd.getOutput();
cmd.run();
String line;
while (true){
synchronized (output) {
while (output.isEmpty()) {
try {
output.wait();
}
catch (InterruptedException e) {
}
}
line = output.remove(0);
}
if (line!=null){
System.out.println(line);
}
else{
break;
}
}
</pre>
* If you want to get the entire output all at once, just call the getOutput()
* AFTER the run() method. Example:
<pre>
javaxt.io.Shell cmd = new javaxt.io.Shell(exe, options);
cmd.run();
java.util.List<String> output = cmd.getOutput();
for (int i=0; i<output.size(); i++){
System.out.println(output.get(i));
}
</pre>
*
*/
public java.util.List getOutput(){ return output; }
//**************************************************************************
//** getErrors
//**************************************************************************
/** Used to retrieve the error output stream. Returns a List that can be
* parsed while the executable is still running or after is has been run.
*/
public java.util.List getErrors(){ return errors; }
//**************************************************************************
//** run
//**************************************************************************
/** Used to execute the process specified in the constructor and populate
* the output streams.
*/
public void run() {
ellapsedTime = -1;
startTime = System.currentTimeMillis();
try {
//Run executable via Command Line and pick up the output stream
Runtime runtime = Runtime.getRuntime();
if (executable!=null){
process = runtime.exec(inputs, null, executable.getParentFile());
}
else{
process = runtime.exec(inputs);
}
StreamReader s1 = new StreamReader(output, process.getInputStream());
StreamReader s2 = new StreamReader(errors, process.getErrorStream());
s1.start();
s2.start();
process.waitFor();
s1.join();
s2.join();
//Clean up!
cleanUp();
}
catch(IOException e){
throw new RuntimeException(e);
}
catch(InterruptedException e){
throw new RuntimeException(e);
}
ellapsedTime = System.currentTimeMillis()-startTime;
}
//**************************************************************************
//** stop
//**************************************************************************
/** Used to stop the current process. Note that this method does not stop
* or kill process grandchildren. This is a limitation of Java, not this
* class per se. See Sun bug 4770092 for more details.
*/
public void stop(){
if (process!=null){
process.destroy();
cleanUp();
ellapsedTime = System.currentTimeMillis()-startTime;
}
}
//**************************************************************************
//** cleanUp
//**************************************************************************
/** Used to clean up system resources after a process has been terminated.
* Based on recommendations found in "Five Common java.lang.Process Pitfalls"
* by Kyle Cartmell (http://kylecartmell.com/?p=9).
*/
private void cleanUp(){
//Explicitly clean up every instance of Process by calling close on each stream
try{process.getInputStream().close();} catch(Exception ex){}
try{process.getErrorStream().close();} catch(Exception ex){}
try{process.getOutputStream().close();} catch(Exception ex){}
//Explicitly destroy the process even if the process is already terminated
try{process.destroy();} catch(Exception ex){}
process = null;
}
//**************************************************************************
//** getEllapsedTime
//**************************************************************************
/** Used to return the total time (milliseconds) it took to execute the
* process.
*/
public long getEllapsedTime(){
return ellapsedTime;
}
/*
public void run(byte[] b){
try{
//Run executable via Command Line and pick up the output stream
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(inputs, null, executable.getParentFile());
//process.getOutputStream().write(b);
System.out.println(b.length);
BufferedOutputStream commandsStdIn = new BufferedOutputStream(process.getOutputStream());
commandsStdIn.write(b);
commandsStdIn.flush();
commandsStdIn.close();
StreamReader s1 = new StreamReader(output, process.getInputStream());
StreamReader s2 = new StreamReader(errors, process.getErrorStream());
s1.start();
s2.start();
process.waitFor();
}
catch(Exception e){
e.printStackTrace();
}
}
*/
//**************************************************************************
//** StreamReader Class
//**************************************************************************
/** Thread used to read the standard input and output streams. */
private class StreamReader implements Runnable {
java.util.List list;
InputStream is;
Thread thread;
public StreamReader (java.util.List list, InputStream is){
this.list = list;
this.is = is;
}
public void start () {
thread = new Thread(this);
thread.start();
}
public void run () {
try {
InputStreamReader isr = new InputStreamReader (is);
BufferedReader br = new BufferedReader (isr);
while (true) {
String s = br.readLine();
if (s == null) break;
//System.out.println(s);
if (list!=null) list.add(s);
}
list.add(null);
is.close();
}
catch (Exception ex) {
//System.out.println ("Problem reading stream... :" + ex);
ex.printStackTrace ();
}
}
public void join() throws InterruptedException {
thread.join();
}
} //End StreamReader Class
}