2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 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;
21 import org.apidesign.bck2brwsr.launcher.fximpl.FXBrwsr;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.lang.reflect.Method;
25 import java.net.JarURLConnection;
27 import java.net.URISyntaxException;
29 import java.net.URLClassLoader;
30 import java.util.ArrayList;
31 import java.util.Enumeration;
32 import java.util.List;
34 import java.util.concurrent.Executors;
35 import java.util.jar.Manifest;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38 import javafx.application.Platform;
39 import org.apidesign.bck2brwsr.launcher.fximpl.JVMBridge;
43 * @author Jaroslav Tulach <jtulach@netbeans.org>
45 final class FXBrwsrLauncher extends BaseHTTPLauncher {
46 private static final Logger LOG = Logger.getLogger(FXBrwsrLauncher.class.getName());
49 Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
50 m.setAccessible(true);
51 URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar");
52 LOG.log(Level.INFO, "url : {0}", l);
53 m.invoke(ClassLoader.getSystemClassLoader(), l);
54 } catch (Exception ex) {
55 throw new LinkageError("Can't add jfxrt.jar on the classpath", ex);
59 public FXBrwsrLauncher(String ignore) {
64 protected Object[] showBrwsr(final URI url) throws IOException {
66 LOG.log(Level.INFO, "showBrwsr for {0}", url);
67 JVMBridge.registerClassLoaders(loaders());
68 LOG.info("About to launch WebView");
69 Executors.newSingleThreadExecutor().submit(new Runnable() {
72 LOG.log(Level.INFO, "In FX thread. Launching!");
74 List<String> params = new ArrayList<String>();
75 params.add(url.toString());
77 params.add("--toolbar=true");
78 params.add("--firebug=true");
79 String ud = System.getProperty("netbeans.user");
81 params.add("--userdir=" + ud);
84 FXBrwsr.launch(FXBrwsr.class, params.toArray(new String[params.size()]));
85 LOG.log(Level.INFO, "Launcher is back. Closing");
88 } catch (Throwable ex) {
89 LOG.log(Level.WARNING, "Error launching Web View", ex);
93 } catch (Throwable ex) {
94 LOG.log(Level.WARNING, "Can't open WebView", ex);
100 void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException {
101 sb.append("(function() {\n"
102 + " var impl = this.bck2brwsr;\n"
103 + " this.bck2brwsr = function() { return impl; };\n");
104 sb.append("})(window);\n");
105 JVMBridge.onBck2BrwsrLoad();
109 public void close() throws IOException {
114 String harnessResource() {
115 return "org/apidesign/bck2brwsr/launcher/fximpl/harness.xhtml";
118 public static void main(String... args) throws IOException {
119 String startPage = null;
121 final ClassLoader cl = FXBrwsrLauncher.class.getClassLoader();
122 URL[] manifestURL = { null };
123 startPage = findStartPage(cl, startPage, manifestURL);
124 if (startPage == null) {
125 throw new NullPointerException("Can't find StartPage tag in manifests!");
128 File dir = new File(".");
129 if (manifestURL[0].getProtocol().equals("jar")) {
132 ((JarURLConnection)manifestURL[0].openConnection()).getJarFileURL().toURI()
134 } catch (URISyntaxException ex) {
135 LOG.log(Level.WARNING, "Can't find root directory", ex);
139 Launcher.showDir("fxbrwsr", dir, cl, startPage);
142 private static String findStartPage(
143 final ClassLoader cl, String startPage, URL[] startURL
144 ) throws IOException {
145 Enumeration<URL> en = cl.getResources("META-INF/MANIFEST.MF");
146 while (en.hasMoreElements()) {
147 URL url = en.nextElement();
149 InputStream is = null;
151 is = url.openStream();
152 mf = new Manifest(is);
154 if (is != null) is.close();
156 String sp = mf.getMainAttributes().getValue("StartPage");
159 if (startURL != null) {
168 private static boolean isDebugged() {
170 return isDebuggedImpl();
171 } catch (LinkageError e) {
176 private static boolean isDebuggedImpl() {
177 java.lang.management.RuntimeMXBean runtime;
178 runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();
179 List<String> args = runtime.getInputArguments();
180 if (args.contains("-Xdebug")) { // NOI18N