]> git.argeo.org Git - gpl/argeo-slc.git/blob - swt/rap/org.argeo.tool.rap.cli/src/org/argeo/tool/rap/cli/RwtRunner.java
4cd6de13583513495b662897649ab275522384bc
[gpl/argeo-slc.git] / swt / rap / org.argeo.tool.rap.cli / src / org / argeo / tool / rap / cli / RwtRunner.java
1 package org.argeo.tool.rap.cli;
2
3 import java.io.IOException;
4 import java.lang.management.ManagementFactory;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.util.Objects;
8
9 import javax.servlet.ServletContextEvent;
10 import javax.servlet.ServletContextListener;
11
12 import org.argeo.minidesktop.MiniDesktopManager;
13 import org.eclipse.jetty.ee8.servlet.DefaultServlet;
14 import org.eclipse.jetty.ee8.servlet.ServletContextHandler;
15 import org.eclipse.jetty.ee8.servlet.ServletHolder;
16 import org.eclipse.jetty.server.Connector;
17 import org.eclipse.jetty.server.Server;
18 import org.eclipse.jetty.server.ServerConnector;
19 import org.eclipse.jetty.util.thread.QueuedThreadPool;
20 import org.eclipse.rap.rwt.application.Application.OperationMode;
21 import org.eclipse.rap.rwt.application.ApplicationConfiguration;
22 import org.eclipse.rap.rwt.application.ApplicationRunner;
23 import org.eclipse.rap.rwt.application.EntryPoint;
24 import org.eclipse.rap.rwt.engine.RWTServlet;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Label;
28
29 /** A minimal RWT runner based on embedded Jetty. */
30 public class RwtRunner {
31
32 private final Server server;
33 private final ServerConnector serverConnector;
34 private Path tempDir;
35
36 private ApplicationConfiguration applicationConfiguration;
37
38 public RwtRunner() {
39 server = new Server(new QueuedThreadPool(10, 1));
40 serverConnector = new ServerConnector(server);
41 serverConnector.setPort(0);
42 server.setConnectors(new Connector[] { serverConnector });
43 }
44
45 protected Control createUi(Composite parent, Object context) {
46 return new Label(parent, 0);
47 }
48
49 public void init() {
50 Objects.requireNonNull(applicationConfiguration);
51
52 ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
53 context.setContextPath("/");
54 server.setHandler(context);
55
56 String entryPoint = "app";
57
58 // rwt-resources requires a file system
59 try {
60 tempDir = Files.createTempDirectory("argeo-rwtRunner");
61 // FIXME we need a base directory
62 // context.setBaseResource(new PathResource(tempDir.resolve("www")));
63 } catch (IOException e) {
64 throw new IllegalStateException("Cannot create temporary directory", e);
65 }
66 context.addEventListener(new ServletContextListener() {
67 ApplicationRunner applicationRunner;
68
69 @Override
70 public void contextInitialized(ServletContextEvent sce) {
71 applicationRunner = new ApplicationRunner(applicationConfiguration, sce.getServletContext());
72 applicationRunner.start();
73 }
74
75 @Override
76 public void contextDestroyed(ServletContextEvent sce) {
77 applicationRunner.stop();
78 }
79 });
80
81 context.addServlet(new ServletHolder(new RWTServlet()), "/" + entryPoint);
82
83 // Required to serve rwt-resources. It is important that this is last.
84 ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
85 context.addServlet(holderPwd, "/");
86
87 try {
88 server.start();
89 } catch (Exception e) {
90 throw new IllegalStateException("Cannot start Jetty server", e);
91 }
92 Runtime.getRuntime().addShutdownHook(new Thread(() -> destroy(), "Jetty shutdown"));
93
94 long jvmUptime = ManagementFactory.getRuntimeMXBean().getUptime();
95 System.out.println("RWT App available in " + jvmUptime + " ms, on port " + getEffectivePort());
96 }
97
98 public void destroy() {
99 try {
100 serverConnector.close();
101 server.stop();
102 // TODO delete temp dir
103 } catch (Exception e) {
104 e.printStackTrace();
105 }
106 }
107
108 public Integer getEffectivePort() {
109 return serverConnector.getLocalPort();
110 }
111
112 public void waitFor() throws InterruptedException {
113 server.join();
114 }
115
116 public void setApplicationConfiguration(ApplicationConfiguration applicationConfiguration) {
117 this.applicationConfiguration = applicationConfiguration;
118 }
119
120 public static void main(String[] args) throws Exception {
121 RwtRunner rwtRunner = new RwtRunner();
122
123 String entryPoint = "app";
124 ApplicationConfiguration applicationConfiguration = (application) -> {
125 application.setOperationMode(OperationMode.SWT_COMPATIBILITY);
126 application.addEntryPoint("/" + entryPoint, () -> new EntryPoint() {
127 @Override
128 public int createUI() {
129 MiniDesktopManager miniDesktopManager = new MiniDesktopManager(false, false);
130 miniDesktopManager.init();
131 miniDesktopManager.run();
132 return 0;
133 }
134 }, null);
135 };
136
137 rwtRunner.setApplicationConfiguration(applicationConfiguration);
138 rwtRunner.init();
139
140 // open browser in app mode
141 Thread.sleep(2000);// wait for RWT to be ready
142 Runtime.getRuntime().exec("google-chrome --app=http://localhost:" + rwtRunner.getEffectivePort() + "/app");
143
144 rwtRunner.waitFor();
145 }
146 }