-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInspectorSetup.java
141 lines (125 loc) · 4.6 KB
/
InspectorSetup.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
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
package dk.brics.inspector;
import dk.brics.inspector.api.InspectorAPI;
import dk.brics.inspector.client.InspectorClient;
import dk.brics.inspector.server.InspectorServer;
import org.apache.log4j.Logger;
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
/**
* External handles for starting an {@link InspectorServer}.
*/
public class InspectorSetup {
private static final Logger log = Logger.getLogger(InspectorSetup.class);
/**
* Starts a server, opens a browser, waits for the server to terminate.
*/
@SuppressWarnings("unused") // used externally
public static void simpleStart(InspectorAPI api) {
InspectorClient client = InspectorClient.makeStandardClient();
simpleStart(api, client);
}
public static void simpleStart(InspectorAPI api, InspectorClient client) {
InspectorServer server = new InspectorServer(api, client);
Thread serverThread = makeServerThread(server);
serverThread.start();
try {
log.info("Waiting for server-thread to terminate...");
serverThread.join();
log.info("Server-thread has terminated.");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private static Thread makeServerThread(InspectorServer server) {
return new Thread(() -> {
InspectorServer.RunningServer runningServer = server.startServer();
try {
openBrowser(new URL("http", "localhost", runningServer.getURI().getPort(), runningServer.getURI().getPath()).toURI());
try {
Thread.sleep(1000); // wait for the browser to start properly
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} catch (URISyntaxException | MalformedURLException e) {
throw new RuntimeException(e);
}
try {
makeConsole(runningServer);
log.info("Console access terminated. Waiting for server to terminate...");
runningServer.join();
log.info("Server has terminated.");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
}
public static void main(String[] args) {
makeConsole(null);
}
private static void makeConsole(InspectorServer.RunningServer server) {
Console c = new Console();
boolean INTERACTIVE = false;
if (INTERACTIVE) {
c.format("%n---%nWrite commands to interact with the server (write 'stop' to stop the server).%n");
while (true) {
String command = c.readLine("SERVER > ");
if (command.equals("stop")) {
c.format("Got command '%s', stopping ...%n", command);
try {
server.stop();
} catch (Exception e) {
throw new RuntimeException(e);
}
break;
}
c.format("Command '%s' not understood%n", command);
}
} else {
c.format("%n---%n---%nPress <ENTER> to stop the server.%n---%n---%n");
c.readLine("");
try {
server.stop();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
private static void openBrowser(URI uri) {
try {
if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(uri);
} else {
Runtime.getRuntime().exec("xdg-open " + uri);
}
}
catch(Error | Exception e) {
System.out.println("Browser cannot be opended for inspector, ignoring");
}
}
private static class Console {
BufferedReader br;
PrintStream ps;
public Console() {
br = new BufferedReader(new InputStreamReader(System.in));
ps = System.out;
}
public String readLine(String out) {
ps.format(out);
try {
return br.readLine();
} catch (IOException e) {
return null;
}
}
public PrintStream format(String format, Object... objects) {
return ps.format(format, objects);
}
}
}