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
jaroslav@323
     1
/**
jaroslav@323
     2
 * Back 2 Browser Bytecode Translator
jaroslav@323
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@323
     4
 *
jaroslav@323
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@323
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@323
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@323
     8
 *
jaroslav@323
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@323
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@323
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@323
    12
 * GNU General Public License for more details.
jaroslav@323
    13
 *
jaroslav@323
    14
 * You should have received a copy of the GNU General Public License
jaroslav@323
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@323
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@323
    17
 */
jaroslav@323
    18
package org.apidesign.bck2brwsr.launcher;
jaroslav@323
    19
jaroslav@323
    20
import java.awt.Desktop;
jaroslav@323
    21
import java.io.InputStream;
jaroslav@323
    22
import java.io.OutputStream;
jaroslav@323
    23
import java.io.Writer;
jaroslav@323
    24
import java.net.URI;
jaroslav@323
    25
import java.net.URL;
jaroslav@323
    26
import java.util.Enumeration;
jaroslav@323
    27
import org.apidesign.vm4brwsr.Bck2Brwsr;
jaroslav@323
    28
import org.glassfish.grizzly.PortRange;
jaroslav@323
    29
import org.glassfish.grizzly.http.server.HttpHandler;
jaroslav@323
    30
import org.glassfish.grizzly.http.server.HttpServer;
jaroslav@323
    31
import org.glassfish.grizzly.http.server.NetworkListener;
jaroslav@323
    32
import org.glassfish.grizzly.http.server.Request;
jaroslav@323
    33
import org.glassfish.grizzly.http.server.Response;
jaroslav@323
    34
import org.glassfish.grizzly.http.server.ServerConfiguration;
jaroslav@323
    35
jaroslav@323
    36
/**
jaroslav@323
    37
 * Lightweight server to launch Bck2Brwsr applications in real browser.
jaroslav@323
    38
 */
jaroslav@323
    39
public class Bck2BrwsrLauncher {
jaroslav@323
    40
    public static void main( String[] args ) throws Exception {
jaroslav@323
    41
        HttpServer server = HttpServer.createSimpleServer(".", new PortRange(8080, 65535));
jaroslav@323
    42
        final ClassLoader loader = Bck2BrwsrLauncher.class.getClassLoader();
jaroslav@323
    43
        
jaroslav@323
    44
        final ServerConfiguration conf = server.getServerConfiguration();
jaroslav@323
    45
        conf.addHttpHandler(new HttpHandler() {
jaroslav@323
    46
            @Override
jaroslav@323
    47
            public void service(Request request, Response response) throws Exception {
jaroslav@323
    48
                response.setContentType("text/html");
jaroslav@323
    49
                OutputStream os = response.getOutputStream();
jaroslav@323
    50
                InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream("console.xhtml");
jaroslav@323
    51
                for (;;) {
jaroslav@323
    52
                    int ch = is.read();
jaroslav@323
    53
                    if (ch == -1) {
jaroslav@323
    54
                        break;
jaroslav@323
    55
                    }
jaroslav@323
    56
                    os.write(ch);
jaroslav@323
    57
                }
jaroslav@323
    58
            }
jaroslav@323
    59
        }, "/console");
jaroslav@323
    60
        conf.addHttpHandler(new HttpHandler() {
jaroslav@323
    61
            @Override
jaroslav@323
    62
            public void service(Request request, Response response) throws Exception {
jaroslav@323
    63
                response.setCharacterEncoding("UTF-8");
jaroslav@323
    64
                response.setContentType("text/javascript");
jaroslav@323
    65
                Bck2Brwsr.generate(response.getWriter(), loader);
jaroslav@323
    66
            }
jaroslav@323
    67
        }, "/bck2brwsr.js");
jaroslav@323
    68
        conf.addHttpHandler(new HttpHandler() {
jaroslav@323
    69
            @Override
jaroslav@323
    70
            public void service(Request request, Response response) throws Exception {
jaroslav@323
    71
                String res = request.getHttpHandlerPath();
jaroslav@323
    72
                if (res.startsWith("/")) {
jaroslav@323
    73
                    res = res.substring(1);
jaroslav@323
    74
                }
jaroslav@323
    75
                Enumeration<URL> en = loader.getResources(res);
jaroslav@323
    76
                URL u = null;
jaroslav@323
    77
                while (en.hasMoreElements()) {
jaroslav@323
    78
                    u = en.nextElement();
jaroslav@323
    79
                }
jaroslav@323
    80
                if (u == null) {
jaroslav@323
    81
                    response.setError();
jaroslav@323
    82
                    response.setDetailMessage("Can't find resource " + res);
jaroslav@323
    83
                }
jaroslav@323
    84
                response.setContentType("text/javascript");
jaroslav@323
    85
                InputStream is = u.openStream();
jaroslav@323
    86
                Writer w = response.getWriter();
jaroslav@323
    87
                w.append("[");
jaroslav@323
    88
                for (int i = 0;; i++) {
jaroslav@323
    89
                    int b = is.read();
jaroslav@323
    90
                    if (b == -1) {
jaroslav@323
    91
                        break;
jaroslav@323
    92
                    }
jaroslav@323
    93
                    if (i > 0) {
jaroslav@323
    94
                        w.append(", ");
jaroslav@323
    95
                    }
jaroslav@323
    96
                    if (i % 20 == 0) {
jaroslav@323
    97
                        w.write("\n");
jaroslav@323
    98
                    }
jaroslav@323
    99
                    if (b > 127) {
jaroslav@323
   100
                        b = b - 256;
jaroslav@323
   101
                    }
jaroslav@323
   102
                    w.append(Integer.toString(b));
jaroslav@323
   103
                }
jaroslav@323
   104
                w.append("\n]");
jaroslav@323
   105
            }
jaroslav@323
   106
        }, "/classes/");
jaroslav@323
   107
        
jaroslav@323
   108
        server.start();
jaroslav@323
   109
        NetworkListener listener = server.getListeners().iterator().next();
jaroslav@323
   110
        int port = listener.getPort();
jaroslav@323
   111
        
jaroslav@323
   112
        URI uri = new URI("http://localhost:" + port + "/console");
jaroslav@323
   113
        try {
jaroslav@323
   114
            Desktop.getDesktop().browse(uri);
jaroslav@323
   115
        } catch (UnsupportedOperationException ex) {
jaroslav@323
   116
            String[] cmd = { 
jaroslav@323
   117
                "xdg-open", uri.toString()
jaroslav@323
   118
            };
jaroslav@323
   119
            Runtime.getRuntime().exec(cmd).waitFor();
jaroslav@323
   120
        }
jaroslav@323
   121
        
jaroslav@323
   122
        System.in.read();
jaroslav@323
   123
    }
jaroslav@323
   124
}