Rewritting the archetype to be simpler and more easily modifiable to one's needs
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;
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;
40 import org.openide.util.Exceptions;
44 * @author Jaroslav Tulach <jtulach@netbeans.org>
46 final class FXBrwsrLauncher extends BaseHTTPLauncher {
47 private static final Logger LOG = Logger.getLogger(FXBrwsrLauncher.class.getName());
50 Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
51 m.setAccessible(true);
52 URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar");
53 LOG.log(Level.INFO, "url : {0}", l);
54 m.invoke(ClassLoader.getSystemClassLoader(), l);
55 } catch (Exception ex) {
56 throw new LinkageError("Can't add jfxrt.jar on the classpath", ex);
60 public FXBrwsrLauncher(String ignore) {
65 protected Object[] showBrwsr(final URI url) throws IOException {
67 LOG.log(Level.INFO, "showBrwsr for {0}", url);
68 JVMBridge.registerClassLoaders(loaders());
69 LOG.info("About to launch WebView");
70 Executors.newSingleThreadExecutor().submit(new Runnable() {
73 LOG.log(Level.INFO, "In FX thread. Launching!");
75 List<String> params = new ArrayList<String>();
76 params.add(url.toString());
78 params.add("--toolbar=true");
79 params.add("--firebug=true");
80 String ud = System.getProperty("netbeans.user");
82 params.add("--userdir=" + ud);
85 FXBrwsr.launch(FXBrwsr.class, params.toArray(new String[params.size()]));
86 LOG.log(Level.INFO, "Launcher is back. Closing");
89 } catch (Throwable ex) {
90 LOG.log(Level.WARNING, "Error launching Web View", ex);
94 } catch (Throwable ex) {
95 LOG.log(Level.WARNING, "Can't open WebView", ex);
101 void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException {
102 sb.append("(function() {\n"
103 + " var impl = this.bck2brwsr;\n"
104 + " this.bck2brwsr = function() { return impl; };\n");
105 sb.append("})(window);\n");
106 JVMBridge.onBck2BrwsrLoad();
110 public void close() throws IOException {
115 String harnessResource() {
116 return "org/apidesign/bck2brwsr/launcher/fximpl/harness.xhtml";
119 public static void main(String... args) throws IOException {
120 String startPage = null;
122 final ClassLoader cl = FXBrwsrLauncher.class.getClassLoader();
123 URL[] manifestURL = { null };
124 startPage = findStartPage(cl, startPage, manifestURL);
125 if (startPage == null) {
126 throw new NullPointerException("Can't find StartPage tag in manifests!");
129 File dir = new File(".");
130 if (manifestURL[0].getProtocol().equals("jar")) {
133 ((JarURLConnection)manifestURL[0].openConnection()).getJarFileURL().toURI()
135 } catch (URISyntaxException ex) {
136 LOG.log(Level.WARNING, "Can't find root directory", ex);
140 Launcher.showDir("fxbrwsr", dir, cl, startPage);
143 private static String findStartPage(
144 final ClassLoader cl, String startPage, URL[] startURL
145 ) throws IOException {
146 Enumeration<URL> en = cl.getResources("META-INF/MANIFEST.MF");
147 while (en.hasMoreElements()) {
148 URL url = en.nextElement();
150 InputStream is = null;
152 is = url.openStream();
153 mf = new Manifest(is);
155 if (is != null) is.close();
157 String sp = mf.getMainAttributes().getValue("StartPage");
160 if (startURL != null) {
169 private static boolean isDebugged() {
171 return isDebuggedImpl();
172 } catch (LinkageError e) {
177 private static boolean isDebuggedImpl() {
178 java.lang.management.RuntimeMXBean runtime;
179 runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();
180 List<String> args = runtime.getInputArguments();
181 if (args.contains("-Xdebug")) { // NOI18N