-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPhantomJS.java
More file actions
120 lines (95 loc) · 2.72 KB
/
PhantomJS.java
File metadata and controls
120 lines (95 loc) · 2.72 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
package jsjunit.service;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import com.google.common.io.Closeables;
public final class PhantomJS {
private static class ProcessDestroyer extends TimerTask {
private Process p;
public ProcessDestroyer(Process p) {
this.p = p;
}
@Override
public void run() {
p.destroy();
}
}
private final Process process;
private final Thread outThread;
private final Thread errThread;
public PhantomJS(OutputStream out, OutputStream err, String... args)
throws IOException {
final String phantomJsPath = decidePath();
List<String> commands = new ArrayList<String>();
commands.add(phantomJsPath);
for (String arg : args) {
commands.add(arg);
}
ProcessBuilder processBuilder = new ProcessBuilder(
commands.toArray(new String[commands.size()]));
process = processBuilder.start();
TimerTask task = new ProcessDestroyer(process);
Timer timer = new Timer();
timer.schedule(task, TimeUnit.SECONDS.toMillis(10));
outThread = new Thread(new Pipe(new BufferedReader(
new InputStreamReader(process.getInputStream())),
new BufferedWriter(new OutputStreamWriter(out))));
errThread = new Thread(new Pipe(new BufferedReader(
new InputStreamReader(process.getErrorStream())),
new BufferedWriter(new OutputStreamWriter(err))));
outThread.start();
errThread.start();
}
private static final String SEPARATOR = File.separator;
private String decidePath() {
String directoryPath = System.getProperty("phantomjs.path", "");
if (!directoryPath.isEmpty() && !directoryPath.endsWith(SEPARATOR)){
directoryPath = directoryPath + SEPARATOR;
}
return directoryPath + "phantomjs";
}
public void destroy() {
while (true) {
try {
process.waitFor();
outThread.join();
errThread.join();
break;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static class Pipe implements Runnable {
public Pipe(BufferedReader reader, BufferedWriter writer) {
this.reader = reader;
this.writer = writer;
}
private final BufferedReader reader;
private final BufferedWriter writer;
@Override
public void run() {
String line = null;
try {
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.write("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
Closeables.closeQuietly(reader);
Closeables.closeQuietly(writer);
}
}
}
}