launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/FXBrwsrLauncher.java
branchmodel
changeset 1041 f18b7262fe91
child 1043 bd80952bfd11
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/FXBrwsrLauncher.java	Sun Apr 28 17:42:49 2013 +0200
     1.3 @@ -0,0 +1,99 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.launcher;
    1.22 +
    1.23 +import org.apidesign.bck2brwsr.launcher.fximpl.FXBrwsr;
    1.24 +import java.io.IOException;
    1.25 +import java.lang.reflect.Method;
    1.26 +import java.net.URI;
    1.27 +import java.net.URL;
    1.28 +import java.net.URLClassLoader;
    1.29 +
    1.30 +import java.util.concurrent.Executors;
    1.31 +import java.util.logging.Level;
    1.32 +import java.util.logging.Logger;
    1.33 +import javafx.application.Platform;
    1.34 +import org.apidesign.bck2brwsr.launcher.fximpl.JVMBridge;
    1.35 +import org.apidesign.vm4brwsr.Bck2Brwsr;
    1.36 +
    1.37 +/**
    1.38 + *
    1.39 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.40 + */
    1.41 +final class FXBrwsrLauncher extends BaseHTTPLauncher {
    1.42 +    private static final Logger LOG = Logger.getLogger(FXBrwsrLauncher.class.getName());
    1.43 +    static {
    1.44 +        try {
    1.45 +            Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
    1.46 +            m.setAccessible(true);
    1.47 +            URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar");
    1.48 +            LOG.log(Level.INFO, "url : {0}", l);
    1.49 +            m.invoke(ClassLoader.getSystemClassLoader(), l);
    1.50 +        } catch (Exception ex) {
    1.51 +            throw new LinkageError("Can't add jfxrt.jar on the classpath", ex);
    1.52 +        }
    1.53 +    }
    1.54 +    
    1.55 +    public FXBrwsrLauncher(String ignore) {
    1.56 +        super(null);
    1.57 +    }
    1.58 +
    1.59 +    @Override
    1.60 +    protected Object[] showBrwsr(final URI url) throws IOException {
    1.61 +        try {
    1.62 +            LOG.log(Level.INFO, "showBrwsr for {0}", url);
    1.63 +            JVMBridge.registerClassLoaders(loaders());
    1.64 +            LOG.info("About to launch WebView");
    1.65 +            Executors.newSingleThreadExecutor().submit(new Runnable() {
    1.66 +                @Override
    1.67 +                public void run() {
    1.68 +                    LOG.log(Level.INFO, "In FX thread. Launching!");
    1.69 +                    try {
    1.70 +                        FXBrwsr.launch(FXBrwsr.class, url.toString());
    1.71 +                        LOG.log(Level.INFO, "Launcher is back. Closing");
    1.72 +                        close();
    1.73 +                    } catch (Throwable ex) {
    1.74 +                        LOG.log(Level.WARNING, "Error launching Web View", ex);
    1.75 +                    }
    1.76 +                }
    1.77 +            });
    1.78 +        } catch (Throwable ex) {
    1.79 +            LOG.log(Level.WARNING, "Can't open WebView", ex);
    1.80 +        }
    1.81 +        return null;
    1.82 +    }
    1.83 +    
    1.84 +    @Override
    1.85 +    void generateBck2BrwsrJS(StringBuilder sb, Bck2Brwsr.Resources loader) throws IOException {
    1.86 +        sb.append("(function() {\n"
    1.87 +            + "  var impl = this.bck2brwsr;\n"
    1.88 +            + "  this.bck2brwsr = function() { return impl; };\n"
    1.89 +            + "})(window);\n"
    1.90 +        );
    1.91 +        JVMBridge.onBck2BrwsrLoad();
    1.92 +    }
    1.93 +    
    1.94 +    
    1.95 +    
    1.96 +    @Override
    1.97 +    public void close() throws IOException {
    1.98 +        super.close();
    1.99 +        Platform.exit();
   1.100 +    }
   1.101 +    
   1.102 +}