launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JVMBridge.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 17 Jun 2013 19:55:31 +0200
branchclassloader
changeset 1183 29302b4c0776
parent 1181 b703d9d71f25
child 1184 ccf2447021f6
permissions -rw-r--r--
Using the same trick with call also inside WebView
jaroslav@1041
     1
/**
jaroslav@1041
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1041
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1041
     4
 *
jaroslav@1041
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1041
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1041
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1041
     8
 *
jaroslav@1041
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1041
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1041
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1041
    12
 * GNU General Public License for more details.
jaroslav@1041
    13
 *
jaroslav@1041
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1041
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1041
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1041
    17
 */
jaroslav@1041
    18
package org.apidesign.bck2brwsr.launcher.fximpl;
jaroslav@1041
    19
jaroslav@1174
    20
import java.net.URL;
jaroslav@1175
    21
import java.util.ArrayList;
jaroslav@1183
    22
import java.util.Arrays;
jaroslav@1175
    23
import java.util.Collections;
jaroslav@1175
    24
import java.util.Enumeration;
jaroslav@1175
    25
import java.util.List;
jaroslav@1041
    26
import java.util.TooManyListenersException;
jaroslav@1041
    27
import javafx.beans.value.ChangeListener;
jaroslav@1174
    28
import javafx.scene.web.WebEngine;
jaroslav@1183
    29
import javax.script.Invocable;
jaroslav@1174
    30
import netscape.javascript.JSObject;
jaroslav@1041
    31
jaroslav@1041
    32
/**
jaroslav@1041
    33
 *
jaroslav@1041
    34
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@1041
    35
 */
jaroslav@1041
    36
public final class JVMBridge {
jaroslav@1174
    37
    private final WebEngine engine;
jaroslav@1174
    38
    private final WebClassLoader cl;
jaroslav@1174
    39
    
jaroslav@1041
    40
    private static ClassLoader[] ldrs;
jaroslav@1041
    41
    private static ChangeListener<Void> onBck2BrwsrLoad;
jaroslav@1174
    42
    
jaroslav@1174
    43
    JVMBridge(WebEngine eng) {
jaroslav@1174
    44
        this.engine = eng;
jaroslav@1179
    45
        this.cl = new WebClassLoader(JVMBridge.class.getClassLoader().getParent());
jaroslav@1174
    46
    }
jaroslav@1041
    47
        
jaroslav@1041
    48
    public static void registerClassLoaders(ClassLoader[] loaders) {
jaroslav@1041
    49
        ldrs = loaders.clone();
jaroslav@1041
    50
    }
jaroslav@1041
    51
    
jaroslav@1041
    52
    public static void addBck2BrwsrLoad(ChangeListener<Void> l) throws TooManyListenersException {
jaroslav@1041
    53
        if (onBck2BrwsrLoad != null) {
jaroslav@1041
    54
            throw new TooManyListenersException();
jaroslav@1041
    55
        }
jaroslav@1041
    56
        onBck2BrwsrLoad = l;
jaroslav@1041
    57
    }
jaroslav@1041
    58
jaroslav@1041
    59
    public static void onBck2BrwsrLoad() {
jaroslav@1041
    60
        ChangeListener<Void> l = onBck2BrwsrLoad;
jaroslav@1041
    61
        if (l != null) {
jaroslav@1041
    62
            l.changed(null, null, null);
jaroslav@1041
    63
        }
jaroslav@1041
    64
    }
jaroslav@1041
    65
    
jaroslav@1041
    66
    public Class<?> loadClass(String name) throws ClassNotFoundException {
jaroslav@1174
    67
        return Class.forName(name, true, cl);
jaroslav@1174
    68
    }
jaroslav@1174
    69
    
jaroslav@1174
    70
    private final class WebClassLoader extends JsClassLoader {
jaroslav@1174
    71
        public WebClassLoader(ClassLoader parent) {
jaroslav@1174
    72
            super(parent);
jaroslav@1174
    73
        }
jaroslav@1174
    74
jaroslav@1174
    75
        @Override
jaroslav@1174
    76
        protected URL findResource(String name) {
jaroslav@1174
    77
            if (ldrs != null) for (ClassLoader l : ldrs) {
jaroslav@1174
    78
                URL u = l.getResource(name);
jaroslav@1174
    79
                if (u != null) {
jaroslav@1174
    80
                    return u;
jaroslav@1174
    81
                }
jaroslav@1041
    82
            }
jaroslav@1174
    83
            return null;
jaroslav@1041
    84
        }
jaroslav@1175
    85
        
jaroslav@1175
    86
        @Override
jaroslav@1175
    87
        protected Enumeration<URL> findResources(String name) {
jaroslav@1175
    88
            List<URL> arr = new ArrayList<URL>();
jaroslav@1175
    89
            if (ldrs != null) {
jaroslav@1175
    90
                for (ClassLoader l : ldrs) {
jaroslav@1175
    91
                    URL u = l.getResource(name);
jaroslav@1175
    92
                    if (u != null) {
jaroslav@1175
    93
                        arr.add(u);
jaroslav@1175
    94
                    }
jaroslav@1175
    95
                }
jaroslav@1175
    96
            }
jaroslav@1175
    97
            return Collections.enumeration(arr);
jaroslav@1175
    98
        }
jaroslav@1174
    99
jaroslav@1174
   100
        @Override
jaroslav@1174
   101
        protected Fn defineFn(String code, String... names) {
jaroslav@1174
   102
            StringBuilder sb = new StringBuilder();
jaroslav@1174
   103
            sb.append("(function() {");
jaroslav@1183
   104
            sb.append("  return function(");
jaroslav@1174
   105
            String sep = "";
jaroslav@1174
   106
            for (String n : names) {
jaroslav@1174
   107
                sb.append(sep).append(n);
jaroslav@1174
   108
                sep = ",";
jaroslav@1174
   109
            }
jaroslav@1174
   110
            sb.append(") {\n");
jaroslav@1174
   111
            sb.append(code);
jaroslav@1174
   112
            sb.append("};");
jaroslav@1174
   113
            sb.append("})()");
jaroslav@1174
   114
            
jaroslav@1174
   115
            JSObject x = (JSObject) engine.executeScript(sb.toString());
jaroslav@1174
   116
            return new JSFn(x);
jaroslav@1041
   117
        }
jaroslav@1174
   118
    }
jaroslav@1174
   119
    
jaroslav@1174
   120
    private static final class JSFn extends Fn {
jaroslav@1174
   121
        private final JSObject fn;
jaroslav@1174
   122
jaroslav@1174
   123
        public JSFn(JSObject fn) {
jaroslav@1174
   124
            this.fn = fn;
jaroslav@1174
   125
        }
jaroslav@1174
   126
        
jaroslav@1174
   127
        @Override
jaroslav@1181
   128
        public Object invoke(Object thiz, Object... args) throws Exception {
jaroslav@1179
   129
            try {
jaroslav@1183
   130
                List<Object> all = new ArrayList<Object>(args.length + 1);
jaroslav@1183
   131
                all.add(thiz == null ? fn : thiz);
jaroslav@1183
   132
                all.addAll(Arrays.asList(args));
jaroslav@1183
   133
                Object ret = fn.call("call", all.toArray()); // NOI18N
jaroslav@1183
   134
                return ret == fn ? null : ret;
jaroslav@1179
   135
            } catch (Error t) {
jaroslav@1179
   136
                t.printStackTrace();
jaroslav@1179
   137
                throw t;
jaroslav@1179
   138
            } catch (Exception t) {
jaroslav@1179
   139
                t.printStackTrace();
jaroslav@1179
   140
                throw t;
jaroslav@1179
   141
            }
jaroslav@1174
   142
        }
jaroslav@1041
   143
    }
jaroslav@1041
   144
}