launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/FXBrwsrLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 30 Apr 2014 15:04:10 +0200
branchclosure
changeset 1513 ba912ef24b27
parent 1273 37ad459579bc
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Merging from default branch and resolving conflicts. mvn install -DskipTests passes OK.
     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 java.io.File;
    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;
    26 import java.net.URI;
    27 import java.net.URISyntaxException;
    28 import java.net.URL;
    29 import java.net.URLClassLoader;
    30 import java.util.ArrayList;
    31 import java.util.Enumeration;
    32 import java.util.List;
    33 
    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 
    41 /**
    42  *
    43  * @author Jaroslav Tulach <jtulach@netbeans.org>
    44  */
    45 final class FXBrwsrLauncher extends BaseHTTPLauncher {
    46     private static final Logger LOG = Logger.getLogger(FXBrwsrLauncher.class.getName());
    47     static {
    48         try {
    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);
    56         }
    57     }
    58 
    59     public FXBrwsrLauncher(String ignore) {
    60         super(null);
    61     }
    62 
    63     @Override
    64     protected Object[] showBrwsr(final URI url) throws IOException {
    65         try {
    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() {
    70                 @Override
    71                 public void run() {
    72                     LOG.log(Level.INFO, "In FX thread. Launching!");
    73                     try {
    74                         List<String> params = new ArrayList<String>();
    75                         params.add(url.toString());
    76                         if (isDebugged()) {
    77                             params.add("--toolbar=true");
    78                             params.add("--firebug=true");
    79                             String ud = System.getProperty("netbeans.user");
    80                             if (ud != null) {
    81                                 params.add("--userdir=" + ud);
    82                             }
    83                         }
    84                         FXBrwsr.launch(FXBrwsr.class, params.toArray(new String[params.size()]));
    85                         LOG.log(Level.INFO, "Launcher is back. Closing");
    86                         close();
    87                         System.exit(0);
    88                     } catch (Throwable ex) {
    89                         LOG.log(Level.WARNING, "Error launching Web View", ex);
    90                     }
    91                 }
    92             });
    93         } catch (Throwable ex) {
    94             LOG.log(Level.WARNING, "Can't open WebView", ex);
    95         }
    96         return null;
    97     }
    98     
    99     @Override
   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();
   106     }
   107 
   108     @Override
   109     public void close() throws IOException {
   110         super.close();
   111         Platform.exit();
   112     }
   113 
   114     String harnessResource() {
   115         return "org/apidesign/bck2brwsr/launcher/fximpl/harness.xhtml";
   116     }
   117 
   118     public static void main(String... args) throws IOException {
   119         String startPage = null;
   120 
   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!");
   126         }
   127         
   128         File dir = new File(".");
   129         if (manifestURL[0].getProtocol().equals("jar")) {
   130             try {
   131                 dir = new File(
   132                     ((JarURLConnection)manifestURL[0].openConnection()).getJarFileURL().toURI()
   133                 ).getParentFile();
   134             } catch (URISyntaxException ex) {
   135                 LOG.log(Level.WARNING, "Can't find root directory", ex);
   136             }
   137         }
   138         
   139         Launcher.showDir("fxbrwsr", dir, cl, startPage);
   140     }
   141     
   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();
   148             Manifest mf;
   149             InputStream is = null;
   150             try {
   151                 is = url.openStream();
   152                 mf = new Manifest(is);
   153             } finally {
   154                 if (is != null) is.close();
   155             }
   156             String sp = mf.getMainAttributes().getValue("StartPage");
   157             if (sp != null) {
   158                 startPage = sp;
   159                 if (startURL != null) {
   160                     startURL[0] = url;
   161                 }
   162                 break;
   163             }
   164         }
   165         return startPage;
   166     }
   167     
   168     private static boolean isDebugged() {
   169         try {
   170             return isDebuggedImpl();
   171         } catch (LinkageError e) {
   172             return false;
   173         }
   174     }
   175 
   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
   181             return true;
   182         }
   183         return false;
   184     }
   185 }