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.dew;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.Writer;
24 import java.net.URISyntaxException;
26 import java.util.Arrays;
27 import java.util.Enumeration;
28 import java.util.LinkedHashSet;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32 import org.apidesign.vm4brwsr.Bck2Brwsr;
33 import org.glassfish.grizzly.PortRange;
34 import org.glassfish.grizzly.http.server.HttpHandler;
35 import org.glassfish.grizzly.http.server.HttpServer;
36 import org.glassfish.grizzly.http.server.NetworkListener;
37 import org.glassfish.grizzly.http.server.Request;
38 import org.glassfish.grizzly.http.server.Response;
39 import org.glassfish.grizzly.http.server.ServerConfiguration;
42 * Lightweight server to launch dew - the Development Environment for Web.
44 final class DewLauncher {
45 private static final Logger LOG = Logger.getLogger(DewLauncher.class.getName());
46 private Set<ClassLoader> loaders = new LinkedHashSet<>();
47 private Set<Bck2Brwsr.Resources> xRes = new LinkedHashSet<>();
48 private final Res resources = new Res();
49 private final String cmd;
51 public DewLauncher(String cmd) {
55 public void addClassLoader(ClassLoader url) {
56 this.loaders.add(url);
59 final HttpServer initServer(Bck2Brwsr.Resources... extraResources) {
60 xRes.addAll(Arrays.asList(extraResources));
62 HttpServer s = HttpServer.createSimpleServer(".", new PortRange(8080, 65535));
64 final ServerConfiguration conf = s.getServerConfiguration();
65 conf.addHttpHandler(new VM(resources), "/bck2brwsr.js");
66 conf.addHttpHandler(new VMInit(), "/vm.js");
67 conf.addHttpHandler(new Classes(resources), "/classes/");
71 final Object[] launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException {
73 NetworkListener listener = server.getListeners().iterator().next();
74 int port = listener.getPort();
76 URI uri = new URI("http://localhost:" + port + page);
77 LOG.log(Level.INFO, "Showing {0}", uri);
80 LOG.log(Level.INFO, "Trying Desktop.browse on {0} {2} by {1}", new Object[] {
81 System.getProperty("java.vm.name"),
82 System.getProperty("java.vm.vendor"),
83 System.getProperty("java.vm.version"),
85 java.awt.Desktop.getDesktop().browse(uri);
86 LOG.log(Level.INFO, "Desktop.browse successfully finished");
88 } catch (UnsupportedOperationException ex) {
89 LOG.log(Level.INFO, "Desktop.browse not supported: {0}", ex.getMessage());
90 LOG.log(Level.FINE, null, ex);
94 String cmdName = cmd == null ? "xdg-open" : cmd;
96 cmdName, uri.toString()
98 LOG.log(Level.INFO, "Launching {0}", Arrays.toString(cmdArr));
99 final Process process = Runtime.getRuntime().exec(cmdArr);
100 return new Object[] { process, null };
104 private class Res implements Bck2Brwsr.Resources {
106 public InputStream get(String resource) throws IOException {
107 for (ClassLoader l : loaders) {
109 Enumeration<URL> en = l.getResources(resource);
110 while (en.hasMoreElements()) {
111 u = en.nextElement();
114 return u.openStream();
117 for (Bck2Brwsr.Resources r : xRes) {
118 InputStream is = r.get(resource);
123 throw new IOException("Can't find " + resource);
127 private static class VM extends HttpHandler {
128 private final Res loader;
130 public VM(Res loader) {
131 this.loader = loader;
135 public void service(Request request, Response response) throws Exception {
136 response.setCharacterEncoding("UTF-8");
137 response.setContentType("text/javascript");
138 Bck2Brwsr.generate(response.getWriter(), loader);
141 private static class VMInit extends HttpHandler {
146 public void service(Request request, Response response) throws Exception {
147 response.setCharacterEncoding("UTF-8");
148 response.setContentType("text/javascript");
149 response.getWriter().append(
150 "function ldCls(res) {\n"
151 + " var request = new XMLHttpRequest();\n"
152 + " request.open('GET', '/classes/' + res, false);\n"
153 + " request.send();\n"
154 + " var arr = eval('(' + request.responseText + ')');\n"
157 + "var vm = new bck2brwsr(ldCls);\n");
161 private static class Classes extends HttpHandler {
162 private final Res loader;
164 public Classes(Res loader) {
165 this.loader = loader;
169 public void service(Request request, Response response) throws Exception {
170 String res = request.getHttpHandlerPath();
171 if (res.startsWith("/")) {
172 res = res.substring(1);
174 try (InputStream is = loader.get(res)) {
175 response.setContentType("text/javascript");
176 Writer w = response.getWriter();
178 for (int i = 0;; i++) {
192 w.append(Integer.toString(b));
195 } catch (IOException ex) {
197 response.setDetailMessage(ex.getMessage());