|
1 | 1 | package com.vaadin.starter.skeleton;
|
2 | 2 |
|
3 |
| -import com.vaadin.flow.server.VaadinServlet; |
4 |
| -import com.vaadin.flow.server.startup.ServletContextListeners; |
5 |
| -import org.eclipse.jetty.annotations.AnnotationConfiguration; |
6 |
| -import org.eclipse.jetty.server.Server; |
7 |
| -import org.eclipse.jetty.util.resource.Resource; |
8 |
| -import org.eclipse.jetty.webapp.*; |
9 |
| -import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer; |
| 3 | +import com.github.mvysny.vaadinboot.VaadinBoot; |
10 | 4 | import org.jetbrains.annotations.NotNull;
|
11 | 5 |
|
12 |
| -import java.net.MalformedURLException; |
13 |
| -import java.net.URL; |
14 |
| - |
15 | 6 | /**
|
16 | 7 | * Run {@link #main(String[])} to launch your app in Embedded Jetty.
|
17 | 8 | * @author mavi
|
18 | 9 | */
|
19 | 10 | public final class Main {
|
20 |
| - |
21 |
| - private static Server server; |
22 |
| - |
23 | 11 | public static void main(@NotNull String[] args) throws Exception {
|
24 |
| - start(args); |
25 |
| - server.join(); |
26 |
| - } |
27 |
| - |
28 |
| - public static void start(@NotNull String[] args) throws Exception { |
29 |
| - // detect&enable production mode |
30 |
| - if (isProductionMode()) { |
31 |
| - // fixes https://github.com/mvysny/vaadin14-embedded-jetty/issues/1 |
32 |
| - System.out.println("Production mode detected, enforcing"); |
33 |
| - System.setProperty("vaadin.productionMode", "true"); |
34 |
| - } |
35 |
| - |
36 |
| - final WebAppContext context = new WebAppContext(); |
37 |
| - context.setBaseResource(findWebRoot()); |
38 |
| - context.setContextPath("/"); |
39 |
| - context.addServlet(VaadinServlet.class, "/*"); |
40 |
| - context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*\\.jar|.*/classes/.*"); |
41 |
| - context.setConfigurationDiscovered(true); |
42 |
| - context.getServletContext().setExtendedListenerTypes(true); |
43 |
| - context.addEventListener(new ServletContextListeners()); |
44 |
| - WebSocketServerContainerInitializer.initialize(context); // fixes IllegalStateException: Unable to configure jsr356 at that stage. ServerContainer is null |
45 |
| - |
46 |
| - int port = 8080; |
47 |
| - if (args.length >= 1) { |
48 |
| - port = Integer.parseInt(args[0]); |
49 |
| - } |
50 |
| - server = new Server(port); |
51 |
| - server.setHandler(context); |
52 |
| - final Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server); |
53 |
| - classlist.addBefore(JettyWebXmlConfiguration.class.getName(), AnnotationConfiguration.class.getName()); |
54 |
| - server.start(); |
55 |
| - |
56 |
| - System.out.println("\n\n=================================================\n\n" + |
57 |
| - "Please open http://localhost:" + port + " in your browser\n\n" + |
58 |
| - "If you see the 'Unable to determine mode of operation' exception, just kill me and run `mvn -C clean package`\n\n" + |
59 |
| - "=================================================\n\n"); |
60 |
| - } |
61 |
| - |
62 |
| - public static void stop() throws Exception { |
63 |
| - server.stop(); |
64 |
| - server = null; |
65 |
| - } |
66 |
| - |
67 |
| - private static boolean isProductionMode() { |
68 |
| - final String probe = "META-INF/maven/com.vaadin/flow-server-production-mode/pom.xml"; |
69 |
| - final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); |
70 |
| - return classLoader.getResource(probe) != null; |
71 |
| - } |
72 |
| - |
73 |
| - @NotNull |
74 |
| - private static Resource findWebRoot() throws MalformedURLException { |
75 |
| - // don't look up directory as a resource, it's unreliable: https://github.com/eclipse/jetty.project/issues/4173#issuecomment-539769734 |
76 |
| - // instead we'll look up the /webapp/ROOT and retrieve the parent folder from that. |
77 |
| - final URL f = Main.class.getResource("/webapp/ROOT"); |
78 |
| - if (f == null) { |
79 |
| - throw new IllegalStateException("Invalid state: the resource /webapp/ROOT doesn't exist, has webapp been packaged in as a resource?"); |
80 |
| - } |
81 |
| - final String url = f.toString(); |
82 |
| - if (!url.endsWith("/ROOT")) { |
83 |
| - throw new RuntimeException("Parameter url: invalid value " + url + ": doesn't end with /ROOT"); |
84 |
| - } |
85 |
| - System.err.println("/webapp/ROOT is " + f); |
86 |
| - |
87 |
| - // Resolve file to directory |
88 |
| - URL webRoot = new URL(url.substring(0, url.length() - 5)); |
89 |
| - System.err.println("WebRoot is " + webRoot); |
90 |
| - return Resource.newResource(webRoot); |
| 12 | + new VaadinBoot().withArgs(args).run(); |
91 | 13 | }
|
92 | 14 | }
|
93 |
| - |
0 commit comments