diff options
Diffstat (limited to 'language-web/src/main/java/org/eclipse/viatra')
-rw-r--r-- | language-web/src/main/java/org/eclipse/viatra/solver/language/web/ServerLauncher.java | 105 |
1 files changed, 78 insertions, 27 deletions
diff --git a/language-web/src/main/java/org/eclipse/viatra/solver/language/web/ServerLauncher.java b/language-web/src/main/java/org/eclipse/viatra/solver/language/web/ServerLauncher.java index d0b2562a..7127d7d5 100644 --- a/language-web/src/main/java/org/eclipse/viatra/solver/language/web/ServerLauncher.java +++ b/language-web/src/main/java/org/eclipse/viatra/solver/language/web/ServerLauncher.java | |||
@@ -3,55 +3,106 @@ | |||
3 | */ | 3 | */ |
4 | package org.eclipse.viatra.solver.language.web; | 4 | package org.eclipse.viatra.solver.language.web; |
5 | 5 | ||
6 | import java.io.IOException; | ||
6 | import java.net.InetSocketAddress; | 7 | import java.net.InetSocketAddress; |
8 | import java.net.URI; | ||
9 | import java.net.URISyntaxException; | ||
10 | import java.net.URL; | ||
11 | |||
7 | import org.eclipse.jetty.annotations.AnnotationConfiguration; | 12 | import org.eclipse.jetty.annotations.AnnotationConfiguration; |
8 | import org.eclipse.jetty.server.Server; | 13 | import org.eclipse.jetty.server.Server; |
9 | import org.eclipse.jetty.util.log.Slf4jLog; | 14 | import org.eclipse.jetty.util.log.Slf4jLog; |
15 | import org.eclipse.jetty.util.resource.Resource; | ||
10 | import org.eclipse.jetty.webapp.Configuration; | 16 | import org.eclipse.jetty.webapp.Configuration; |
11 | import org.eclipse.jetty.webapp.MetaInfConfiguration; | 17 | import org.eclipse.jetty.webapp.MetaInfConfiguration; |
12 | import org.eclipse.jetty.webapp.WebAppContext; | 18 | import org.eclipse.jetty.webapp.WebAppContext; |
13 | import org.eclipse.jetty.webapp.WebInfConfiguration; | 19 | import org.eclipse.jetty.webapp.WebInfConfiguration; |
14 | import org.eclipse.jetty.webapp.WebXmlConfiguration; | 20 | import org.eclipse.jetty.webapp.WebXmlConfiguration; |
15 | 21 | ||
16 | /** | ||
17 | * This program starts an HTTP server for testing the web integration of your | ||
18 | * DSL. Just execute it and point a web browser to http://localhost:8080/ | ||
19 | */ | ||
20 | public class ServerLauncher { | 22 | public class ServerLauncher { |
21 | public static void main(String[] args) { | 23 | private static final Slf4jLog LOG = new Slf4jLog(ServerLauncher.class.getName()); |
22 | Server server = new Server(new InetSocketAddress("localhost", 1313)); | 24 | |
25 | private final Server server; | ||
26 | |||
27 | public ServerLauncher(InetSocketAddress bindAddress, Resource baseResource) throws IOException, URISyntaxException { | ||
28 | server = new Server(bindAddress); | ||
23 | WebAppContext ctx = new WebAppContext(); | 29 | WebAppContext ctx = new WebAppContext(); |
24 | ctx.setResourceBase("src/main/webapp"); | 30 | ctx.setBaseResource(baseResource); |
25 | ctx.setWelcomeFiles(new String[] { "index.html" }); | 31 | ctx.setWelcomeFiles(new String[] { "index.html" }); |
26 | ctx.setContextPath("/"); | 32 | ctx.setContextPath("/"); |
27 | ctx.setConfigurations(new Configuration[] { new AnnotationConfiguration(), new WebXmlConfiguration(), | 33 | ctx.setConfigurations(new Configuration[] { new AnnotationConfiguration(), new WebXmlConfiguration(), |
28 | new WebInfConfiguration(), new MetaInfConfiguration() }); | 34 | new WebInfConfiguration(), new MetaInfConfiguration() }); |
29 | ctx.setAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN, ".*/language-web/.*,.*\\.jar"); | 35 | ctx.setAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN, ".*/build/classes/.*,.*\\.jar"); |
30 | ctx.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false"); | 36 | ctx.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false"); |
31 | server.setHandler(ctx); | 37 | server.setHandler(ctx); |
32 | Slf4jLog log = new Slf4jLog(ServerLauncher.class.getName()); | 38 | } |
33 | try { | 39 | |
34 | server.start(); | 40 | public void start() throws Exception { |
35 | log.info("Server started " + server.getURI() + "..."); | 41 | server.start(); |
36 | new Thread() { | 42 | LOG.info("Server started " + server.getURI() + "..."); |
37 | public void run() { | 43 | new Thread() { |
38 | try { | 44 | public void run() { |
39 | log.info("Press enter to stop the server..."); | 45 | try { |
40 | int key = System.in.read(); | 46 | LOG.info("Press enter to stop the server..."); |
41 | if (key != -1) { | 47 | int key = System.in.read(); |
42 | server.stop(); | 48 | if (key != -1) { |
43 | } else { | 49 | server.stop(); |
44 | log.warn( | 50 | } else { |
45 | "Console input is not available. In order to stop the server, you need to cancel process manually."); | 51 | LOG.warn( |
46 | } | 52 | "Console input is not available. In order to stop the server, you need to cancel process manually."); |
47 | } catch (Exception e) { | ||
48 | log.warn(e); | ||
49 | } | 53 | } |
54 | } catch (Exception e) { | ||
55 | LOG.warn(e); | ||
56 | System.exit(-1); | ||
50 | } | 57 | } |
51 | }.start(); | 58 | } |
59 | }.start(); | ||
60 | try { | ||
52 | server.join(); | 61 | server.join(); |
62 | } catch (InterruptedException e) { | ||
63 | LOG.info(e); | ||
64 | } | ||
65 | } | ||
66 | |||
67 | private static InetSocketAddress getBindAddress(String listenAddress, int port) { | ||
68 | if (listenAddress == null) { | ||
69 | return new InetSocketAddress(port); | ||
70 | } | ||
71 | return new InetSocketAddress(listenAddress, port); | ||
72 | } | ||
73 | |||
74 | private static Resource getBaseResource(String baseResourceOverride) throws IOException, URISyntaxException { | ||
75 | if (baseResourceOverride != null) { | ||
76 | return Resource.newResource(baseResourceOverride); | ||
77 | } | ||
78 | URL indexUrlInJar = ServerLauncher.class.getResource("/webapp/index.html"); | ||
79 | if (indexUrlInJar == null) { | ||
80 | throw new RuntimeException("Cannot find pacakged web assets"); | ||
81 | } | ||
82 | URI webRootUri = URI.create(indexUrlInJar.toURI().toASCIIString().replaceFirst("/index.html$", "/")); | ||
83 | return Resource.newResource(webRootUri); | ||
84 | } | ||
85 | |||
86 | public static void main(String[] args) { | ||
87 | String listenAddress = System.getenv("LiSTEN_ADDRESS"); | ||
88 | int port = 8080; | ||
89 | String portStr = System.getenv("LISTEN_PORT"); | ||
90 | if (portStr != null) { | ||
91 | try { | ||
92 | port = Integer.parseInt(portStr); | ||
93 | } catch (NumberFormatException e) { | ||
94 | LOG.warn(e); | ||
95 | System.exit(-1); | ||
96 | } | ||
97 | } | ||
98 | String baseResourceOverride = System.getenv("BASE_RESOURCE"); | ||
99 | try { | ||
100 | InetSocketAddress bindAddress = getBindAddress(listenAddress, port); | ||
101 | Resource baseResource = getBaseResource(baseResourceOverride); | ||
102 | ServerLauncher serverLauncher = new ServerLauncher(bindAddress, baseResource); | ||
103 | serverLauncher.start(); | ||
53 | } catch (Exception exception) { | 104 | } catch (Exception exception) { |
54 | log.warn(exception.getMessage()); | 105 | LOG.warn(exception); |
55 | System.exit(1); | 106 | System.exit(1); |
56 | } | 107 | } |
57 | } | 108 | } |