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 org.apidesign.bck2brwsr.launcher.fximpl.FXBrwsr;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.lang.reflect.Method;
26 import java.net.URLClassLoader;
27 import java.util.ArrayList;
28 import java.util.Enumeration;
29 import java.util.List;
31 import java.util.concurrent.Executors;
32 import java.util.jar.Manifest;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
35 import javafx.application.Platform;
36 import org.apidesign.bck2brwsr.launcher.fximpl.JVMBridge;
40 * @author Jaroslav Tulach <jtulach@netbeans.org>
42 final class FXBrwsrLauncher extends BaseHTTPLauncher {
43 private static final Logger LOG = Logger.getLogger(FXBrwsrLauncher.class.getName());
46 Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
47 m.setAccessible(true);
48 URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar");
49 LOG.log(Level.INFO, "url : {0}", l);
50 m.invoke(ClassLoader.getSystemClassLoader(), l);
51 } catch (Exception ex) {
52 throw new LinkageError("Can't add jfxrt.jar on the classpath", ex);
56 public FXBrwsrLauncher(String ignore) {
61 protected Object[] showBrwsr(final URI url) throws IOException {
63 LOG.log(Level.INFO, "showBrwsr for {0}", url);
64 JVMBridge.registerClassLoaders(loaders());
65 LOG.info("About to launch WebView");
66 Executors.newSingleThreadExecutor().submit(new Runnable() {
69 LOG.log(Level.INFO, "In FX thread. Launching!");
71 List<String> params = new ArrayList<String>();
72 params.add(url.toString());
74 params.add("--toolbar=true");
75 params.add("--firebug=true");
76 String ud = System.getProperty("netbeans.user");
78 params.add("--userdir=" + ud);
81 FXBrwsr.launch(FXBrwsr.class, params.toArray(new String[params.size()]));
82 LOG.log(Level.INFO, "Launcher is back. Closing");
85 } catch (Throwable ex) {
86 LOG.log(Level.WARNING, "Error launching Web View", ex);
90 } catch (Throwable ex) {
91 LOG.log(Level.WARNING, "Can't open WebView", ex);
97 void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException {
98 sb.append("(function() {\n"
99 + " var impl = this.bck2brwsr;\n"
100 + " this.bck2brwsr = function() { return impl; };\n");
101 sb.append("})(window);\n");
102 JVMBridge.onBck2BrwsrLoad();
106 public void close() throws IOException {
111 String harnessResource() {
112 return "org/apidesign/bck2brwsr/launcher/fximpl/harness.xhtml";
115 public static void main(String... args) throws IOException {
116 String startPage = null;
118 final ClassLoader cl = FXBrwsrLauncher.class.getClassLoader();
119 startPage = findStartPage(cl, startPage);
120 if (startPage == null) {
121 throw new NullPointerException("Can't find StartPage tag in manifests!");
124 Launcher.showURL("fxbrwsr", cl, startPage);
127 private static String findStartPage(final ClassLoader cl, String startPage) throws IOException {
128 Enumeration<URL> en = cl.getResources("META-INF/MANIFEST.MF");
129 while (en.hasMoreElements()) {
130 URL url = en.nextElement();
132 InputStream is = null;
134 is = url.openStream();
135 mf = new Manifest(is);
137 if (is != null) is.close();
139 String sp = mf.getMainAttributes().getValue("StartPage");
148 private static boolean isDebugged() {
150 return isDebuggedImpl();
151 } catch (LinkageError e) {
156 private static boolean isDebuggedImpl() {
157 java.lang.management.RuntimeMXBean runtime;
158 runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();
159 List<String> args = runtime.getInputArguments();
160 if (args.contains("-Xdebug")) { // NOI18N