2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.launcher;
20 import java.awt.Desktop;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.io.Writer;
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;
37 * Lightweight server to launch Bck2Brwsr applications in real browser.
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();
44 final ServerConfiguration conf = server.getServerConfiguration();
45 conf.addHttpHandler(new HttpHandler() {
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");
60 conf.addHttpHandler(new HttpHandler() {
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);
68 conf.addHttpHandler(new HttpHandler() {
70 public void service(Request request, Response response) throws Exception {
71 String res = request.getHttpHandlerPath();
72 if (res.startsWith("/")) {
73 res = res.substring(1);
75 Enumeration<URL> en = loader.getResources(res);
77 while (en.hasMoreElements()) {
82 response.setDetailMessage("Can't find resource " + res);
84 response.setContentType("text/javascript");
85 InputStream is = u.openStream();
86 Writer w = response.getWriter();
88 for (int i = 0;; i++) {
102 w.append(Integer.toString(b));
109 NetworkListener listener = server.getListeners().iterator().next();
110 int port = listener.getPort();
112 URI uri = new URI("http://localhost:" + port + "/console");
114 Desktop.getDesktop().browse(uri);
115 } catch (UnsupportedOperationException ex) {
117 "xdg-open", uri.toString()
119 Runtime.getRuntime().exec(cmd).waitFor();