jaroslav@1041: /** jaroslav@1041: * Back 2 Browser Bytecode Translator jaroslav@1041: * Copyright (C) 2012 Jaroslav Tulach jaroslav@1041: * jaroslav@1041: * This program is free software: you can redistribute it and/or modify jaroslav@1041: * it under the terms of the GNU General Public License as published by jaroslav@1041: * the Free Software Foundation, version 2 of the License. jaroslav@1041: * jaroslav@1041: * This program is distributed in the hope that it will be useful, jaroslav@1041: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@1041: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@1041: * GNU General Public License for more details. jaroslav@1041: * jaroslav@1041: * You should have received a copy of the GNU General Public License jaroslav@1041: * along with this program. Look for COPYING file in the top folder. jaroslav@1041: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@1041: */ jaroslav@1041: package org.apidesign.bck2brwsr.launcher; jaroslav@1041: jaroslav@1041: import org.apidesign.bck2brwsr.launcher.fximpl.FXBrwsr; jaroslav@1041: import java.io.IOException; jaroslav@1041: import java.lang.reflect.Method; jaroslav@1041: import java.net.URI; jaroslav@1041: import java.net.URL; jaroslav@1041: import java.net.URLClassLoader; jaroslav@1041: jaroslav@1041: import java.util.concurrent.Executors; jaroslav@1041: import java.util.logging.Level; jaroslav@1041: import java.util.logging.Logger; jaroslav@1041: import javafx.application.Platform; jaroslav@1041: import org.apidesign.bck2brwsr.launcher.fximpl.JVMBridge; jaroslav@1041: jaroslav@1041: /** jaroslav@1041: * jaroslav@1041: * @author Jaroslav Tulach jaroslav@1041: */ jaroslav@1041: final class FXBrwsrLauncher extends BaseHTTPLauncher { jaroslav@1041: private static final Logger LOG = Logger.getLogger(FXBrwsrLauncher.class.getName()); jaroslav@1041: static { jaroslav@1041: try { jaroslav@1041: Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); jaroslav@1041: m.setAccessible(true); jaroslav@1041: URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar"); jaroslav@1041: LOG.log(Level.INFO, "url : {0}", l); jaroslav@1041: m.invoke(ClassLoader.getSystemClassLoader(), l); jaroslav@1041: } catch (Exception ex) { jaroslav@1041: throw new LinkageError("Can't add jfxrt.jar on the classpath", ex); jaroslav@1041: } jaroslav@1041: } jaroslav@1041: jaroslav@1041: public FXBrwsrLauncher(String ignore) { jaroslav@1041: super(null); jaroslav@1041: } jaroslav@1041: jaroslav@1041: @Override jaroslav@1041: protected Object[] showBrwsr(final URI url) throws IOException { jaroslav@1041: try { jaroslav@1041: LOG.log(Level.INFO, "showBrwsr for {0}", url); jaroslav@1041: JVMBridge.registerClassLoaders(loaders()); jaroslav@1041: LOG.info("About to launch WebView"); jaroslav@1041: Executors.newSingleThreadExecutor().submit(new Runnable() { jaroslav@1041: @Override jaroslav@1041: public void run() { jaroslav@1041: LOG.log(Level.INFO, "In FX thread. Launching!"); jaroslav@1041: try { jaroslav@1041: FXBrwsr.launch(FXBrwsr.class, url.toString()); jaroslav@1041: LOG.log(Level.INFO, "Launcher is back. Closing"); jaroslav@1041: close(); jaroslav@1041: } catch (Throwable ex) { jaroslav@1041: LOG.log(Level.WARNING, "Error launching Web View", ex); jaroslav@1041: } jaroslav@1041: } jaroslav@1041: }); jaroslav@1041: } catch (Throwable ex) { jaroslav@1041: LOG.log(Level.WARNING, "Can't open WebView", ex); jaroslav@1041: } jaroslav@1041: return null; jaroslav@1041: } jaroslav@1041: jaroslav@1041: @Override jaroslav@1043: void generateBck2BrwsrJS(StringBuilder sb, Object loader) throws IOException { jaroslav@1041: sb.append("(function() {\n" jaroslav@1041: + " var impl = this.bck2brwsr;\n" jaroslav@1041: + " this.bck2brwsr = function() { return impl; };\n" jaroslav@1041: + "})(window);\n" jaroslav@1041: ); jaroslav@1041: JVMBridge.onBck2BrwsrLoad(); jaroslav@1041: } jaroslav@1041: jaroslav@1041: jaroslav@1041: jaroslav@1041: @Override jaroslav@1041: public void close() throws IOException { jaroslav@1041: super.close(); jaroslav@1041: Platform.exit(); jaroslav@1041: } jaroslav@1041: jaroslav@1041: }