rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/WebViewLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 17 Apr 2013 17:04:40 +0200
branchfx
changeset 1004 04efef2a9c1e
parent 855 b11992f928c7
child 1005 512984207634
permissions -rw-r--r--
Rather than piggybacking on first alert call, use the fact that the server and FX Web View are in the same VM and notify the view that bck2brwsr.js is about to be served from the server.
jaroslav@844
     1
/**
jaroslav@844
     2
 * Back 2 Browser Bytecode Translator
jaroslav@844
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@844
     4
 *
jaroslav@844
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@844
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@844
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@844
     8
 *
jaroslav@844
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@844
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@844
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@844
    12
 * GNU General Public License for more details.
jaroslav@844
    13
 *
jaroslav@844
    14
 * You should have received a copy of the GNU General Public License
jaroslav@844
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@844
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@844
    17
 */
jaroslav@844
    18
package org.apidesign.bck2brwsr.launcher;
jaroslav@844
    19
jaroslav@845
    20
import org.apidesign.bck2brwsr.launcher.impl.FXBrwsr;
jaroslav@844
    21
import java.io.IOException;
jaroslav@844
    22
import java.lang.reflect.Method;
jaroslav@844
    23
import java.net.URI;
jaroslav@844
    24
import java.net.URL;
jaroslav@844
    25
import java.net.URLClassLoader;
jaroslav@844
    26
jaroslav@844
    27
import java.util.concurrent.Executors;
jaroslav@845
    28
import java.util.logging.Level;
jaroslav@845
    29
import java.util.logging.Logger;
jaroslav@844
    30
import javafx.application.Platform;
jaroslav@845
    31
import org.apidesign.bck2brwsr.launcher.impl.JVMBridge;
jaroslav@845
    32
import org.apidesign.vm4brwsr.Bck2Brwsr;
jaroslav@844
    33
jaroslav@844
    34
/**
jaroslav@844
    35
 *
jaroslav@844
    36
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@844
    37
 */
jaroslav@844
    38
final class WebViewLauncher extends Bck2BrwsrLauncher {
jaroslav@845
    39
    private static final Logger LOG = Logger.getLogger(WebViewLauncher.class.getName());
jaroslav@844
    40
    static {
jaroslav@844
    41
        try {
jaroslav@844
    42
            Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
jaroslav@844
    43
            m.setAccessible(true);
jaroslav@844
    44
            URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar");
jaroslav@845
    45
            LOG.log(Level.INFO, "url : {0}", l);
jaroslav@844
    46
            m.invoke(ClassLoader.getSystemClassLoader(), l);
jaroslav@844
    47
        } catch (Exception ex) {
jaroslav@844
    48
            throw new LinkageError("Can't add jfxrt.jar on the classpath", ex);
jaroslav@844
    49
        }
jaroslav@844
    50
    }
jaroslav@844
    51
    
jaroslav@844
    52
    public WebViewLauncher() {
jaroslav@844
    53
        super(null);
jaroslav@844
    54
    }
jaroslav@844
    55
jaroslav@844
    56
    @Override
jaroslav@844
    57
    protected Object[] showBrwsr(final URI url) throws IOException {
jaroslav@844
    58
        try {
jaroslav@845
    59
            JVMBridge.registerClassLoaders(loaders());
jaroslav@844
    60
            Executors.newSingleThreadExecutor().submit(new Runnable() {
jaroslav@844
    61
                @Override
jaroslav@844
    62
                public void run() {
jaroslav@845
    63
                    FXBrwsr.launch(FXBrwsr.class, url.toString());
jaroslav@855
    64
                    LOG.log(Level.FINE, "Launcher is back!");
jaroslav@855
    65
                    try {
jaroslav@855
    66
                        close();
jaroslav@855
    67
                    } catch (IOException ex) {
jaroslav@855
    68
                        LOG.log(Level.WARNING, null, ex);
jaroslav@855
    69
                    }
jaroslav@855
    70
                    System.exit(0);
jaroslav@844
    71
                }
jaroslav@844
    72
            });
jaroslav@844
    73
        } catch (Throwable ex) {
jaroslav@845
    74
            LOG.log(Level.WARNING, "Can't open WebView", ex);
jaroslav@844
    75
        }
jaroslav@844
    76
        return null;
jaroslav@844
    77
    }
jaroslav@845
    78
    
jaroslav@845
    79
    @Override
jaroslav@845
    80
    void generateBck2BrwsrJS(StringBuilder sb, Bck2Brwsr.Resources loader) throws IOException {
jaroslav@845
    81
        sb.append("(function() {\n"
jaroslav@853
    82
            + "  var impl = this.bck2brwsr;\n"
jaroslav@853
    83
            + "  this.bck2brwsr = function() { return impl; };\n"
jaroslav@845
    84
            + "})(window);\n"
jaroslav@845
    85
        );
jaroslav@1004
    86
        JVMBridge.onBck2BrwsrLoad();
jaroslav@845
    87
    }
jaroslav@845
    88
    
jaroslav@845
    89
    
jaroslav@845
    90
    
jaroslav@844
    91
    @Override
jaroslav@844
    92
    public void close() throws IOException {
jaroslav@855
    93
        super.close();
jaroslav@844
    94
        Platform.exit();
jaroslav@844
    95
    }
jaroslav@844
    96
    
jaroslav@844
    97
}