launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/FXBrwsrLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 31 May 2013 09:12:10 +0200
changeset 1167 fd8ac9eb0008
parent 1166 16555ef29e9e
child 1169 c19ac78b940e
permissions -rw-r--r--
Toggle button to enable/disable Firebug
     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.ArrayList;
    28 import java.util.Enumeration;
    29 import java.util.List;
    30 
    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;
    37 
    38 /**
    39  *
    40  * @author Jaroslav Tulach <jtulach@netbeans.org>
    41  */
    42 final class FXBrwsrLauncher extends BaseHTTPLauncher {
    43     private static final Logger LOG = Logger.getLogger(FXBrwsrLauncher.class.getName());
    44     static {
    45         try {
    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);
    53         }
    54     }
    55 
    56     public FXBrwsrLauncher(String ignore) {
    57         super(null);
    58     }
    59 
    60     @Override
    61     protected Object[] showBrwsr(final URI url) throws IOException {
    62         try {
    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() {
    67                 @Override
    68                 public void run() {
    69                     LOG.log(Level.INFO, "In FX thread. Launching!");
    70                     try {
    71                         List<String> params = new ArrayList<String>();
    72                         params.add(url.toString());
    73                         if (isDebugged()) {
    74                             params.add("--toolbar=true");
    75                             params.add("--firebug=true");
    76                         }
    77                         FXBrwsr.launch(FXBrwsr.class, params.toArray(new String[params.size()]));
    78                         LOG.log(Level.INFO, "Launcher is back. Closing");
    79                         close();
    80                         System.exit(0);
    81                     } catch (Throwable ex) {
    82                         LOG.log(Level.WARNING, "Error launching Web View", ex);
    83                     }
    84                 }
    85             });
    86         } catch (Throwable ex) {
    87             LOG.log(Level.WARNING, "Can't open WebView", ex);
    88         }
    89         return null;
    90     }
    91     
    92     @Override
    93     void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException {
    94         sb.append("(function() {\n"
    95             + "  var impl = this.bck2brwsr;\n"
    96             + "  this.bck2brwsr = function() { return impl; };\n");
    97         sb.append("})(window);\n");
    98         JVMBridge.onBck2BrwsrLoad();
    99     }
   100 
   101     @Override
   102     public void close() throws IOException {
   103         super.close();
   104         Platform.exit();
   105     }
   106 
   107     String harnessResource() {
   108         return "org/apidesign/bck2brwsr/launcher/fximpl/harness.xhtml";
   109     }
   110 
   111     public static void main(String... args) throws IOException {
   112         String startPage = null;
   113 
   114         final ClassLoader cl = FXBrwsrLauncher.class.getClassLoader();
   115         startPage = findStartPage(cl, startPage);
   116         if (startPage == null) {
   117             throw new NullPointerException("Can't find StartPage tag in manifests!");
   118         }
   119         
   120         Launcher.showURL("fxbrwsr", cl, startPage);
   121     }
   122     
   123     private static String findStartPage(final ClassLoader cl, String startPage) throws IOException {
   124         Enumeration<URL> en = cl.getResources("META-INF/MANIFEST.MF");
   125         while (en.hasMoreElements()) {
   126             URL url = en.nextElement();
   127             Manifest mf;
   128             InputStream is = null;
   129             try {
   130                 is = url.openStream();
   131                 mf = new Manifest(is);
   132             } finally {
   133                 if (is != null) is.close();
   134             }
   135             String sp = mf.getMainAttributes().getValue("StartPage");
   136             if (sp != null) {
   137                 startPage = sp;
   138                 break;
   139             }
   140         }
   141         return startPage;
   142     }
   143     
   144     private static boolean isDebugged() {
   145         try {
   146             return isDebuggedImpl();
   147         } catch (LinkageError e) {
   148             return false;
   149         }
   150     }
   151 
   152     private static boolean isDebuggedImpl() {
   153         java.lang.management.RuntimeMXBean runtime;
   154         runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();
   155         List<String> args = runtime.getInputArguments();
   156         if (args.contains("-Xdebug")) { // NOI18N
   157             return true;
   158         }
   159         return false;
   160     }
   161 }