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.Arrays;
23 import java.util.Collections;
24 import java.util.Enumeration;
25 import java.util.List;
26 import java.util.TooManyListenersException;
27 import javafx.beans.value.ChangeListener;
28 import javafx.scene.web.WebEngine;
29 import javax.script.Invocable;
30 import netscape.javascript.JSObject;
34 * @author Jaroslav Tulach <jtulach@netbeans.org>
36 public final class JVMBridge {
37 private final WebEngine engine;
38 private final WebClassLoader cl;
40 private static ClassLoader[] ldrs;
41 private static ChangeListener<Void> onBck2BrwsrLoad;
43 JVMBridge(WebEngine eng) {
45 this.cl = new WebClassLoader(JVMBridge.class.getClassLoader().getParent());
48 public static void registerClassLoaders(ClassLoader[] loaders) {
49 ldrs = loaders.clone();
52 public static void addBck2BrwsrLoad(ChangeListener<Void> l) throws TooManyListenersException {
53 if (onBck2BrwsrLoad != null) {
54 throw new TooManyListenersException();
59 public static void onBck2BrwsrLoad() {
60 ChangeListener<Void> l = onBck2BrwsrLoad;
62 l.changed(null, null, null);
66 public Class<?> loadClass(String name) throws ClassNotFoundException {
67 return Class.forName(name, true, cl);
70 private final class WebClassLoader extends JsClassLoader {
71 public WebClassLoader(ClassLoader parent) {
76 protected URL findResource(String name) {
77 if (ldrs != null) for (ClassLoader l : ldrs) {
78 URL u = l.getResource(name);
87 protected Enumeration<URL> findResources(String name) {
88 List<URL> arr = new ArrayList<URL>();
90 for (ClassLoader l : ldrs) {
91 URL u = l.getResource(name);
97 return Collections.enumeration(arr);
101 protected Fn defineFn(String code, String... names) {
102 StringBuilder sb = new StringBuilder();
103 sb.append("(function() {");
104 sb.append(" return function(");
106 for (String n : names) {
107 sb.append(sep).append(n);
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 List<Object> all = new ArrayList<Object>(args.length + 1);
131 all.add(thiz == null ? fn : thiz);
132 all.addAll(Arrays.asList(args));
133 Object ret = fn.call("call", all.toArray()); // NOI18N
134 return ret == fn ? null : ret;
138 } catch (Exception t) {