boot/src/test/java/org/apidesign/html/boot/impl/JsClassLoaderTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 19 Jun 2013 12:52:23 +0200
branchclassloader
changeset 123 8e54b83ea65c
child 161 ea5ca9cc685d
permissions -rw-r--r--
Creating bootstraping APIs
     1 /**
     2  * HTML via Java(tm) Language Bindings
     3  * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    16  *
    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
    20  */
    21 package org.apidesign.html.boot.impl;
    22 
    23 import org.apidesign.html.boot.spi.Fn;
    24 import java.net.URL;
    25 import java.net.URLClassLoader;
    26 import java.util.ArrayList;
    27 import java.util.Arrays;
    28 import java.util.Enumeration;
    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.testng.annotations.AfterClass;
    35 import org.testng.annotations.BeforeClass;
    36 
    37 /**
    38  *
    39  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    40  */
    41 public class JsClassLoaderTest extends JsClassLoaderBase{
    42 
    43     @BeforeClass
    44     public static void setUpClass() throws Exception {
    45         ScriptEngineManager sem = new ScriptEngineManager();
    46         final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
    47         
    48         final URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
    49         ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
    50         final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
    51         JsClassLoader loader = new JsClassLoader(parent) {
    52             @Override
    53             protected URL findResource(String name) {
    54                 return ul.getResource(name);
    55             }
    56             @Override
    57             protected Fn defineFn(String code, String... names) {
    58                 StringBuilder sb = new StringBuilder();
    59                 sb.append("(function() {");
    60                 sb.append("return function(");
    61                 String sep = "";
    62                 for (String n : names) {
    63                     sb.append(sep);
    64                     sb.append(n);
    65                     sep = ", ";
    66                 }
    67                 sb.append(") {");
    68                 sb.append(code);
    69                 sb.append("};");
    70                 sb.append("})()");
    71                 try {
    72                     final Object val = eng.eval(sb.toString());
    73                     return new Fn() {
    74                         @Override
    75                         public Object invoke(Object thiz, Object... args) throws Exception {
    76                             List<Object> all = new ArrayList<Object>(args.length + 1);
    77                             all.add(thiz == null ? val : thiz);
    78                             all.addAll(Arrays.asList(args));
    79                             Invocable inv = (Invocable)eng;
    80                             Object ret = inv.invokeMethod(val, "call", all.toArray());
    81                             return ret == val ? null : ret;
    82                         }
    83                     };
    84                 } catch (ScriptException ex) {
    85                     throw new LinkageError("Can't parse: " + sb, ex);
    86                 }
    87             }
    88 
    89             @Override
    90             protected Enumeration<URL> findResources(String name) {
    91                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    92             }
    93         };
    94         
    95         methodClass = loader.loadClass(JsMethods.class.getName());
    96     }
    97 
    98     @AfterClass
    99     public static void cleanUp() {
   100         methodClass = null;
   101     }
   102 }