jaroslav@123
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@123
|
3 |
*
|
jaroslav@551
|
4 |
* Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
|
jaroslav@123
|
5 |
*
|
jaroslav@358
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
jaroslav@358
|
7 |
* Other names may be trademarks of their respective owners.
|
jaroslav@123
|
8 |
*
|
jaroslav@358
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
jaroslav@358
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
jaroslav@358
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
jaroslav@358
|
12 |
* "License"). You may not use this file except in compliance with the
|
jaroslav@358
|
13 |
* License. You can obtain a copy of the License at
|
jaroslav@358
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
jaroslav@358
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jaroslav@358
|
16 |
* specific language governing permissions and limitations under the
|
jaroslav@358
|
17 |
* License. When distributing the software, include this License Header
|
jaroslav@358
|
18 |
* Notice in each file and include the License file at
|
jaroslav@358
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
jaroslav@358
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@358
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
jaroslav@358
|
22 |
* accompanied this code. If applicable, add the following below the
|
jaroslav@358
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jaroslav@358
|
24 |
* your own identifying information:
|
jaroslav@358
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jaroslav@358
|
26 |
*
|
jaroslav@358
|
27 |
* Contributor(s):
|
jaroslav@358
|
28 |
*
|
jaroslav@358
|
29 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jaroslav@551
|
30 |
* Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
|
jaroslav@358
|
31 |
*
|
jaroslav@358
|
32 |
* If you wish your version of this file to be governed by only the CDDL
|
jaroslav@358
|
33 |
* or only the GPL Version 2, indicate your decision by adding
|
jaroslav@358
|
34 |
* "[Contributor] elects to include this software in this distribution
|
jaroslav@358
|
35 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
jaroslav@358
|
36 |
* single choice of license, a recipient has the option to distribute
|
jaroslav@358
|
37 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
jaroslav@358
|
38 |
* to extend the choice of license to its licensees as provided above.
|
jaroslav@358
|
39 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
jaroslav@358
|
40 |
* Version 2 license, then the option applies only if the new code is
|
jaroslav@358
|
41 |
* made subject to such option by the copyright holder.
|
jaroslav@123
|
42 |
*/
|
jaroslav@362
|
43 |
package org.netbeans.html.boot.impl;
|
jaroslav@123
|
44 |
|
jaroslav@323
|
45 |
import java.io.Closeable;
|
jaroslav@163
|
46 |
import java.io.Reader;
|
jaroslav@123
|
47 |
import java.net.URL;
|
jaroslav@123
|
48 |
import java.net.URLClassLoader;
|
jaroslav@123
|
49 |
import java.util.ArrayList;
|
jaroslav@123
|
50 |
import java.util.Arrays;
|
jaroslav@123
|
51 |
import java.util.Collection;
|
jaroslav@123
|
52 |
import java.util.List;
|
jaroslav@123
|
53 |
import javax.script.Invocable;
|
jaroslav@123
|
54 |
import javax.script.ScriptEngine;
|
jaroslav@123
|
55 |
import javax.script.ScriptEngineManager;
|
jaroslav@123
|
56 |
import javax.script.ScriptException;
|
jaroslav@128
|
57 |
import org.apidesign.html.boot.spi.Fn;
|
jaroslav@123
|
58 |
import org.testng.annotations.BeforeClass;
|
jaroslav@288
|
59 |
import org.testng.annotations.BeforeMethod;
|
jaroslav@123
|
60 |
|
jaroslav@123
|
61 |
/**
|
jaroslav@123
|
62 |
*
|
jaroslav@123
|
63 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@123
|
64 |
*/
|
jaroslav@123
|
65 |
public class FnTest extends JsClassLoaderBase {
|
jaroslav@288
|
66 |
private static Fn.Presenter presenter;
|
jaroslav@123
|
67 |
|
jaroslav@123
|
68 |
public FnTest() {
|
jaroslav@123
|
69 |
}
|
jaroslav@123
|
70 |
|
jaroslav@123
|
71 |
@BeforeClass
|
jaroslav@123
|
72 |
public static void createClassLoader() throws Exception {
|
jaroslav@123
|
73 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@123
|
74 |
final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
|
jaroslav@123
|
75 |
|
jaroslav@123
|
76 |
final URL my = FnTest.class.getProtectionDomain().getCodeSource().getLocation();
|
jaroslav@123
|
77 |
ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
|
jaroslav@123
|
78 |
final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
|
jaroslav@123
|
79 |
|
jaroslav@128
|
80 |
class Impl implements FindResources, Fn.Presenter {
|
jaroslav@123
|
81 |
@Override
|
jaroslav@123
|
82 |
public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
|
jaroslav@123
|
83 |
URL u = ul.findResource(path);
|
jaroslav@123
|
84 |
if (u != null) {
|
jaroslav@123
|
85 |
results.add(u);
|
jaroslav@123
|
86 |
}
|
jaroslav@123
|
87 |
}
|
jaroslav@123
|
88 |
|
jaroslav@123
|
89 |
@Override
|
jaroslav@123
|
90 |
public Fn defineFn(String code, String... names) {
|
jaroslav@123
|
91 |
StringBuilder sb = new StringBuilder();
|
jaroslav@123
|
92 |
sb.append("(function() {");
|
jaroslav@123
|
93 |
sb.append("return function(");
|
jaroslav@123
|
94 |
String sep = "";
|
jaroslav@123
|
95 |
for (String n : names) {
|
jaroslav@123
|
96 |
sb.append(sep);
|
jaroslav@123
|
97 |
sb.append(n);
|
jaroslav@123
|
98 |
sep = ", ";
|
jaroslav@123
|
99 |
}
|
jaroslav@123
|
100 |
sb.append(") {");
|
jaroslav@123
|
101 |
sb.append(code);
|
jaroslav@123
|
102 |
sb.append("};");
|
jaroslav@123
|
103 |
sb.append("})()");
|
jaroslav@123
|
104 |
try {
|
jaroslav@123
|
105 |
final Object val = eng.eval(sb.toString());
|
jaroslav@288
|
106 |
return new Fn(this) {
|
jaroslav@123
|
107 |
@Override
|
jaroslav@289
|
108 |
public Object invoke(Object thiz, Object... args) throws Exception {
|
jaroslav@123
|
109 |
List<Object> all = new ArrayList<Object>(args.length + 1);
|
jaroslav@123
|
110 |
all.add(thiz == null ? val : thiz);
|
jaroslav@123
|
111 |
all.addAll(Arrays.asList(args));
|
jaroslav@123
|
112 |
Invocable inv = (Invocable)eng;
|
jaroslav@161
|
113 |
try {
|
jaroslav@161
|
114 |
Object ret = inv.invokeMethod(val, "call", all.toArray());
|
jaroslav@214
|
115 |
return val.equals(ret) ? null : ret;
|
jaroslav@161
|
116 |
} catch (ScriptException ex) {
|
jaroslav@161
|
117 |
throw ex;
|
jaroslav@161
|
118 |
}
|
jaroslav@123
|
119 |
}
|
jaroslav@123
|
120 |
};
|
jaroslav@123
|
121 |
} catch (ScriptException ex) {
|
jaroslav@123
|
122 |
throw new LinkageError("Can't parse: " + sb, ex);
|
jaroslav@123
|
123 |
}
|
jaroslav@123
|
124 |
}
|
jaroslav@127
|
125 |
|
jaroslav@127
|
126 |
@Override
|
jaroslav@128
|
127 |
public void displayPage(URL resource, Runnable r) {
|
jaroslav@127
|
128 |
throw new UnsupportedOperationException();
|
jaroslav@127
|
129 |
}
|
jaroslav@163
|
130 |
|
jaroslav@163
|
131 |
@Override
|
jaroslav@163
|
132 |
public void loadScript(Reader code) throws Exception {
|
jaroslav@163
|
133 |
eng.eval(code);
|
jaroslav@163
|
134 |
}
|
jaroslav@123
|
135 |
}
|
jaroslav@123
|
136 |
Impl impl = new Impl();
|
jaroslav@126
|
137 |
ClassLoader loader = FnUtils.newLoader(impl, impl, parent);
|
jaroslav@288
|
138 |
presenter = impl;
|
jaroslav@123
|
139 |
|
jaroslav@323
|
140 |
Closeable close = FnContext.activate(impl);
|
jaroslav@123
|
141 |
methodClass = loader.loadClass(JsMethods.class.getName());
|
jaroslav@323
|
142 |
close.close();
|
jaroslav@123
|
143 |
}
|
jaroslav@288
|
144 |
|
jaroslav@288
|
145 |
@BeforeMethod public void initPresenter() {
|
jaroslav@309
|
146 |
FnContext.currentPresenter(presenter);
|
jaroslav@288
|
147 |
}
|
jaroslav@123
|
148 |
}
|