1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Sat Dec 15 16:25:28 2012 +0100
1.3 @@ -0,0 +1,124 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.launcher;
1.22 +
1.23 +import java.awt.Desktop;
1.24 +import java.io.InputStream;
1.25 +import java.io.OutputStream;
1.26 +import java.io.Writer;
1.27 +import java.net.URI;
1.28 +import java.net.URL;
1.29 +import java.util.Enumeration;
1.30 +import org.apidesign.vm4brwsr.Bck2Brwsr;
1.31 +import org.glassfish.grizzly.PortRange;
1.32 +import org.glassfish.grizzly.http.server.HttpHandler;
1.33 +import org.glassfish.grizzly.http.server.HttpServer;
1.34 +import org.glassfish.grizzly.http.server.NetworkListener;
1.35 +import org.glassfish.grizzly.http.server.Request;
1.36 +import org.glassfish.grizzly.http.server.Response;
1.37 +import org.glassfish.grizzly.http.server.ServerConfiguration;
1.38 +
1.39 +/**
1.40 + * Lightweight server to launch Bck2Brwsr applications in real browser.
1.41 + */
1.42 +public class Bck2BrwsrLauncher {
1.43 + public static void main( String[] args ) throws Exception {
1.44 + HttpServer server = HttpServer.createSimpleServer(".", new PortRange(8080, 65535));
1.45 + final ClassLoader loader = Bck2BrwsrLauncher.class.getClassLoader();
1.46 +
1.47 + final ServerConfiguration conf = server.getServerConfiguration();
1.48 + conf.addHttpHandler(new HttpHandler() {
1.49 + @Override
1.50 + public void service(Request request, Response response) throws Exception {
1.51 + response.setContentType("text/html");
1.52 + OutputStream os = response.getOutputStream();
1.53 + InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream("console.xhtml");
1.54 + for (;;) {
1.55 + int ch = is.read();
1.56 + if (ch == -1) {
1.57 + break;
1.58 + }
1.59 + os.write(ch);
1.60 + }
1.61 + }
1.62 + }, "/console");
1.63 + conf.addHttpHandler(new HttpHandler() {
1.64 + @Override
1.65 + public void service(Request request, Response response) throws Exception {
1.66 + response.setCharacterEncoding("UTF-8");
1.67 + response.setContentType("text/javascript");
1.68 + Bck2Brwsr.generate(response.getWriter(), loader);
1.69 + }
1.70 + }, "/bck2brwsr.js");
1.71 + conf.addHttpHandler(new HttpHandler() {
1.72 + @Override
1.73 + public void service(Request request, Response response) throws Exception {
1.74 + String res = request.getHttpHandlerPath();
1.75 + if (res.startsWith("/")) {
1.76 + res = res.substring(1);
1.77 + }
1.78 + Enumeration<URL> en = loader.getResources(res);
1.79 + URL u = null;
1.80 + while (en.hasMoreElements()) {
1.81 + u = en.nextElement();
1.82 + }
1.83 + if (u == null) {
1.84 + response.setError();
1.85 + response.setDetailMessage("Can't find resource " + res);
1.86 + }
1.87 + response.setContentType("text/javascript");
1.88 + InputStream is = u.openStream();
1.89 + Writer w = response.getWriter();
1.90 + w.append("[");
1.91 + for (int i = 0;; i++) {
1.92 + int b = is.read();
1.93 + if (b == -1) {
1.94 + break;
1.95 + }
1.96 + if (i > 0) {
1.97 + w.append(", ");
1.98 + }
1.99 + if (i % 20 == 0) {
1.100 + w.write("\n");
1.101 + }
1.102 + if (b > 127) {
1.103 + b = b - 256;
1.104 + }
1.105 + w.append(Integer.toString(b));
1.106 + }
1.107 + w.append("\n]");
1.108 + }
1.109 + }, "/classes/");
1.110 +
1.111 + server.start();
1.112 + NetworkListener listener = server.getListeners().iterator().next();
1.113 + int port = listener.getPort();
1.114 +
1.115 + URI uri = new URI("http://localhost:" + port + "/console");
1.116 + try {
1.117 + Desktop.getDesktop().browse(uri);
1.118 + } catch (UnsupportedOperationException ex) {
1.119 + String[] cmd = {
1.120 + "xdg-open", uri.toString()
1.121 + };
1.122 + Runtime.getRuntime().exec(cmd).waitFor();
1.123 + }
1.124 +
1.125 + System.in.read();
1.126 + }
1.127 +}