Basic sketch of an HTTP server for launching bck2brwsr applications launcher
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 15 Dec 2012 16:25:28 +0100
branchlauncher
changeset 323d41cfd77842d
parent 322 3884815c0629
child 329 a1c84cc2ed14
Basic sketch of an HTTP server for launching bck2brwsr applications
launcher/pom.xml
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java
launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/console.xhtml
pom.xml
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/launcher/pom.xml	Sat Dec 15 16:25:28 2012 +0100
     1.3 @@ -0,0 +1,36 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
     1.6 +    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     1.7 +  <modelVersion>4.0.0</modelVersion>
     1.8 +  <parent>
     1.9 +    <groupId>org.apidesign</groupId>
    1.10 +    <artifactId>bck2brwsr</artifactId>
    1.11 +    <version>0.3-SNAPSHOT</version>
    1.12 +  </parent>
    1.13 +  <groupId>org.apidesign.bck2brwsr</groupId>
    1.14 +  <artifactId>launcher</artifactId>
    1.15 +  <version>0.3-SNAPSHOT</version>
    1.16 +  <name>Bck2Brwsr Launcher</name>
    1.17 +  <url>http://maven.apache.org</url>
    1.18 +  <properties>
    1.19 +    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    1.20 +  </properties>
    1.21 +  <dependencies>
    1.22 +    <dependency>
    1.23 +      <groupId>junit</groupId>
    1.24 +      <artifactId>junit</artifactId>
    1.25 +      <version>3.8.1</version>
    1.26 +      <scope>test</scope>
    1.27 +    </dependency>
    1.28 +    <dependency>
    1.29 +      <groupId>org.glassfish.grizzly</groupId>
    1.30 +      <artifactId>grizzly-http-server</artifactId>
    1.31 +      <version>2.2.19</version>
    1.32 +    </dependency>
    1.33 +    <dependency>
    1.34 +      <groupId>${project.groupId}</groupId>
    1.35 +      <artifactId>vm4brwsr</artifactId>
    1.36 +      <version>${project.version}</version>
    1.37 +    </dependency>
    1.38 +  </dependencies>
    1.39 +</project>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Sat Dec 15 16:25:28 2012 +0100
     2.3 @@ -0,0 +1,124 @@
     2.4 +/**
     2.5 + * Back 2 Browser Bytecode Translator
     2.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details.
    2.16 + *
    2.17 + * You should have received a copy of the GNU General Public License
    2.18 + * along with this program. Look for COPYING file in the top folder.
    2.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    2.20 + */
    2.21 +package org.apidesign.bck2brwsr.launcher;
    2.22 +
    2.23 +import java.awt.Desktop;
    2.24 +import java.io.InputStream;
    2.25 +import java.io.OutputStream;
    2.26 +import java.io.Writer;
    2.27 +import java.net.URI;
    2.28 +import java.net.URL;
    2.29 +import java.util.Enumeration;
    2.30 +import org.apidesign.vm4brwsr.Bck2Brwsr;
    2.31 +import org.glassfish.grizzly.PortRange;
    2.32 +import org.glassfish.grizzly.http.server.HttpHandler;
    2.33 +import org.glassfish.grizzly.http.server.HttpServer;
    2.34 +import org.glassfish.grizzly.http.server.NetworkListener;
    2.35 +import org.glassfish.grizzly.http.server.Request;
    2.36 +import org.glassfish.grizzly.http.server.Response;
    2.37 +import org.glassfish.grizzly.http.server.ServerConfiguration;
    2.38 +
    2.39 +/**
    2.40 + * Lightweight server to launch Bck2Brwsr applications in real browser.
    2.41 + */
    2.42 +public class Bck2BrwsrLauncher {
    2.43 +    public static void main( String[] args ) throws Exception {
    2.44 +        HttpServer server = HttpServer.createSimpleServer(".", new PortRange(8080, 65535));
    2.45 +        final ClassLoader loader = Bck2BrwsrLauncher.class.getClassLoader();
    2.46 +        
    2.47 +        final ServerConfiguration conf = server.getServerConfiguration();
    2.48 +        conf.addHttpHandler(new HttpHandler() {
    2.49 +            @Override
    2.50 +            public void service(Request request, Response response) throws Exception {
    2.51 +                response.setContentType("text/html");
    2.52 +                OutputStream os = response.getOutputStream();
    2.53 +                InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream("console.xhtml");
    2.54 +                for (;;) {
    2.55 +                    int ch = is.read();
    2.56 +                    if (ch == -1) {
    2.57 +                        break;
    2.58 +                    }
    2.59 +                    os.write(ch);
    2.60 +                }
    2.61 +            }
    2.62 +        }, "/console");
    2.63 +        conf.addHttpHandler(new HttpHandler() {
    2.64 +            @Override
    2.65 +            public void service(Request request, Response response) throws Exception {
    2.66 +                response.setCharacterEncoding("UTF-8");
    2.67 +                response.setContentType("text/javascript");
    2.68 +                Bck2Brwsr.generate(response.getWriter(), loader);
    2.69 +            }
    2.70 +        }, "/bck2brwsr.js");
    2.71 +        conf.addHttpHandler(new HttpHandler() {
    2.72 +            @Override
    2.73 +            public void service(Request request, Response response) throws Exception {
    2.74 +                String res = request.getHttpHandlerPath();
    2.75 +                if (res.startsWith("/")) {
    2.76 +                    res = res.substring(1);
    2.77 +                }
    2.78 +                Enumeration<URL> en = loader.getResources(res);
    2.79 +                URL u = null;
    2.80 +                while (en.hasMoreElements()) {
    2.81 +                    u = en.nextElement();
    2.82 +                }
    2.83 +                if (u == null) {
    2.84 +                    response.setError();
    2.85 +                    response.setDetailMessage("Can't find resource " + res);
    2.86 +                }
    2.87 +                response.setContentType("text/javascript");
    2.88 +                InputStream is = u.openStream();
    2.89 +                Writer w = response.getWriter();
    2.90 +                w.append("[");
    2.91 +                for (int i = 0;; i++) {
    2.92 +                    int b = is.read();
    2.93 +                    if (b == -1) {
    2.94 +                        break;
    2.95 +                    }
    2.96 +                    if (i > 0) {
    2.97 +                        w.append(", ");
    2.98 +                    }
    2.99 +                    if (i % 20 == 0) {
   2.100 +                        w.write("\n");
   2.101 +                    }
   2.102 +                    if (b > 127) {
   2.103 +                        b = b - 256;
   2.104 +                    }
   2.105 +                    w.append(Integer.toString(b));
   2.106 +                }
   2.107 +                w.append("\n]");
   2.108 +            }
   2.109 +        }, "/classes/");
   2.110 +        
   2.111 +        server.start();
   2.112 +        NetworkListener listener = server.getListeners().iterator().next();
   2.113 +        int port = listener.getPort();
   2.114 +        
   2.115 +        URI uri = new URI("http://localhost:" + port + "/console");
   2.116 +        try {
   2.117 +            Desktop.getDesktop().browse(uri);
   2.118 +        } catch (UnsupportedOperationException ex) {
   2.119 +            String[] cmd = { 
   2.120 +                "xdg-open", uri.toString()
   2.121 +            };
   2.122 +            Runtime.getRuntime().exec(cmd).waitFor();
   2.123 +        }
   2.124 +        
   2.125 +        System.in.read();
   2.126 +    }
   2.127 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java	Sat Dec 15 16:25:28 2012 +0100
     3.3 @@ -0,0 +1,65 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program. Look for COPYING file in the top folder.
    3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 + */
    3.21 +package org.apidesign.bck2brwsr.launcher;
    3.22 +
    3.23 +import java.lang.reflect.Method;
    3.24 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    3.25 +
    3.26 +/**
    3.27 + *
    3.28 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.29 + */
    3.30 +public class Console {
    3.31 +    public static String welcome() {
    3.32 +        final String msg = "Hello from Bck2Brwsr!";
    3.33 +        alert(msg);
    3.34 +        return msg;
    3.35 +    }
    3.36 +    
    3.37 +    @JavaScriptBody(args = "msg", body = "alert(msg);")
    3.38 +    private static native void alert(String msg);
    3.39 +    
    3.40 +    @JavaScriptBody(args = {"id", "attr"}, body = 
    3.41 +        "return window.document.getElementById(id)[attr].toString();")
    3.42 +    private static native Object getAttr(String id, String attr);
    3.43 +
    3.44 +    @JavaScriptBody(args = {"id", "attr", "value"}, body = 
    3.45 +        "window.document.getElementById(id)[attr] = value;")
    3.46 +    private static native void setAttr(String id, String attr, Object value);
    3.47 +    
    3.48 +    public static void execute() throws Exception {
    3.49 +        String clazz = (String) getAttr("clazz", "value");
    3.50 +        String method = (String) getAttr("method", "value");
    3.51 +
    3.52 +        Method found = null;
    3.53 +        Class<?> c = Class.forName(clazz);
    3.54 +        for (Method m : c.getMethods()) {
    3.55 +            if (m.getName().equals(method)) {
    3.56 +                found = m;
    3.57 +            }
    3.58 +        }
    3.59 +        Object res;
    3.60 +        if (found != null) {
    3.61 +            res = found.invoke(null);
    3.62 +        } else {
    3.63 +            res = "Can't find method " + method + " in " + clazz;
    3.64 +        }
    3.65 +        
    3.66 +        setAttr("result", "value", res);
    3.67 +    }
    3.68 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/console.xhtml	Sat Dec 15 16:25:28 2012 +0100
     4.3 @@ -0,0 +1,54 @@
     4.4 +<?xml version="1.0" encoding="UTF-8"?>
     4.5 +<!--
     4.6 +
     4.7 +    Back 2 Browser Bytecode Translator
     4.8 +    Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.9 +
    4.10 +    This program is free software: you can redistribute it and/or modify
    4.11 +    it under the terms of the GNU General Public License as published by
    4.12 +    the Free Software Foundation, version 2 of the License.
    4.13 +
    4.14 +    This program is distributed in the hope that it will be useful,
    4.15 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.16 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.17 +    GNU General Public License for more details.
    4.18 +
    4.19 +    You should have received a copy of the GNU General Public License
    4.20 +    along with this program. Look for COPYING file in the top folder.
    4.21 +    If not, see http://opensource.org/licenses/GPL-2.0.
    4.22 +
    4.23 +-->
    4.24 +<!DOCTYPE html>
    4.25 +<html xmlns="http://www.w3.org/1999/xhtml">
    4.26 +    <head>
    4.27 +        <title>Bck2Brwsr Launcher</title>
    4.28 +    </head>
    4.29 +    <body>
    4.30 +        <script src="bck2brwsr.js"></script>
    4.31 +        <script type="text/javascript">
    4.32 +            function ldCls(res) {
    4.33 +                var request = new XMLHttpRequest();
    4.34 +                request.open("GET", "classes/" + res, false);
    4.35 +                request.send();
    4.36 +                var arr = eval('(' + request.responseText + ')');
    4.37 +                return arr;
    4.38 +            }
    4.39 +            var vm = new bck2brwsr(ldCls);
    4.40 +        </script>
    4.41 +        <h1>Bck2Browser Console Launcher</h1>
    4.42 +        
    4.43 +        Class Name:
    4.44 +        <input id="clazz" value="org.apidesign.bck2brwsr.launcher.Console"/>
    4.45 +        <br/>
    4.46 +        Method Name:
    4.47 +
    4.48 +        <input id="method" value="welcome"/>
    4.49 +        <br/>
    4.50 +        
    4.51 +        <button onclick="vm.loadClass('org.apidesign.bck2brwsr.launcher.Console').execute__V();">Execute!</button>
    4.52 +        
    4.53 +        <hr/>
    4.54 +        <textarea id="result" rows="10" cols="80">
    4.55 +        </textarea>
    4.56 +    </body>
    4.57 +</html>
     5.1 --- a/pom.xml	Sat Dec 15 08:17:45 2012 +0100
     5.2 +++ b/pom.xml	Sat Dec 15 16:25:28 2012 +0100
     5.3 @@ -14,6 +14,7 @@
     5.4      <module>javaquery</module>
     5.5      <module>javap</module>
     5.6      <module>benchmarks</module>
     5.7 +    <module>launcher</module>
     5.8    </modules>
     5.9    <licenses>
    5.10        <license>
    5.11 @@ -109,3 +110,4 @@
    5.12        <license>COPYING</license>
    5.13    </properties>
    5.14  </project>
    5.15 +