boot/src/test/java/org/apidesign/html/boot/impl/JsClassLoaderTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 06 Nov 2013 15:15:54 +0100
changeset 323 86aabecda7a3
parent 309 7025177bd67e
child 358 80702021b851
permissions -rw-r--r--
Introducing Agent-Class to allow java.lang.instrument-like transforms of the @JavaScriptBody annotation
     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.Closeable;
    24 import java.io.Reader;
    25 import org.apidesign.html.boot.spi.Fn;
    26 import java.net.URL;
    27 import java.net.URLClassLoader;
    28 import java.util.ArrayList;
    29 import java.util.Arrays;
    30 import java.util.Enumeration;
    31 import java.util.List;
    32 import javax.script.Invocable;
    33 import javax.script.ScriptEngine;
    34 import javax.script.ScriptEngineManager;
    35 import javax.script.ScriptException;
    36 import org.testng.annotations.AfterClass;
    37 import org.testng.annotations.BeforeClass;
    38 import org.testng.annotations.BeforeMethod;
    39 
    40 /**
    41  *
    42  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    43  */
    44 public class JsClassLoaderTest extends JsClassLoaderBase{
    45     private static Fn.Presenter loader;
    46 
    47     @BeforeClass
    48     public static void setUpClass() throws Exception {
    49         ScriptEngineManager sem = new ScriptEngineManager();
    50         final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
    51         
    52         final URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
    53         ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
    54         final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
    55         class MyCL extends JsClassLoader implements Fn.Presenter {
    56 
    57             public MyCL(ClassLoader parent) {
    58                 super(parent);
    59             }
    60             
    61             @Override
    62             protected URL findResource(String name) {
    63                 return ul.getResource(name);
    64             }
    65             @Override
    66             public Fn defineFn(String code, String... names) {
    67                 StringBuilder sb = new StringBuilder();
    68                 sb.append("(function() {");
    69                 sb.append("return function(");
    70                 String sep = "";
    71                 for (String n : names) {
    72                     sb.append(sep);
    73                     sb.append(n);
    74                     sep = ", ";
    75                 }
    76                 sb.append(") {");
    77                 sb.append(code);
    78                 sb.append("};");
    79                 sb.append("})()");
    80                 try {
    81                     final Object val = eng.eval(sb.toString());
    82                     return new Fn(this) {
    83                         @Override
    84                         public Object invoke(Object thiz, Object... args) throws Exception {
    85                             List<Object> all = new ArrayList<Object>(args.length + 1);
    86                             all.add(thiz == null ? val : thiz);
    87                             all.addAll(Arrays.asList(args));
    88                             Invocable inv = (Invocable)eng;
    89                             try {
    90                                 Object ret = inv.invokeMethod(val, "call", all.toArray());
    91                                 return val.equals(ret) ? null : ret;
    92                             } catch (Exception ex) {
    93                                 throw ex;
    94                             }
    95                         }
    96                     };
    97                 } catch (ScriptException ex) {
    98                     throw new LinkageError("Can't parse: " + sb, ex);
    99                 }
   100             }
   101 
   102             @Override
   103             protected Enumeration<URL> findResources(String name) {
   104                 throw new UnsupportedOperationException();
   105             }
   106 
   107             @Override
   108             public void loadScript(Reader code) throws ScriptException {
   109                 eng.eval(code);
   110             }
   111 
   112             @Override
   113             public void displayPage(URL page, Runnable onPageLoad) {
   114                 throw new UnsupportedOperationException();
   115             }
   116         };
   117         
   118         MyCL l = new MyCL(parent);
   119         Closeable close = FnContext.activate(l);
   120         methodClass = l.loadClass(JsMethods.class.getName());
   121         close.close();
   122         loader = l;
   123     }
   124     
   125     @BeforeMethod public void initPresenter() {
   126         FnContext.currentPresenter(loader);
   127     }
   128 
   129     @AfterClass
   130     public static void cleanUp() {
   131         methodClass = null;
   132     }
   133 }