launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 23 Dec 2012 17:02:34 +0100
branchlauncher
changeset 370 ed48023d1d85
parent 369 launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java@723d854272ae
child 371 bafc670aa10d
permissions -rw-r--r--
JavaScript and HTTP launchers separated
     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 public final class JSLauncher {
    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 Object console;
    42     
    43     
    44     public MethodInvocation addMethod(Class<?> clazz, String method) {
    45         loaders.add(clazz.getClassLoader());
    46         MethodInvocation mi = new MethodInvocation(clazz.getName(), method);
    47         try {
    48             mi.result = code.invokeMethod(
    49                 console,
    50                 "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
    51                 mi.className, mi.methodName).toString();
    52         } catch (ScriptException scriptException) {
    53             mi.exception = scriptException;
    54         } catch (NoSuchMethodException noSuchMethodException) {
    55             mi.exception = noSuchMethodException;
    56         }
    57         return mi;
    58     }
    59     
    60     public void addClassLoader(ClassLoader url) {
    61         this.loaders.add(url);
    62     }
    63 
    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         
    96         Object vm = code.invokeFunction("initVM");
    97         console = code.invokeMethod(vm, "loadClass", Console.class.getName());
    98     }
    99     
   100     private class Res implements Bck2Brwsr.Resources {
   101         @Override
   102         public InputStream get(String resource) throws IOException {
   103             for (ClassLoader l : loaders) {
   104                 URL u = null;
   105                 Enumeration<URL> en = l.getResources(resource);
   106                 while (en.hasMoreElements()) {
   107                     u = en.nextElement();
   108                 }
   109                 if (u != null) {
   110                     return u.openStream();
   111                 }
   112             }
   113             throw new IOException("Can't find " + resource);
   114         }
   115     }
   116 }