rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 17 Apr 2013 17:14:30 +0200
branchfx
changeset 1005 512984207634
parent 1004 04efef2a9c1e
child 1016 6dc2c6c752df
permissions -rw-r--r--
More logging from the process of launching WebView
     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.impl.FXBrwsr;
    21 import java.io.IOException;
    22 import java.lang.reflect.Method;
    23 import java.net.URI;
    24 import java.net.URL;
    25 import java.net.URLClassLoader;
    26 
    27 import java.util.concurrent.Executors;
    28 import java.util.logging.Level;
    29 import java.util.logging.Logger;
    30 import javafx.application.Platform;
    31 import org.apidesign.bck2brwsr.launcher.impl.JVMBridge;
    32 import org.apidesign.vm4brwsr.Bck2Brwsr;
    33 
    34 /**
    35  *
    36  * @author Jaroslav Tulach <jtulach@netbeans.org>
    37  */
    38 final class WebViewLauncher extends Bck2BrwsrLauncher {
    39     private static final Logger LOG = Logger.getLogger(WebViewLauncher.class.getName());
    40     static {
    41         try {
    42             Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
    43             m.setAccessible(true);
    44             URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar");
    45             LOG.log(Level.INFO, "url : {0}", l);
    46             m.invoke(ClassLoader.getSystemClassLoader(), l);
    47         } catch (Exception ex) {
    48             throw new LinkageError("Can't add jfxrt.jar on the classpath", ex);
    49         }
    50     }
    51     
    52     public WebViewLauncher() {
    53         super(null);
    54     }
    55 
    56     @Override
    57     protected Object[] showBrwsr(final URI url) throws IOException {
    58         try {
    59             LOG.log(Level.INFO, "showBrwsr for {0}", url);
    60             JVMBridge.registerClassLoaders(loaders());
    61             LOG.info("About to launch WebView");
    62             Executors.newSingleThreadExecutor().submit(new Runnable() {
    63                 @Override
    64                 public void run() {
    65                     LOG.log(Level.INFO, "In FX thread. Launching!");
    66                     try {
    67                         FXBrwsr.launch(FXBrwsr.class, url.toString());
    68                         LOG.log(Level.INFO, "Launcher is back. Closing");
    69                         close();
    70                     } catch (Throwable ex) {
    71                         LOG.log(Level.WARNING, "Error launching Web View", ex);
    72                     }
    73                     System.exit(0);
    74                 }
    75             });
    76         } catch (Throwable ex) {
    77             LOG.log(Level.WARNING, "Can't open WebView", ex);
    78         }
    79         return null;
    80     }
    81     
    82     @Override
    83     void generateBck2BrwsrJS(StringBuilder sb, Bck2Brwsr.Resources loader) throws IOException {
    84         sb.append("(function() {\n"
    85             + "  var impl = this.bck2brwsr;\n"
    86             + "  this.bck2brwsr = function() { return impl; };\n"
    87             + "})(window);\n"
    88         );
    89         JVMBridge.onBck2BrwsrLoad();
    90     }
    91     
    92     
    93     
    94     @Override
    95     public void close() throws IOException {
    96         super.close();
    97         Platform.exit();
    98     }
    99     
   100 }