ProcessRunner.java
5.38 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class ProcessRunner
implements Runnable {
public static final int PROCESS_RUNNING = -1;
public static final int PROCESS_ABORTED = -2;
private static final Logger log;
private static final int POLL_OUTPUT_DELAY = 100;
private final String cmdLine;
private final InputStream stdin;
private final OutputStream stdout;
private final OutputStream stderr;
private final Object lock = new Object();
private boolean running = true;
private int rc = -1;
public ProcessRunner(String cmdLine, InputStream stdin, OutputStream stdout, OutputStream stderr) {
this.cmdLine = cmdLine;
this.stdin = stdin;
this.stdout = stdout == null ? System.out : stdout;
this.stderr = stderr == null ? System.err : stderr;
}
public ProcessRunner(String cmdLine) {
this(cmdLine, null, null, null);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
* Enabled aggressive block sorting
* Enabled unnecessary exception pruning
* Enabled aggressive exception aggregation
*/
public void run(long waitTime) {
Thread t = new Thread(this);
t.setDaemon(true);
t.start();
if (waitTime < 0) {
return;
}
Object object = this.lock;
synchronized (object) {
while (this.running) {
try {
this.lock.wait(waitTime);
if (!this.running) continue;
t.interrupt();
waitTime = 0;
}
catch (InterruptedException ie) {}
}
return;
}
}
public int getReturnCode() {
return this.rc;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void run() {
block24 : {
InputStream procOut = null;
InputStream procErr = null;
Process p = null;
try {
p = Runtime.getRuntime().exec(this.cmdLine);
procOut = p.getInputStream();
procErr = p.getErrorStream();
this.writeStdIn(p, this.stdin);
while (p != null) {
Thread.sleep(100);
this.spoolAvailable(procOut, this.stdout);
this.spoolAvailable(procErr, this.stderr);
try {
this.rc = p.exitValue();
p = null;
}
catch (IllegalThreadStateException e) {}
}
log.info("run: Process ended with rc={}", (Object)new Integer(this.rc));
}
catch (IOException ioe) {
log.info("run: IO problem during execution: {}", (Object)ioe.toString());
}
catch (InterruptedException ie) {
if (p == null) break block24;
p.destroy();
try {
this.rc = p.exitValue();
}
catch (IllegalThreadStateException itse) {
log.info("run: Exit value of destroyed process unavailable");
}
p = null;
}
finally {
this.tryClose(procOut);
this.tryClose(procErr);
Object ie = this.lock;
synchronized (ie) {
this.lock.notifyAll();
this.running = false;
}
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void writeStdIn(Process proc, InputStream stdin) throws IOException {
if (stdin != null) {
int bufLen = 1024;
byte[] b = new byte[1024];
OutputStream procIn = null;
try {
int numRead;
procIn = proc.getOutputStream();
while ((numRead = stdin.read(b)) > 0) {
procIn.write(b, 0, numRead);
}
}
finally {
this.tryClose(procIn);
}
}
}
private void spoolAvailable(InputStream source, OutputStream sink) throws IOException {
int num;
int bufLen = 1024;
byte[] buf = new byte[1024];
for (int avail = source.available(); avail > 0; avail -= num) {
num = avail > 1024 ? 1024 : avail;
num = source.read(buf, 0, num);
sink.write(buf, 0, num);
}
}
private void tryClose(OutputStream out) {
if (out != null) {
try {
out.close();
}
catch (IOException ioe) {
// empty catch block
}
}
}
private void tryClose(InputStream in) {
if (in != null) {
try {
in.close();
}
catch (IOException ioe) {
// empty catch block
}
}
}
static {
Class class_ = ProcessRunner.class;
log = LoggerFactory.getLogger((Class)class_);
}
}