2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.launcher.fximpl;
21 import java.util.TooManyListenersException;
22 import javafx.beans.value.ChangeListener;
23 import javafx.scene.web.WebEngine;
24 import netscape.javascript.JSObject;
28 * @author Jaroslav Tulach <jtulach@netbeans.org>
30 public final class JVMBridge {
31 private final WebEngine engine;
32 private final WebClassLoader cl;
34 private static ClassLoader[] ldrs;
35 private static ChangeListener<Void> onBck2BrwsrLoad;
37 JVMBridge(WebEngine eng) {
39 this.cl = new WebClassLoader(JVMBridge.class.getClassLoader());
42 public static void registerClassLoaders(ClassLoader[] loaders) {
43 ldrs = loaders.clone();
46 public static void addBck2BrwsrLoad(ChangeListener<Void> l) throws TooManyListenersException {
47 if (onBck2BrwsrLoad != null) {
48 throw new TooManyListenersException();
53 public static void onBck2BrwsrLoad() {
54 ChangeListener<Void> l = onBck2BrwsrLoad;
56 l.changed(null, null, null);
60 public Class<?> loadClass(String name) throws ClassNotFoundException {
61 return Class.forName(name, true, cl);
64 private final class WebClassLoader extends JsClassLoader {
65 public WebClassLoader(ClassLoader parent) {
70 protected URL findResource(String name) {
71 if (ldrs != null) for (ClassLoader l : ldrs) {
72 URL u = l.getResource(name);
81 protected Fn defineFn(String code, String... names) {
82 StringBuilder sb = new StringBuilder();
83 sb.append("(function() {");
84 sb.append(" var x = {};");
85 sb.append(" x.fn = function(");
87 for (String n : names) {
88 sb.append(sep).append(n);
94 sb.append("return x;");
97 JSObject x = (JSObject) engine.executeScript(sb.toString());
102 private static final class JSFn extends Fn {
103 private final JSObject fn;
105 public JSFn(JSObject fn) {
110 public Object invoke(Object... args) throws Exception {
111 return fn.call("fn", args); // NOI18N