24
24
25
25
import java .io .IOException ;
26
26
import java .lang .reflect .Field ;
27
+ import java .util .logging .Level ;
27
28
import java .util .logging .Logger ;
28
29
29
30
import static org .openqa .selenium .Platform .WINDOWS ;
34
35
import com .sun .jna .platform .win32 .WinNT ;
35
36
36
37
public class ProcessUtils {
37
- static Logger log = Logger .getLogger (ProcessUtils .class .getName ());
38
+
39
+ private static Logger LOG = Logger .getLogger (ProcessUtils .class .getName ());
38
40
39
41
/**
40
42
* Waits the specified timeout for the process to die
@@ -106,13 +108,12 @@ private static int killUnixProcess(Process process) {
106
108
throw ex ;
107
109
}
108
110
try {
109
- log .info ("Process didn't die after 10 seconds" );
111
+ LOG .info ("Process didn't die after 10 seconds" );
110
112
kill9 (process );
111
113
exitValue = waitForProcessDeath (process , 10000 );
112
114
closeAllStreamsAndDestroyProcess ( process );
113
115
} catch (Exception e ) {
114
- log .warning ("Process refused to die after 10 seconds, and couldn't kill9 it" );
115
- e .printStackTrace ();
116
+ LOG .log (Level .WARNING , "Process refused to die after 10 seconds, and couldn't kill9 it" , ex );
116
117
throw new RuntimeException (
117
118
"Process refused to die after 10 seconds, and couldn't kill9 it: " + e .getMessage (),
118
119
ex );
@@ -136,9 +137,8 @@ private static int killWinProcess(Process process) {
136
137
137
138
killPID ("" + pid );
138
139
exitValue = waitForProcessDeath (process , 10000 );
139
- } catch (Throwable ex ) {
140
- log .warning ("Process refused to die after 10 seconds, and couldn't taskkill it" );
141
- ex .printStackTrace ();
140
+ } catch (Exception ex ) {
141
+ LOG .log (Level .WARNING , "Process refused to die after 10 seconds, and couldn't taskkill it" , ex );
142
142
throw new RuntimeException (
143
143
"Process refused to die after 10 seconds, and couldn't taskkill it: " + ex .getMessage (),
144
144
ex );
@@ -151,14 +151,14 @@ private static class ProcessWaiter implements Runnable {
151
151
private volatile InterruptedException t ;
152
152
private final Process p ;
153
153
154
- public InterruptedException getException () {
155
- return t ;
156
- }
157
-
158
154
public ProcessWaiter (Process p ) {
159
155
this .p = p ;
160
156
}
161
157
158
+ public InterruptedException getException () {
159
+ return t ;
160
+ }
161
+
162
162
public void run () {
163
163
try {
164
164
p .waitFor ();
@@ -191,22 +191,21 @@ static int getProcessId(Process p) {
191
191
try {
192
192
Field f = p .getClass ().getDeclaredField ("pid" );
193
193
f .setAccessible (true );
194
- Integer pid = (Integer ) f .get (p );
195
- return pid ;
194
+ return (Integer ) f .get (p );
196
195
} catch (Exception e ) {
197
196
throw new RuntimeException ("Couldn't detect pid" , e );
198
197
}
199
198
}
200
199
201
200
/** runs "kill -9" on the specified pid */
202
201
private static void kill9 (Integer pid ) {
203
- log .fine ("kill -9 " + pid );
202
+ LOG .fine ("kill -9 " + pid );
204
203
205
204
CommandLine command = new CommandLine ("kill" , "-9" , pid .toString ());
206
205
command .execute ();
207
206
String result = command .getStdOut ();
208
207
int output = command .getExitCode ();
209
- log .fine (String .valueOf (output ));
208
+ LOG .fine (String .valueOf (output ));
210
209
if (!command .isSuccessful ()) {
211
210
throw new RuntimeException ("exec return code " + result + ": " + output );
212
211
}
0 commit comments