launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/FXBrwsrLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 09 May 2013 21:51:56 +0200
changeset 1088 4b65abc39565
parent 1053 f44a970bbc2f
child 1095 c06cedde886d
permissions -rw-r--r--
Sharing BaseHTTPLauncher between FX and Bck2Brwsr
     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 
    29 import java.util.concurrent.Executors;
    30 import java.util.jar.Manifest;
    31 import java.util.logging.Level;
    32 import java.util.logging.Logger;
    33 import javafx.application.Platform;
    34 import org.apidesign.bck2brwsr.launcher.fximpl.JVMBridge;
    35 
    36 /**
    37  *
    38  * @author Jaroslav Tulach <jtulach@netbeans.org>
    39  */
    40 final class FXBrwsrLauncher extends BaseHTTPLauncher {
    41     private static final Logger LOG = Logger.getLogger(FXBrwsrLauncher.class.getName());
    42     static {
    43         try {
    44             Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
    45             m.setAccessible(true);
    46             URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar");
    47             LOG.log(Level.INFO, "url : {0}", l);
    48             m.invoke(ClassLoader.getSystemClassLoader(), l);
    49         } catch (Exception ex) {
    50             throw new LinkageError("Can't add jfxrt.jar on the classpath", ex);
    51         }
    52     }
    53 
    54     public FXBrwsrLauncher(String ignore) {
    55         super(null);
    56     }
    57 
    58     @Override
    59     protected Object[] showBrwsr(final URI url) throws IOException {
    60         try {
    61             LOG.log(Level.INFO, "showBrwsr for {0}", url);
    62             JVMBridge.registerClassLoaders(loaders());
    63             LOG.info("About to launch WebView");
    64             Executors.newSingleThreadExecutor().submit(new Runnable() {
    65                 @Override
    66                 public void run() {
    67                     LOG.log(Level.INFO, "In FX thread. Launching!");
    68                     try {
    69                         FXBrwsr.launch(FXBrwsr.class, url.toString());
    70                         LOG.log(Level.INFO, "Launcher is back. Closing");
    71                         close();
    72                     } catch (Throwable ex) {
    73                         LOG.log(Level.WARNING, "Error launching Web View", ex);
    74                     }
    75                 }
    76             });
    77         } catch (Throwable ex) {
    78             LOG.log(Level.WARNING, "Can't open WebView", ex);
    79         }
    80         return null;
    81     }
    82     
    83     @Override
    84     void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException {
    85         sb.append("(function() {\n"
    86             + "  var impl = this.bck2brwsr;\n"
    87             + "  this.bck2brwsr = function() { return impl; };\n"
    88             + "})(window);\n"
    89         );
    90         JVMBridge.onBck2BrwsrLoad();
    91     }
    92     
    93     
    94     
    95     @Override
    96     public void close() throws IOException {
    97         super.close();
    98         Platform.exit();
    99     }
   100 
   101     String harnessResource() {
   102         return "org/apidesign/bck2brwsr/launcher/fximpl/harness.xhtml";
   103     }
   104 
   105     public static void main(String... args) throws IOException {
   106         String startPage = null;
   107 
   108         final ClassLoader cl = FXBrwsrLauncher.class.getClassLoader();
   109         startPage = findStartPage(cl, startPage);
   110         if (startPage == null) {
   111             throw new NullPointerException("Can't find StartPage tag in manifests!");
   112         }
   113         
   114         Launcher.showURL("fxbrwsr", cl, startPage);
   115     }
   116     
   117     private static String findStartPage(final ClassLoader cl, String startPage) throws IOException {
   118         Enumeration<URL> en = cl.getResources("META-INF/MANIFEST.MF");
   119         while (en.hasMoreElements()) {
   120             URL url = en.nextElement();
   121             Manifest mf;
   122             try (InputStream is = url.openStream()) {
   123                 mf = new Manifest(is);
   124             }
   125             String sp = mf.getMainAttributes().getValue("StartPage");
   126             if (sp != null) {
   127                 startPage = sp;
   128                 break;
   129             }
   130         }
   131         return startPage;
   132     }
   133 }