jaroslav@323: /** jaroslav@323: * Back 2 Browser Bytecode Translator jaroslav@323: * Copyright (C) 2012 Jaroslav Tulach jaroslav@323: * jaroslav@323: * This program is free software: you can redistribute it and/or modify jaroslav@323: * it under the terms of the GNU General Public License as published by jaroslav@323: * the Free Software Foundation, version 2 of the License. jaroslav@323: * jaroslav@323: * This program is distributed in the hope that it will be useful, jaroslav@323: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@323: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@323: * GNU General Public License for more details. jaroslav@323: * jaroslav@323: * You should have received a copy of the GNU General Public License jaroslav@323: * along with this program. Look for COPYING file in the top folder. jaroslav@323: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@323: */ jaroslav@323: package org.apidesign.bck2brwsr.launcher; jaroslav@323: jaroslav@323: import java.awt.Desktop; jaroslav@331: import java.io.IOException; jaroslav@323: import java.io.InputStream; jaroslav@323: import java.io.OutputStream; jaroslav@323: import java.io.Writer; jaroslav@323: import java.net.URI; jaroslav@323: import java.net.URL; jaroslav@323: import java.util.Enumeration; jaroslav@331: import static org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher.copyStream; jaroslav@323: import org.apidesign.vm4brwsr.Bck2Brwsr; jaroslav@323: import org.glassfish.grizzly.PortRange; jaroslav@323: import org.glassfish.grizzly.http.server.HttpHandler; jaroslav@323: import org.glassfish.grizzly.http.server.HttpServer; jaroslav@323: import org.glassfish.grizzly.http.server.NetworkListener; jaroslav@323: import org.glassfish.grizzly.http.server.Request; jaroslav@323: import org.glassfish.grizzly.http.server.Response; jaroslav@323: import org.glassfish.grizzly.http.server.ServerConfiguration; jaroslav@323: jaroslav@323: /** jaroslav@323: * Lightweight server to launch Bck2Brwsr applications in real browser. jaroslav@323: */ jaroslav@323: public class Bck2BrwsrLauncher { jaroslav@323: public static void main( String[] args ) throws Exception { jaroslav@323: HttpServer server = HttpServer.createSimpleServer(".", new PortRange(8080, 65535)); jaroslav@323: final ClassLoader loader = Bck2BrwsrLauncher.class.getClassLoader(); jaroslav@323: jaroslav@323: final ServerConfiguration conf = server.getServerConfiguration(); jaroslav@331: conf.addHttpHandler(new Console("org.apidesign.bck2brwsr.launcher.Console", "welcome", "false"), "/console"); jaroslav@330: conf.addHttpHandler(new VM(loader), "/bck2brwsr.js"); jaroslav@330: conf.addHttpHandler(new Classes(loader), "/classes/"); jaroslav@331: conf.addHttpHandler(new HttpHandler() { jaroslav@331: @Override jaroslav@331: public void service(Request request, Response response) throws Exception { jaroslav@331: String clazz = request.getParameter("class"); jaroslav@331: String method = request.getParameter("method"); jaroslav@331: if (clazz == null || method == null) { jaroslav@331: response.setError(); jaroslav@331: response.setDetailMessage("Need two parameters: class and method name!"); jaroslav@331: return; jaroslav@331: } jaroslav@331: response.setContentType("text/html"); jaroslav@331: OutputStream os = response.getOutputStream(); jaroslav@331: InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream("console.xhtml"); jaroslav@331: copyStream(is, os, clazz, method, "true"); jaroslav@331: } jaroslav@331: }, "/execute"); jaroslav@323: jaroslav@323: server.start(); jaroslav@323: NetworkListener listener = server.getListeners().iterator().next(); jaroslav@323: int port = listener.getPort(); jaroslav@323: jaroslav@331: URI uri = new URI("http://localhost:" + port + "/execute?class=org.apidesign.bck2brwsr.launcher.Console&method=welcome"); jaroslav@323: try { jaroslav@323: Desktop.getDesktop().browse(uri); jaroslav@323: } catch (UnsupportedOperationException ex) { jaroslav@323: String[] cmd = { jaroslav@323: "xdg-open", uri.toString() jaroslav@323: }; jaroslav@323: Runtime.getRuntime().exec(cmd).waitFor(); jaroslav@323: } jaroslav@323: jaroslav@323: System.in.read(); jaroslav@323: } jaroslav@331: jaroslav@331: static void copyStream(InputStream is, OutputStream os, String... params) throws IOException { jaroslav@331: for (;;) { jaroslav@331: int ch = is.read(); jaroslav@331: if (ch == -1) { jaroslav@331: break; jaroslav@331: } jaroslav@331: if (ch == '$') { jaroslav@331: int cnt = is.read() - '0'; jaroslav@331: if (cnt < params.length) { jaroslav@331: os.write(params[cnt].getBytes()); jaroslav@331: } jaroslav@331: } else { jaroslav@331: os.write(ch); jaroslav@331: } jaroslav@331: } jaroslav@331: } jaroslav@330: jaroslav@330: private static class Console extends HttpHandler { jaroslav@331: private final String[] args; jaroslav@331: jaroslav@331: public Console(String... args) { jaroslav@331: this.args = args; jaroslav@330: } jaroslav@330: jaroslav@330: @Override jaroslav@330: public void service(Request request, Response response) throws Exception { jaroslav@330: response.setContentType("text/html"); jaroslav@330: OutputStream os = response.getOutputStream(); jaroslav@330: InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream("console.xhtml"); jaroslav@331: copyStream(is, os, args); jaroslav@330: } jaroslav@330: } jaroslav@330: jaroslav@330: private static class VM extends HttpHandler { jaroslav@330: private final ClassLoader loader; jaroslav@330: jaroslav@330: public VM(ClassLoader loader) { jaroslav@330: this.loader = loader; jaroslav@330: } jaroslav@330: jaroslav@330: @Override jaroslav@330: public void service(Request request, Response response) throws Exception { jaroslav@330: response.setCharacterEncoding("UTF-8"); jaroslav@330: response.setContentType("text/javascript"); jaroslav@330: Bck2Brwsr.generate(response.getWriter(), loader); jaroslav@330: } jaroslav@330: } jaroslav@330: jaroslav@330: private static class Classes extends HttpHandler { jaroslav@330: private final ClassLoader loader; jaroslav@330: jaroslav@330: public Classes(ClassLoader loader) { jaroslav@330: this.loader = loader; jaroslav@330: } jaroslav@330: jaroslav@330: @Override jaroslav@330: public void service(Request request, Response response) throws Exception { jaroslav@330: String res = request.getHttpHandlerPath(); jaroslav@330: if (res.startsWith("/")) { jaroslav@330: res = res.substring(1); jaroslav@330: } jaroslav@330: Enumeration en = loader.getResources(res); jaroslav@330: URL u = null; jaroslav@330: while (en.hasMoreElements()) { jaroslav@330: u = en.nextElement(); jaroslav@330: } jaroslav@330: if (u == null) { jaroslav@330: response.setError(); jaroslav@330: response.setDetailMessage("Can't find resource " + res); jaroslav@330: } jaroslav@330: response.setContentType("text/javascript"); jaroslav@330: InputStream is = u.openStream(); jaroslav@330: Writer w = response.getWriter(); jaroslav@330: w.append("["); jaroslav@330: for (int i = 0;; i++) { jaroslav@330: int b = is.read(); jaroslav@330: if (b == -1) { jaroslav@330: break; jaroslav@330: } jaroslav@330: if (i > 0) { jaroslav@330: w.append(", "); jaroslav@330: } jaroslav@330: if (i % 20 == 0) { jaroslav@330: w.write("\n"); jaroslav@330: } jaroslav@330: if (b > 127) { jaroslav@330: b = b - 256; jaroslav@330: } jaroslav@330: w.append(Integer.toString(b)); jaroslav@330: } jaroslav@330: w.append("\n]"); jaroslav@330: } jaroslav@330: } jaroslav@323: }