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@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@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@323: conf.addHttpHandler(new HttpHandler() { jaroslav@323: @Override jaroslav@323: public void service(Request request, Response response) throws Exception { jaroslav@323: response.setContentType("text/html"); jaroslav@323: OutputStream os = response.getOutputStream(); jaroslav@323: InputStream is = Bck2BrwsrLauncher.class.getResourceAsStream("console.xhtml"); jaroslav@323: for (;;) { jaroslav@323: int ch = is.read(); jaroslav@323: if (ch == -1) { jaroslav@323: break; jaroslav@323: } jaroslav@323: os.write(ch); jaroslav@323: } jaroslav@323: } jaroslav@323: }, "/console"); jaroslav@323: conf.addHttpHandler(new HttpHandler() { jaroslav@323: @Override jaroslav@323: public void service(Request request, Response response) throws Exception { jaroslav@323: response.setCharacterEncoding("UTF-8"); jaroslav@323: response.setContentType("text/javascript"); jaroslav@323: Bck2Brwsr.generate(response.getWriter(), loader); jaroslav@323: } jaroslav@323: }, "/bck2brwsr.js"); jaroslav@323: conf.addHttpHandler(new HttpHandler() { jaroslav@323: @Override jaroslav@323: public void service(Request request, Response response) throws Exception { jaroslav@323: String res = request.getHttpHandlerPath(); jaroslav@323: if (res.startsWith("/")) { jaroslav@323: res = res.substring(1); jaroslav@323: } jaroslav@323: Enumeration en = loader.getResources(res); jaroslav@323: URL u = null; jaroslav@323: while (en.hasMoreElements()) { jaroslav@323: u = en.nextElement(); jaroslav@323: } jaroslav@323: if (u == null) { jaroslav@323: response.setError(); jaroslav@323: response.setDetailMessage("Can't find resource " + res); jaroslav@323: } jaroslav@323: response.setContentType("text/javascript"); jaroslav@323: InputStream is = u.openStream(); jaroslav@323: Writer w = response.getWriter(); jaroslav@323: w.append("["); jaroslav@323: for (int i = 0;; i++) { jaroslav@323: int b = is.read(); jaroslav@323: if (b == -1) { jaroslav@323: break; jaroslav@323: } jaroslav@323: if (i > 0) { jaroslav@323: w.append(", "); jaroslav@323: } jaroslav@323: if (i % 20 == 0) { jaroslav@323: w.write("\n"); jaroslav@323: } jaroslav@323: if (b > 127) { jaroslav@323: b = b - 256; jaroslav@323: } jaroslav@323: w.append(Integer.toString(b)); jaroslav@323: } jaroslav@323: w.append("\n]"); jaroslav@323: } jaroslav@323: }, "/classes/"); jaroslav@323: jaroslav@323: server.start(); jaroslav@323: NetworkListener listener = server.getListeners().iterator().next(); jaroslav@323: int port = listener.getPort(); jaroslav@323: jaroslav@323: URI uri = new URI("http://localhost:" + port + "/console"); 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@323: }