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