launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 15 Dec 2012 16:25:28 +0100
branchlauncher
changeset 323 d41cfd77842d
child 330 ee4e8ba0a847
permissions -rw-r--r--
Basic sketch of an HTTP server for launching bck2brwsr applications
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.launcher;
    19 
    20 import java.awt.Desktop;
    21 import java.io.InputStream;
    22 import java.io.OutputStream;
    23 import java.io.Writer;
    24 import java.net.URI;
    25 import java.net.URL;
    26 import java.util.Enumeration;
    27 import org.apidesign.vm4brwsr.Bck2Brwsr;
    28 import org.glassfish.grizzly.PortRange;
    29 import org.glassfish.grizzly.http.server.HttpHandler;
    30 import org.glassfish.grizzly.http.server.HttpServer;
    31 import org.glassfish.grizzly.http.server.NetworkListener;
    32 import org.glassfish.grizzly.http.server.Request;
    33 import org.glassfish.grizzly.http.server.Response;
    34 import org.glassfish.grizzly.http.server.ServerConfiguration;
    35 
    36 /**
    37  * Lightweight server to launch Bck2Brwsr applications in real browser.
    38  */
    39 public class Bck2BrwsrLauncher {
    40     public static void main( String[] args ) throws Exception {
    41         HttpServer server = HttpServer.createSimpleServer(".", new PortRange(8080, 65535));
    42         final ClassLoader loader = Bck2BrwsrLauncher.class.getClassLoader();
    43         
    44         final ServerConfiguration conf = server.getServerConfiguration();
    45         conf.addHttpHandler(new HttpHandler() {
    46             @Override
    47             public void service(Request request, Response response) throws Exception {
    48                 response.setContentType("text/html");
    49                 OutputStream os = response.getOutputStream();
    50                 InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream("console.xhtml");
    51                 for (;;) {
    52                     int ch = is.read();
    53                     if (ch == -1) {
    54                         break;
    55                     }
    56                     os.write(ch);
    57                 }
    58             }
    59         }, "/console");
    60         conf.addHttpHandler(new HttpHandler() {
    61             @Override
    62             public void service(Request request, Response response) throws Exception {
    63                 response.setCharacterEncoding("UTF-8");
    64                 response.setContentType("text/javascript");
    65                 Bck2Brwsr.generate(response.getWriter(), loader);
    66             }
    67         }, "/bck2brwsr.js");
    68         conf.addHttpHandler(new HttpHandler() {
    69             @Override
    70             public void service(Request request, Response response) throws Exception {
    71                 String res = request.getHttpHandlerPath();
    72                 if (res.startsWith("/")) {
    73                     res = res.substring(1);
    74                 }
    75                 Enumeration<URL> en = loader.getResources(res);
    76                 URL u = null;
    77                 while (en.hasMoreElements()) {
    78                     u = en.nextElement();
    79                 }
    80                 if (u == null) {
    81                     response.setError();
    82                     response.setDetailMessage("Can't find resource " + res);
    83                 }
    84                 response.setContentType("text/javascript");
    85                 InputStream is = u.openStream();
    86                 Writer w = response.getWriter();
    87                 w.append("[");
    88                 for (int i = 0;; i++) {
    89                     int b = is.read();
    90                     if (b == -1) {
    91                         break;
    92                     }
    93                     if (i > 0) {
    94                         w.append(", ");
    95                     }
    96                     if (i % 20 == 0) {
    97                         w.write("\n");
    98                     }
    99                     if (b > 127) {
   100                         b = b - 256;
   101                     }
   102                     w.append(Integer.toString(b));
   103                 }
   104                 w.append("\n]");
   105             }
   106         }, "/classes/");
   107         
   108         server.start();
   109         NetworkListener listener = server.getListeners().iterator().next();
   110         int port = listener.getPort();
   111         
   112         URI uri = new URI("http://localhost:" + port + "/console");
   113         try {
   114             Desktop.getDesktop().browse(uri);
   115         } catch (UnsupportedOperationException ex) {
   116             String[] cmd = { 
   117                 "xdg-open", uri.toString()
   118             };
   119             Runtime.getRuntime().exec(cmd).waitFor();
   120         }
   121         
   122         System.in.read();
   123     }
   124 }