launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/FXBrwsrLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 09 Sep 2013 17:34:30 +0200
changeset 1273 37ad459579bc
parent 1169 c19ac78b940e
child 1513 ba912ef24b27
permissions -rw-r--r--
Rewritting the archetype to be simpler and more easily modifiable to one's needs
     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 import org.openide.util.Exceptions;
    41 
    42 /**
    43  *
    44  * @author Jaroslav Tulach <jtulach@netbeans.org>
    45  */
    46 final class FXBrwsrLauncher extends BaseHTTPLauncher {
    47     private static final Logger LOG = Logger.getLogger(FXBrwsrLauncher.class.getName());
    48     static {
    49         try {
    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);
    57         }
    58     }
    59 
    60     public FXBrwsrLauncher(String ignore) {
    61         super(null);
    62     }
    63 
    64     @Override
    65     protected Object[] showBrwsr(final URI url) throws IOException {
    66         try {
    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() {
    71                 @Override
    72                 public void run() {
    73                     LOG.log(Level.INFO, "In FX thread. Launching!");
    74                     try {
    75                         List<String> params = new ArrayList<String>();
    76                         params.add(url.toString());
    77                         if (isDebugged()) {
    78                             params.add("--toolbar=true");
    79                             params.add("--firebug=true");
    80                             String ud = System.getProperty("netbeans.user");
    81                             if (ud != null) {
    82                                 params.add("--userdir=" + ud);
    83                             }
    84                         }
    85                         FXBrwsr.launch(FXBrwsr.class, params.toArray(new String[params.size()]));
    86                         LOG.log(Level.INFO, "Launcher is back. Closing");
    87                         close();
    88                         System.exit(0);
    89                     } catch (Throwable ex) {
    90                         LOG.log(Level.WARNING, "Error launching Web View", ex);
    91                     }
    92                 }
    93             });
    94         } catch (Throwable ex) {
    95             LOG.log(Level.WARNING, "Can't open WebView", ex);
    96         }
    97         return null;
    98     }
    99     
   100     @Override
   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();
   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         URL[] manifestURL = { null };
   124         startPage = findStartPage(cl, startPage, manifestURL);
   125         if (startPage == null) {
   126             throw new NullPointerException("Can't find StartPage tag in manifests!");
   127         }
   128         
   129         File dir = new File(".");
   130         if (manifestURL[0].getProtocol().equals("jar")) {
   131             try {
   132                 dir = new File(
   133                     ((JarURLConnection)manifestURL[0].openConnection()).getJarFileURL().toURI()
   134                 ).getParentFile();
   135             } catch (URISyntaxException ex) {
   136                 LOG.log(Level.WARNING, "Can't find root directory", ex);
   137             }
   138         }
   139         
   140         Launcher.showDir("fxbrwsr", dir, cl, startPage);
   141     }
   142     
   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();
   149             Manifest mf;
   150             InputStream is = null;
   151             try {
   152                 is = url.openStream();
   153                 mf = new Manifest(is);
   154             } finally {
   155                 if (is != null) is.close();
   156             }
   157             String sp = mf.getMainAttributes().getValue("StartPage");
   158             if (sp != null) {
   159                 startPage = sp;
   160                 if (startURL != null) {
   161                     startURL[0] = url;
   162                 }
   163                 break;
   164             }
   165         }
   166         return startPage;
   167     }
   168     
   169     private static boolean isDebugged() {
   170         try {
   171             return isDebuggedImpl();
   172         } catch (LinkageError e) {
   173             return false;
   174         }
   175     }
   176 
   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
   182             return true;
   183         }
   184         return false;
   185     }
   186 }