boot/src/test/java/org/apidesign/html/boot/impl/JsClassLoaderTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 29 Jul 2013 17:49:48 +0200
changeset 214 19d7f018defd
parent 163 2652760705d6
child 288 8c5b40231d26
permissions -rw-r--r--
Compile and pass on JDK8
     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 java.io.Reader;
    24 import org.apidesign.html.boot.spi.Fn;
    25 import java.net.URL;
    26 import java.net.URLClassLoader;
    27 import java.util.ArrayList;
    28 import java.util.Arrays;
    29 import java.util.Enumeration;
    30 import java.util.List;
    31 import javax.script.Invocable;
    32 import javax.script.ScriptEngine;
    33 import javax.script.ScriptEngineManager;
    34 import javax.script.ScriptException;
    35 import org.testng.annotations.AfterClass;
    36 import org.testng.annotations.BeforeClass;
    37 
    38 /**
    39  *
    40  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    41  */
    42 public class JsClassLoaderTest extends JsClassLoaderBase{
    43 
    44     @BeforeClass
    45     public static void setUpClass() throws Exception {
    46         ScriptEngineManager sem = new ScriptEngineManager();
    47         final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
    48         
    49         final URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
    50         ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
    51         final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
    52         JsClassLoader loader = new JsClassLoader(parent) {
    53             @Override
    54             protected URL findResource(String name) {
    55                 return ul.getResource(name);
    56             }
    57             @Override
    58             protected Fn defineFn(String code, String... names) {
    59                 StringBuilder sb = new StringBuilder();
    60                 sb.append("(function() {");
    61                 sb.append("return function(");
    62                 String sep = "";
    63                 for (String n : names) {
    64                     sb.append(sep);
    65                     sb.append(n);
    66                     sep = ", ";
    67                 }
    68                 sb.append(") {");
    69                 sb.append(code);
    70                 sb.append("};");
    71                 sb.append("})()");
    72                 try {
    73                     final Object val = eng.eval(sb.toString());
    74                     return new Fn() {
    75                         @Override
    76                         public Object invoke(Object thiz, Object... args) throws Exception {
    77                             List<Object> all = new ArrayList<Object>(args.length + 1);
    78                             all.add(thiz == null ? val : thiz);
    79                             all.addAll(Arrays.asList(args));
    80                             Invocable inv = (Invocable)eng;
    81                             try {
    82                                 Object ret = inv.invokeMethod(val, "call", all.toArray());
    83                                 return val.equals(ret) ? null : ret;
    84                             } catch (Exception ex) {
    85                                 throw ex;
    86                             }
    87                         }
    88                     };
    89                 } catch (ScriptException ex) {
    90                     throw new LinkageError("Can't parse: " + sb, ex);
    91                 }
    92             }
    93 
    94             @Override
    95             protected Enumeration<URL> findResources(String name) {
    96                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    97             }
    98 
    99             @Override
   100             protected void loadScript(Reader code) throws ScriptException {
   101                 eng.eval(code);
   102             }
   103         };
   104         
   105         methodClass = loader.loadClass(JsMethods.class.getName());
   106     }
   107 
   108     @AfterClass
   109     public static void cleanUp() {
   110         methodClass = null;
   111     }
   112 }