2 * HTML via Java(tm) Language Bindings
3 * Copyright (C) 2013 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. apidesign.org
13 * designates this particular file as subject to the
14 * "Classpath" exception as provided by apidesign.org
15 * in the License file that accompanied this code.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. Look for COPYING file in the top folder.
19 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
22 package org.apidesign.html.boot.impl;
25 import java.net.URLClassLoader;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.List;
30 import javax.script.Invocable;
31 import javax.script.ScriptEngine;
32 import javax.script.ScriptEngineManager;
33 import javax.script.ScriptException;
34 import org.apidesign.html.boot.spi.Fn;
35 import org.testng.annotations.BeforeClass;
39 * @author Jaroslav Tulach <jtulach@netbeans.org>
41 public class FnTest extends JsClassLoaderBase {
47 public static void createClassLoader() throws Exception {
48 ScriptEngineManager sem = new ScriptEngineManager();
49 final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
51 final URL my = FnTest.class.getProtectionDomain().getCodeSource().getLocation();
52 ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
53 final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
55 class Impl implements FindResources, Fn.Presenter {
57 public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
58 URL u = ul.findResource(path);
65 public Fn defineFn(String code, String... names) {
66 StringBuilder sb = new StringBuilder();
67 sb.append("(function() {");
68 sb.append("return function(");
70 for (String n : names) {
80 final Object val = eng.eval(sb.toString());
83 public Object invoke(Object thiz, Object... args) throws Exception {
84 List<Object> all = new ArrayList<Object>(args.length + 1);
85 all.add(thiz == null ? val : thiz);
86 all.addAll(Arrays.asList(args));
87 Invocable inv = (Invocable)eng;
89 Object ret = inv.invokeMethod(val, "call", all.toArray());
90 return ret == val ? null : ret;
91 } catch (ScriptException ex) {
96 } catch (ScriptException ex) {
97 throw new LinkageError("Can't parse: " + sb, ex);
102 public void displayPage(URL resource, Runnable r) {
103 throw new UnsupportedOperationException();
106 Impl impl = new Impl();
107 ClassLoader loader = FnUtils.newLoader(impl, impl, parent);
110 methodClass = loader.loadClass(JsMethods.class.getName());