-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathExecThread.java
91 lines (76 loc) · 2.29 KB
/
ExecThread.java
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTextArea;
class ExecThread extends Thread {
interface Callback{
public void printLog(String s);
public void printStatus(String s);
public void processExit(boolean s);
}
JTextArea outputArea;
private boolean stop = false;
OutputPanel outputPanel;
List<String> cmd = new ArrayList<String>();
List<ArrayList<String>> cmds = new ArrayList<ArrayList<String>>();
String line;
Process process;
ProcessBuilder pb;
boolean die = false;
private Callback callback;
public ExecThread(Callback c){
callback = c;
}
public void requestStop(){
stop = true;
}
public void setTextArea(JTextArea t){
outputArea = t;
}
public void setCommand(ArrayList<String> s){
cmds.add(s);
}
public void setCommands(ArrayList<ArrayList<String>> s){
cmds = s;
}
public void run(){
BufferedReader reader;
int i, n;
n = cmds.size();
i = 0;
for(ArrayList<String> cmd : cmds){
i ++;
callback.printLog("[RUNNING]" + cmd + "\n");
if(n > 1){
callback.printStatus('(' + Integer.toString(i) + '/' + Integer.toString(n)+')');
}
else{
callback.printStatus("");
}
pb = new ProcessBuilder(cmd);
try {
pb.redirectErrorStream(true);
process = pb.start();
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while((line = reader.readLine()) != null){
callback.printLog(line);
if(stop){
process.destroy();
cmds.clear();
callback.printLog("\n[TERMINATED]");
callback.processExit(false);
stop = false;
return;
}
}
reader.close();
}
catch(Exception e){
}
}
cmds.clear();
callback.processExit(true);
}
}