launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
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.io.Closeable;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InterruptedIOException;
25 import java.io.OutputStream;
26 import java.io.UnsupportedEncodingException;
27 import java.io.Writer;
29 import java.net.URISyntaxException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Enumeration;
34 import java.util.LinkedHashSet;
35 import java.util.List;
37 import java.util.concurrent.BlockingQueue;
38 import java.util.concurrent.CountDownLatch;
39 import java.util.concurrent.LinkedBlockingQueue;
40 import java.util.concurrent.TimeUnit;
41 import java.util.logging.Level;
42 import java.util.logging.Logger;
43 import org.apidesign.bck2brwsr.launcher.InvocationContext.Resource;
44 import org.apidesign.vm4brwsr.Bck2Brwsr;
45 import org.glassfish.grizzly.PortRange;
46 import org.glassfish.grizzly.http.server.HttpHandler;
47 import org.glassfish.grizzly.http.server.HttpServer;
48 import org.glassfish.grizzly.http.server.NetworkListener;
49 import org.glassfish.grizzly.http.server.Request;
50 import org.glassfish.grizzly.http.server.Response;
51 import org.glassfish.grizzly.http.server.ServerConfiguration;
52 import org.glassfish.grizzly.http.util.HttpStatus;
55 * Lightweight server to launch Bck2Brwsr applications and tests.
56 * Supports execution in native browser as well as Java's internal
59 final class Bck2BrwsrLauncher extends Launcher implements Closeable {
60 private static final Logger LOG = Logger.getLogger(Bck2BrwsrLauncher.class.getName());
61 private static final InvocationContext END = new InvocationContext(null, null, null);
62 private final Set<ClassLoader> loaders = new LinkedHashSet<>();
63 private final BlockingQueue<InvocationContext> methods = new LinkedBlockingQueue<>();
65 private final Res resources = new Res();
66 private final String cmd;
67 private Object[] brwsr;
68 private HttpServer server;
69 private CountDownLatch wait;
71 public Bck2BrwsrLauncher(String cmd) {
73 addClassLoader(Bck2Brwsr.class.getClassLoader());
78 InvocationContext runMethod(InvocationContext c) throws IOException {
79 loaders.add(c.clazz.getClassLoader());
83 } catch (InterruptedException ex) {
84 throw new IOException(ex);
89 public void setTimeout(long ms) {
93 public void addClassLoader(ClassLoader url) {
94 this.loaders.add(url);
97 public void showURL(String startpage) throws IOException {
98 if (!startpage.startsWith("/")) {
99 startpage = "/" + startpage;
101 HttpServer s = initServer(".", true);
102 int last = startpage.lastIndexOf('/');
103 String prefix = startpage.substring(0, last);
104 String simpleName = startpage.substring(last);
105 s.getServerConfiguration().addHttpHandler(new SubTree(resources, prefix), "/");
107 launchServerAndBrwsr(s, simpleName);
108 } catch (URISyntaxException | InterruptedException ex) {
109 throw new IOException(ex);
113 void showDirectory(File dir, String startpage) throws IOException {
114 if (!startpage.startsWith("/")) {
115 startpage = "/" + startpage;
117 HttpServer s = initServer(dir.getPath(), false);
119 launchServerAndBrwsr(s, startpage);
120 } catch (URISyntaxException | InterruptedException ex) {
121 throw new IOException(ex);
126 public void initialize() throws IOException {
129 } catch (InterruptedException ex) {
130 final InterruptedIOException iio = new InterruptedIOException(ex.getMessage());
133 } catch (Exception ex) {
134 if (ex instanceof IOException) {
135 throw (IOException)ex;
137 if (ex instanceof RuntimeException) {
138 throw (RuntimeException)ex;
140 throw new IOException(ex);
144 private HttpServer initServer(String path, boolean addClasses) throws IOException {
145 HttpServer s = HttpServer.createSimpleServer(path, new PortRange(8080, 65535));
147 final ServerConfiguration conf = s.getServerConfiguration();
149 conf.addHttpHandler(new VM(resources), "/bck2brwsr.js");
150 conf.addHttpHandler(new Classes(resources), "/classes/");
155 private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
156 wait = new CountDownLatch(1);
157 server = initServer(".", true);
158 final ServerConfiguration conf = server.getServerConfiguration();
160 class DynamicResourceHandler extends HttpHandler {
161 private final InvocationContext ic;
162 public DynamicResourceHandler(InvocationContext ic) {
163 if (ic == null || ic.resources.isEmpty()) {
164 throw new NullPointerException();
167 for (Resource r : ic.resources) {
168 conf.addHttpHandler(this, r.httpPath);
172 public void close() {
173 conf.removeHttpHandler(this);
177 public void service(Request request, Response response) throws Exception {
178 for (Resource r : ic.resources) {
179 if (r.httpPath.equals(request.getRequestURI())) {
180 LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI());
181 response.setContentType(r.httpType);
182 r.httpContent.reset();
183 String[] params = null;
184 if (r.parameters.length != 0) {
185 params = new String[r.parameters.length];
186 for (int i = 0; i < r.parameters.length; i++) {
187 params[i] = request.getParameter(r.parameters[i]);
191 copyStream(r.httpContent, response.getOutputStream(), null, params);
197 conf.addHttpHandler(new Page(resources,
198 "org/apidesign/bck2brwsr/launcher/harness.xhtml"
201 conf.addHttpHandler(new HttpHandler() {
203 List<InvocationContext> cases = new ArrayList<>();
204 DynamicResourceHandler prev;
206 public void service(Request request, Response response) throws Exception {
207 String id = request.getParameter("request");
208 String value = request.getParameter("result");
209 if (value != null && value.indexOf((char)0xC5) != -1) {
210 value = toUTF8(value);
214 InvocationContext mi = null;
217 if (id != null && value != null) {
218 LOG.log(Level.INFO, "Received result for case {0} = {1}", new Object[]{id, value});
219 value = decodeURL(value);
220 int indx = Integer.parseInt(id);
221 cases.get(indx).result(value, null);
222 if (++indx < cases.size()) {
223 mi = cases.get(indx);
224 LOG.log(Level.INFO, "Re-executing case {0}", indx);
228 if (!cases.isEmpty()) {
229 LOG.info("Re-executing test cases");
245 response.getWriter().write("");
248 LOG.log(Level.INFO, "End of data reached. Exiting.");
252 if (!mi.resources.isEmpty()) {
253 prev = new DynamicResourceHandler(mi);
257 final String cn = mi.clazz.getName();
258 final String mn = mi.methodName;
259 LOG.log(Level.INFO, "Request for {0} case. Sending {1}.{2}", new Object[]{caseNmbr, cn, mn});
260 response.getWriter().write("{"
261 + "className: '" + cn + "', "
262 + "methodName: '" + mn + "', "
263 + "request: " + caseNmbr
265 if (mi.html != null) {
266 response.getWriter().write(", html: '");
267 response.getWriter().write(encodeJSON(mi.html));
268 response.getWriter().write("'");
270 response.getWriter().write("}");
274 this.brwsr = launchServerAndBrwsr(server, "/execute");
277 private static String encodeJSON(String in) {
278 StringBuilder sb = new StringBuilder();
279 for (int i = 0; i < in.length(); i++) {
280 char ch = in.charAt(i);
281 if (ch < 32 || ch == '\'' || ch == '"') {
283 String hs = "0000" + Integer.toHexString(ch);
284 hs = hs.substring(hs.length() - 4);
290 return sb.toString();
294 public void shutdown() throws IOException {
297 int prev = methods.size();
299 if (wait != null && wait.await(timeOut, TimeUnit.MILLISECONDS)) {
302 } catch (InterruptedException ex) {
303 throw new IOException(ex);
305 if (prev == methods.size()) {
308 "Timeout and no test has been executed meanwhile (at {0}). Giving up.",
314 "Timeout, but tests got from {0} to {1}. Trying again.",
315 new Object[]{prev, methods.size()}
318 stopServerAndBrwsr(server, brwsr);
321 static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException {
327 if (ch == '$' && params.length > 0) {
328 int cnt = is.read() - '0';
329 if (baseURL != null && cnt == 'U' - '0') {
330 os.write(baseURL.getBytes("UTF-8"));
332 if (cnt >= 0 && cnt < params.length) {
333 os.write(params[cnt].getBytes("UTF-8"));
345 private Object[] launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException {
347 NetworkListener listener = server.getListeners().iterator().next();
348 int port = listener.getPort();
350 URI uri = new URI("http://localhost:" + port + page);
351 LOG.log(Level.INFO, "Showing {0}", uri);
354 LOG.log(Level.INFO, "Trying Desktop.browse on {0} {2} by {1}", new Object[] {
355 System.getProperty("java.vm.name"),
356 System.getProperty("java.vm.vendor"),
357 System.getProperty("java.vm.version"),
359 java.awt.Desktop.getDesktop().browse(uri);
360 LOG.log(Level.INFO, "Desktop.browse successfully finished");
362 } catch (UnsupportedOperationException ex) {
363 LOG.log(Level.INFO, "Desktop.browse not supported: {0}", ex.getMessage());
364 LOG.log(Level.FINE, null, ex);
368 String cmdName = cmd == null ? "xdg-open" : cmd;
370 cmdName, uri.toString()
372 LOG.log(Level.INFO, "Launching {0}", Arrays.toString(cmdArr));
373 final Process process = Runtime.getRuntime().exec(cmdArr);
374 return new Object[] { process, null };
377 private static String toUTF8(String value) throws UnsupportedEncodingException {
378 byte[] arr = new byte[value.length()];
379 for (int i = 0; i < arr.length; i++) {
380 arr[i] = (byte)value.charAt(i);
382 return new String(arr, "UTF-8");
385 private static String decodeURL(String s) {
387 int pos = s.indexOf('%');
391 int i = Integer.parseInt(s.substring(pos + 1, pos + 2), 16);
392 s = s.substring(0, pos) + (char)i + s.substring(pos + 2);
396 private void stopServerAndBrwsr(HttpServer server, Object[] brwsr) throws IOException {
400 Process process = (Process)brwsr[0];
403 InputStream stdout = process.getInputStream();
404 InputStream stderr = process.getErrorStream();
405 drain("StdOut", stdout);
406 drain("StdErr", stderr);
410 res = process.waitFor();
411 } catch (InterruptedException ex) {
412 throw new IOException(ex);
414 LOG.log(Level.INFO, "Exit code: {0}", res);
416 deleteTree((File)brwsr[1]);
419 private static void drain(String name, InputStream is) throws IOException {
420 int av = is.available();
422 StringBuilder sb = new StringBuilder();
423 sb.append("v== ").append(name).append(" ==v\n");
425 sb.append((char)is.read());
427 sb.append("\n^== ").append(name).append(" ==^");
428 LOG.log(Level.INFO, sb.toString());
432 private void deleteTree(File file) {
436 File[] arr = file.listFiles();
446 public void close() throws IOException {
450 private class Res implements Bck2Brwsr.Resources {
452 public InputStream get(String resource) throws IOException {
453 for (ClassLoader l : loaders) {
455 Enumeration<URL> en = l.getResources(resource);
456 while (en.hasMoreElements()) {
457 u = en.nextElement();
460 return u.openStream();
463 throw new IOException("Can't find " + resource);
467 private static class Page extends HttpHandler {
468 final String resource;
469 private final String[] args;
470 private final Res res;
472 public Page(Res res, String resource, String... args) {
474 this.resource = resource;
475 this.args = args.length == 0 ? new String[] { "$0" } : args;
479 public void service(Request request, Response response) throws Exception {
480 String r = computePage(request);
481 if (r.startsWith("/")) {
484 String[] replace = {};
485 if (r.endsWith(".html")) {
486 response.setContentType("text/html");
487 LOG.info("Content type text/html");
490 if (r.endsWith(".xhtml")) {
491 response.setContentType("application/xhtml+xml");
492 LOG.info("Content type application/xhtml+xml");
495 OutputStream os = response.getOutputStream();
496 try (InputStream is = res.get(r)) {
497 copyStream(is, os, request.getRequestURL().toString(), replace);
498 } catch (IOException ex) {
499 response.setDetailMessage(ex.getLocalizedMessage());
501 response.setStatus(404);
505 protected String computePage(Request request) {
508 r = request.getHttpHandlerPath();
514 private static class SubTree extends Page {
516 public SubTree(Res res, String resource, String... args) {
517 super(res, resource, args);
521 protected String computePage(Request request) {
522 return resource + request.getHttpHandlerPath();
528 private static class VM extends HttpHandler {
529 private final String bck2brwsr;
531 public VM(Res loader) throws IOException {
532 StringBuilder sb = new StringBuilder();
533 Bck2Brwsr.generate(sb, loader);
535 "(function WrapperVM(global) {"
536 + " function ldCls(res) {\n"
537 + " var request = new XMLHttpRequest();\n"
538 + " request.open('GET', '/classes/' + res, false);\n"
539 + " request.send();\n"
540 + " if (request.status !== 200) return null;\n"
541 + " var arr = eval('(' + request.responseText + ')');\n"
544 + " var prevvm = global.bck2brwsr;\n"
545 + " global.bck2brwsr = function() {\n"
546 + " var args = Array.prototype.slice.apply(arguments);\n"
547 + " args.unshift(ldCls);\n"
548 + " return prevvm.apply(null, args);\n"
552 this.bck2brwsr = sb.toString();
556 public void service(Request request, Response response) throws Exception {
557 response.setCharacterEncoding("UTF-8");
558 response.setContentType("text/javascript");
559 response.getWriter().write(bck2brwsr);
563 private static class Classes extends HttpHandler {
564 private final Res loader;
566 public Classes(Res loader) {
567 this.loader = loader;
571 public void service(Request request, Response response) throws Exception {
572 String res = request.getHttpHandlerPath();
573 if (res.startsWith("/")) {
574 res = res.substring(1);
576 try (InputStream is = loader.get(res)) {
577 response.setContentType("text/javascript");
578 Writer w = response.getWriter();
580 for (int i = 0;; i++) {
594 w.append(Integer.toString(b));
597 } catch (IOException ex) {
598 response.setStatus(HttpStatus.NOT_FOUND_404);
600 response.setDetailMessage(ex.getMessage());