launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/FXBrwsrLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 May 2013 06:09:42 +0200
changeset 1166 16555ef29e9e
parent 1165 06e7a74c72cf
child 1167 fd8ac9eb0008
permissions -rw-r--r--
When in debug mode, add toolbar
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.launcher;
    19 
    20 import org.apidesign.bck2brwsr.launcher.fximpl.FXBrwsr;
    21 import java.io.IOException;
    22 import java.io.InputStream;
    23 import java.lang.reflect.Method;
    24 import java.net.URI;
    25 import java.net.URL;
    26 import java.net.URLClassLoader;
    27 import java.util.Enumeration;
    28 import java.util.List;
    29 
    30 import java.util.concurrent.Executors;
    31 import java.util.jar.Manifest;
    32 import java.util.logging.Level;
    33 import java.util.logging.Logger;
    34 import javafx.application.Platform;
    35 import org.apidesign.bck2brwsr.launcher.fximpl.JVMBridge;
    36 
    37 /**
    38  *
    39  * @author Jaroslav Tulach <jtulach@netbeans.org>
    40  */
    41 final class FXBrwsrLauncher extends BaseHTTPLauncher {
    42     private static final Logger LOG = Logger.getLogger(FXBrwsrLauncher.class.getName());
    43     static {
    44         try {
    45             Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
    46             m.setAccessible(true);
    47             URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar");
    48             LOG.log(Level.INFO, "url : {0}", l);
    49             m.invoke(ClassLoader.getSystemClassLoader(), l);
    50         } catch (Exception ex) {
    51             throw new LinkageError("Can't add jfxrt.jar on the classpath", ex);
    52         }
    53     }
    54 
    55     public FXBrwsrLauncher(String ignore) {
    56         super(null);
    57     }
    58 
    59     @Override
    60     protected Object[] showBrwsr(final URI url) throws IOException {
    61         try {
    62             LOG.log(Level.INFO, "showBrwsr for {0}", url);
    63             JVMBridge.registerClassLoaders(loaders());
    64             LOG.info("About to launch WebView");
    65             Executors.newSingleThreadExecutor().submit(new Runnable() {
    66                 @Override
    67                 public void run() {
    68                     LOG.log(Level.INFO, "In FX thread. Launching!");
    69                     try {
    70                         if (isDebugged()) {
    71                             FXBrwsr.launch(FXBrwsr.class, url.toString(), "--toolbar=true");
    72                         } else {
    73                             FXBrwsr.launch(FXBrwsr.class, url.toString());
    74                         }
    75                         LOG.log(Level.INFO, "Launcher is back. Closing");
    76                         close();
    77                         System.exit(0);
    78                     } catch (Throwable ex) {
    79                         LOG.log(Level.WARNING, "Error launching Web View", ex);
    80                     }
    81                 }
    82             });
    83         } catch (Throwable ex) {
    84             LOG.log(Level.WARNING, "Can't open WebView", ex);
    85         }
    86         return null;
    87     }
    88     
    89     @Override
    90     void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException {
    91         sb.append("(function() {\n"
    92             + "  var impl = this.bck2brwsr;\n"
    93             + "  this.bck2brwsr = function() { return impl; };\n");
    94         if (isDebugged()) {
    95             sb.append("var scr = window.document.createElement('script');\n");
    96             sb.append("scr.type = 'text/javascript';\n");
    97             sb.append("scr.src = 'https://getfirebug.com/firebug-lite.js';\n");
    98             sb.append("scr.text = '{ startOpened: true }';\n");
    99             sb.append("var head = window.document.getElementsByTagName('head')[0];");
   100             sb.append("head.appendChild(scr);\n");
   101             sb.append("var html = window.document.getElementsByTagName('html')[0];");
   102             sb.append("html.debug = true;\n");
   103         }
   104         
   105         sb.append("})(window);\n");
   106         JVMBridge.onBck2BrwsrLoad();
   107     }
   108 
   109     @Override
   110     public void close() throws IOException {
   111         super.close();
   112         Platform.exit();
   113     }
   114 
   115     String harnessResource() {
   116         return "org/apidesign/bck2brwsr/launcher/fximpl/harness.xhtml";
   117     }
   118 
   119     public static void main(String... args) throws IOException {
   120         String startPage = null;
   121 
   122         final ClassLoader cl = FXBrwsrLauncher.class.getClassLoader();
   123         startPage = findStartPage(cl, startPage);
   124         if (startPage == null) {
   125             throw new NullPointerException("Can't find StartPage tag in manifests!");
   126         }
   127         
   128         Launcher.showURL("fxbrwsr", cl, startPage);
   129     }
   130     
   131     private static String findStartPage(final ClassLoader cl, String startPage) throws IOException {
   132         Enumeration<URL> en = cl.getResources("META-INF/MANIFEST.MF");
   133         while (en.hasMoreElements()) {
   134             URL url = en.nextElement();
   135             Manifest mf;
   136             InputStream is = null;
   137             try {
   138                 is = url.openStream();
   139                 mf = new Manifest(is);
   140             } finally {
   141                 if (is != null) is.close();
   142             }
   143             String sp = mf.getMainAttributes().getValue("StartPage");
   144             if (sp != null) {
   145                 startPage = sp;
   146                 break;
   147             }
   148         }
   149         return startPage;
   150     }
   151     
   152     private static boolean isDebugged() {
   153         try {
   154             return isDebuggedImpl();
   155         } catch (LinkageError e) {
   156             return false;
   157         }
   158     }
   159 
   160     private static boolean isDebuggedImpl() {
   161         java.lang.management.RuntimeMXBean runtime;
   162         runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();
   163         List<String> args = runtime.getInputArguments();
   164         if (args.contains("-Xdebug")) { // NOI18N
   165             return true;
   166         }
   167         return false;
   168     }
   169 }