2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.launcher;
20 import java.awt.Desktop;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.io.Writer;
26 import java.net.URISyntaxException;
28 import java.util.ArrayList;
29 import java.util.Enumeration;
30 import java.util.LinkedHashSet;
31 import java.util.List;
33 import java.util.concurrent.CountDownLatch;
34 import static org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher.copyStream;
35 import org.apidesign.vm4brwsr.Bck2Brwsr;
36 import org.glassfish.grizzly.PortRange;
37 import org.glassfish.grizzly.http.server.HttpHandler;
38 import org.glassfish.grizzly.http.server.HttpServer;
39 import org.glassfish.grizzly.http.server.NetworkListener;
40 import org.glassfish.grizzly.http.server.Request;
41 import org.glassfish.grizzly.http.server.Response;
42 import org.glassfish.grizzly.http.server.ServerConfiguration;
45 * Lightweight server to launch Bck2Brwsr applications in real browser.
47 public class Bck2BrwsrLauncher {
48 private Set<ClassLoader> loaders = new LinkedHashSet<>();
49 private List<MethodInvocation> methods = new ArrayList<>();
52 public MethodInvocation addMethod(Class<?> clazz, String method) {
53 loaders.add(clazz.getClassLoader());
54 MethodInvocation c = new MethodInvocation(clazz.getName(), method);
61 public static void main( String[] args ) throws Exception {
62 Bck2BrwsrLauncher l = new Bck2BrwsrLauncher();
64 final MethodInvocation[] cases = {
65 l.addMethod(Console.class, "welcome"),
66 l.addMethod(Console.class, "multiply"),
71 for (MethodInvocation c : cases) {
72 System.err.println(c.className + "." + c.methodName + " = " + c.result);
77 public void execute() throws URISyntaxException, IOException, InterruptedException {
78 final CountDownLatch wait = new CountDownLatch(1);
79 final MethodInvocation[] cases = this.methods.toArray(new MethodInvocation[0]);
81 HttpServer server = HttpServer.createSimpleServer(".", new PortRange(8080, 65535));
83 Res resources = new Res();
85 final ServerConfiguration conf = server.getServerConfiguration();
86 conf.addHttpHandler(new Page("console.xhtml",
87 "org.apidesign.bck2brwsr.launcher.Console", "welcome", "false"
89 conf.addHttpHandler(new VM(resources), "/bck2brwsr.js");
90 conf.addHttpHandler(new VMInit(), "/vm.js");
91 conf.addHttpHandler(new Classes(resources), "/classes/");
92 conf.addHttpHandler(new HttpHandler() {
95 public void service(Request request, Response response) throws Exception {
96 String id = request.getParameter("request");
97 String value = request.getParameter("result");
98 if (id != null && value != null) {
99 value = value.replace("%20", " ");
100 cases[Integer.parseInt(id)].result = value;
103 if (cnt >= cases.length) {
104 response.getWriter().write("");
110 response.getWriter().write("{"
111 + "className: '" + cases[cnt].className + "', "
112 + "methodName: '" + cases[cnt].methodName + "', "
118 conf.addHttpHandler(new Page("harness.xhtml"), "/");
121 NetworkListener listener = server.getListeners().iterator().next();
122 int port = listener.getPort();
124 URI uri = new URI("http://localhost:" + port + "/execute");
126 Desktop.getDesktop().browse(uri);
127 } catch (UnsupportedOperationException ex) {
129 "xdg-open", uri.toString()
131 Runtime.getRuntime().exec(cmd).waitFor();
137 static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException {
144 int cnt = is.read() - '0';
145 if (cnt == 'U' - '0') {
146 os.write(baseURL.getBytes());
148 if (cnt < params.length) {
149 os.write(params[cnt].getBytes());
157 private class Res implements Bck2Brwsr.Resources {
159 public InputStream get(String resource) throws IOException {
160 for (ClassLoader l : loaders) {
162 Enumeration<URL> en = l.getResources(resource);
163 while (en.hasMoreElements()) {
164 u = en.nextElement();
167 return u.openStream();
170 throw new IOException("Can't find " + resource);
174 private static class Page extends HttpHandler {
175 private final String resource;
176 private final String[] args;
178 public Page(String resource, String... args) {
179 this.resource = resource;
184 public void service(Request request, Response response) throws Exception {
185 response.setContentType("text/html");
186 OutputStream os = response.getOutputStream();
187 InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream(resource);
188 copyStream(is, os, request.getRequestURL().toString(), args);
192 private static class VM extends HttpHandler {
193 private final Res loader;
195 public VM(Res loader) {
196 this.loader = loader;
200 public void service(Request request, Response response) throws Exception {
201 response.setCharacterEncoding("UTF-8");
202 response.setContentType("text/javascript");
203 Bck2Brwsr.generate(response.getWriter(), loader);
206 private static class VMInit extends HttpHandler {
211 public void service(Request request, Response response) throws Exception {
212 response.setCharacterEncoding("UTF-8");
213 response.setContentType("text/javascript");
214 response.getWriter().append(
215 "function ldCls(res) {\n"
216 + " var request = new XMLHttpRequest();\n"
217 + " request.open('GET', 'classes/' + res, false);\n"
218 + " request.send();\n"
219 + " var arr = eval('(' + request.responseText + ')');\n"
222 + "var vm = new bck2brwsr(ldCls);\n");
226 private static class Classes extends HttpHandler {
227 private final Res loader;
229 public Classes(Res loader) {
230 this.loader = loader;
234 public void service(Request request, Response response) throws Exception {
235 String res = request.getHttpHandlerPath();
236 if (res.startsWith("/")) {
237 res = res.substring(1);
239 try (InputStream is = loader.get(res)) {
240 response.setContentType("text/javascript");
241 Writer w = response.getWriter();
243 for (int i = 0;; i++) {
257 w.append(Integer.toString(b));
260 } catch (IOException ex) {
262 response.setDetailMessage(ex.getMessage());
267 public static final class MethodInvocation {
268 final String className;
269 final String methodName;
272 MethodInvocation(String className, String methodName) {
273 this.className = className;
274 this.methodName = methodName;
278 public String toString() {