dew/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 23 Jan 2013 13:18:46 +0100
branchdew
changeset 544 08ffdc3938e7
parent 526 launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java@a0d8b5ab79a2
permissions -rw-r--r--
Moving Development Environment for Web to own project
     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 MethodInvocation addMethod(Class<?> clazz, String method, String html) {
    46         loaders.add(clazz.getClassLoader());
    47         MethodInvocation mi = new MethodInvocation(clazz.getName(), method, html);
    48         try {
    49             mi.result(code.invokeMethod(
    50                 console,
    51                 "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
    52                 mi.className, mi.methodName).toString(), null);
    53         } catch (ScriptException | NoSuchMethodException ex) {
    54             mi.result(null, ex);
    55         }
    56         return mi;
    57     }
    58     
    59     public void addClassLoader(ClassLoader url) {
    60         this.loaders.add(url);
    61     }
    62 
    63     @Override
    64     public void initialize() throws IOException {
    65         try {
    66             initRhino();
    67         } catch (Exception ex) {
    68             if (ex instanceof IOException) {
    69                 throw (IOException)ex;
    70             }
    71             if (ex instanceof RuntimeException) {
    72                 throw (RuntimeException)ex;
    73             }
    74             throw new IOException(ex);
    75         }
    76     }
    77     
    78     private void initRhino() throws IOException, ScriptException, NoSuchMethodException {
    79         StringBuilder sb = new StringBuilder();
    80         Bck2Brwsr.generate(sb, new Res());
    81 
    82         ScriptEngineManager sem = new ScriptEngineManager();
    83         ScriptEngine mach = sem.getEngineByExtension("js");
    84 
    85         sb.append(
    86               "\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.Console.read);"
    87             + "\nfunction initVM() { return vm; };"
    88             + "\n");
    89 
    90         Object res = mach.eval(sb.toString());
    91         if (!(mach instanceof Invocable)) {
    92             throw new IOException("It is invocable object: " + res);
    93         }
    94         code = (Invocable) mach;
    95         codeSeq = sb;
    96         
    97         Object vm = code.invokeFunction("initVM");
    98         console = code.invokeMethod(vm, "loadClass", Console.class.getName());
    99     }
   100 
   101     @Override
   102     public void shutdown() throws IOException {
   103     }
   104 
   105     @Override
   106     public String toString() {
   107         return codeSeq.toString();
   108     }
   109     
   110     private class Res implements Bck2Brwsr.Resources {
   111         @Override
   112         public InputStream get(String resource) throws IOException {
   113             for (ClassLoader l : loaders) {
   114                 URL u = null;
   115                 Enumeration<URL> en = l.getResources(resource);
   116                 while (en.hasMoreElements()) {
   117                     u = en.nextElement();
   118                 }
   119                 if (u != null) {
   120                     return u.openStream();
   121                 }
   122             }
   123             throw new IOException("Can't find " + resource);
   124         }
   125     }
   126 }