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.ArrayList;
22 import java.util.Collections;
23 import java.util.Enumeration;
24 import java.util.List;
25 import java.util.TooManyListenersException;
26 import javafx.beans.value.ChangeListener;
27 import javafx.scene.web.WebEngine;
28 import netscape.javascript.JSObject;
32 * @author Jaroslav Tulach <jtulach@netbeans.org>
34 public final class JVMBridge {
35 private final WebEngine engine;
36 private final WebClassLoader cl;
38 private static ClassLoader[] ldrs;
39 private static ChangeListener<Void> onBck2BrwsrLoad;
41 JVMBridge(WebEngine eng) {
43 this.cl = new WebClassLoader(JVMBridge.class.getClassLoader().getParent());
46 public static void registerClassLoaders(ClassLoader[] loaders) {
47 ldrs = loaders.clone();
50 public static void addBck2BrwsrLoad(ChangeListener<Void> l) throws TooManyListenersException {
51 if (onBck2BrwsrLoad != null) {
52 throw new TooManyListenersException();
57 public static void onBck2BrwsrLoad() {
58 ChangeListener<Void> l = onBck2BrwsrLoad;
60 l.changed(null, null, null);
64 public Class<?> loadClass(String name) throws ClassNotFoundException {
65 return Class.forName(name, true, cl);
68 private final class WebClassLoader extends JsClassLoader {
69 public WebClassLoader(ClassLoader parent) {
74 protected URL findResource(String name) {
75 if (ldrs != null) for (ClassLoader l : ldrs) {
76 URL u = l.getResource(name);
85 protected Enumeration<URL> findResources(String name) {
86 List<URL> arr = new ArrayList<URL>();
88 for (ClassLoader l : ldrs) {
89 URL u = l.getResource(name);
95 return Collections.enumeration(arr);
99 protected Fn defineFn(String code, String... names) {
100 StringBuilder sb = new StringBuilder();
101 sb.append("(function() {");
102 sb.append(" var x = {};");
103 sb.append(" x.fn = function(");
105 for (String n : names) {
106 sb.append(sep).append(n);
112 sb.append("return x;");
115 JSObject x = (JSObject) engine.executeScript(sb.toString());
120 private static final class JSFn extends Fn {
121 private final JSObject fn;
123 public JSFn(JSObject fn) {
128 public Object invoke(Object thiz, Object... args) throws Exception {
130 return fn.call("fn", args); // NOI18N
134 } catch (Exception t) {