launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 29 Dec 2012 19:44:58 +0100
changeset 396 4c7619614872
parent 382 57fc3a0563c9
child 405 e41809be6106
child 526 a0d8b5ab79a2
permissions -rw-r--r--
Providing access to compiled code
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 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.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.launcher;
    19 
    20 import java.io.IOException;
    21 import java.io.InputStream;
    22 import java.net.URL;
    23 import java.util.Enumeration;
    24 import java.util.LinkedHashSet;
    25 import java.util.Set;
    26 import java.util.logging.Logger;
    27 import javax.script.Invocable;
    28 import javax.script.ScriptEngine;
    29 import javax.script.ScriptEngineManager;
    30 import javax.script.ScriptException;
    31 import org.apidesign.vm4brwsr.Bck2Brwsr;
    32 
    33 /**
    34  * Tests execution in Java's internal scripting engine.
    35  */
    36 final class JSLauncher extends Launcher {
    37     private static final Logger LOG = Logger.getLogger(JSLauncher.class.getName());
    38     private Set<ClassLoader> loaders = new LinkedHashSet<>();
    39     private final Res resources = new Res();
    40     private Invocable code;
    41     private StringBuilder codeSeq;
    42     private Object console;
    43     
    44     
    45     @Override
    46     public MethodInvocation addMethod(Class<?> clazz, String method) {
    47         loaders.add(clazz.getClassLoader());
    48         MethodInvocation mi = new MethodInvocation(clazz.getName(), method);
    49         try {
    50             mi.result(code.invokeMethod(
    51                 console,
    52                 "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
    53                 mi.className, mi.methodName).toString(), null);
    54         } catch (ScriptException | NoSuchMethodException ex) {
    55             mi.result(null, ex);
    56         }
    57         return mi;
    58     }
    59     
    60     public void addClassLoader(ClassLoader url) {
    61         this.loaders.add(url);
    62     }
    63 
    64     @Override
    65     public void initialize() throws IOException {
    66         try {
    67             initRhino();
    68         } catch (Exception ex) {
    69             if (ex instanceof IOException) {
    70                 throw (IOException)ex;
    71             }
    72             if (ex instanceof RuntimeException) {
    73                 throw (RuntimeException)ex;
    74             }
    75             throw new IOException(ex);
    76         }
    77     }
    78     
    79     private void initRhino() throws IOException, ScriptException, NoSuchMethodException {
    80         StringBuilder sb = new StringBuilder();
    81         Bck2Brwsr.generate(sb, new Res());
    82 
    83         ScriptEngineManager sem = new ScriptEngineManager();
    84         ScriptEngine mach = sem.getEngineByExtension("js");
    85 
    86         sb.append(
    87               "\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.Console.read);"
    88             + "\nfunction initVM() { return vm; };"
    89             + "\n");
    90 
    91         Object res = mach.eval(sb.toString());
    92         if (!(mach instanceof Invocable)) {
    93             throw new IOException("It is invocable object: " + res);
    94         }
    95         code = (Invocable) mach;
    96         codeSeq = sb;
    97         
    98         Object vm = code.invokeFunction("initVM");
    99         console = code.invokeMethod(vm, "loadClass", Console.class.getName());
   100     }
   101 
   102     @Override
   103     public void shutdown() throws IOException {
   104     }
   105 
   106     @Override
   107     public String toString() {
   108         return codeSeq.toString();
   109     }
   110     
   111     private class Res implements Bck2Brwsr.Resources {
   112         @Override
   113         public InputStream get(String resource) throws IOException {
   114             for (ClassLoader l : loaders) {
   115                 URL u = null;
   116                 Enumeration<URL> en = l.getResources(resource);
   117                 while (en.hasMoreElements()) {
   118                     u = en.nextElement();
   119                 }
   120                 if (u != null) {
   121                     return u.openStream();
   122                 }
   123             }
   124             throw new IOException("Can't find " + resource);
   125         }
   126     }
   127 }