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
|
/*
* generated by Xtext 2.25.0
*/
package org.eclipse.viatra.solver.language.web;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.log.Slf4jLog;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.MetaInfConfiguration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
import org.eclipse.jetty.webapp.WebXmlConfiguration;
public class ServerLauncher {
private static final Slf4jLog LOG = new Slf4jLog(ServerLauncher.class.getName());
private final Server server;
public ServerLauncher(InetSocketAddress bindAddress, Resource baseResource) throws IOException, URISyntaxException {
server = new Server(bindAddress);
WebAppContext ctx = new WebAppContext();
ctx.setBaseResource(baseResource);
ctx.setWelcomeFiles(new String[] { "index.html" });
ctx.setContextPath("/");
ctx.setConfigurations(new Configuration[] { new AnnotationConfiguration(), new WebXmlConfiguration(),
new WebInfConfiguration(), new MetaInfConfiguration() });
ctx.setAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN, ".*/build/classes/.*,.*\\.jar");
ctx.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
server.setHandler(ctx);
}
public void start() throws Exception {
server.start();
LOG.info("Server started " + server.getURI() + "...");
new Thread() {
public void run() {
try {
LOG.info("Press enter to stop the server...");
int key = System.in.read();
if (key != -1) {
server.stop();
} else {
LOG.warn(
"Console input is not available. In order to stop the server, you need to cancel process manually.");
}
} catch (Exception e) {
LOG.warn(e);
System.exit(-1);
}
}
}.start();
try {
server.join();
} catch (InterruptedException e) {
LOG.info(e);
}
}
private static InetSocketAddress getBindAddress(String listenAddress, int port) {
if (listenAddress == null) {
return new InetSocketAddress(port);
}
return new InetSocketAddress(listenAddress, port);
}
private static Resource getBaseResource(String baseResourceOverride) throws IOException, URISyntaxException {
if (baseResourceOverride != null) {
return Resource.newResource(baseResourceOverride);
}
URL indexUrlInJar = ServerLauncher.class.getResource("/webapp/index.html");
if (indexUrlInJar == null) {
throw new RuntimeException("Cannot find pacakged web assets");
}
URI webRootUri = URI.create(indexUrlInJar.toURI().toASCIIString().replaceFirst("/index.html$", "/"));
return Resource.newResource(webRootUri);
}
public static void main(String[] args) {
String listenAddress = System.getenv("LiSTEN_ADDRESS");
if (listenAddress == null) {
listenAddress = "localhost";
}
int port = 1312;
String portStr = System.getenv("LISTEN_PORT");
if (portStr != null) {
try {
port = Integer.parseInt(portStr);
} catch (NumberFormatException e) {
LOG.warn(e);
System.exit(-1);
}
}
String baseResourceOverride = System.getenv("BASE_RESOURCE");
try {
InetSocketAddress bindAddress = getBindAddress(listenAddress, port);
Resource baseResource = getBaseResource(baseResourceOverride);
ServerLauncher serverLauncher = new ServerLauncher(bindAddress, baseResource);
serverLauncher.start();
} catch (Exception exception) {
LOG.warn(exception);
System.exit(1);
}
}
}
|