launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java
branchlauncher
changeset 370 ed48023d1d85
parent 369 723d854272ae
child 371 bafc670aa10d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java	Sun Dec 23 17:02:34 2012 +0100
     1.3 @@ -0,0 +1,116 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.launcher;
    1.22 +
    1.23 +import java.io.IOException;
    1.24 +import java.io.InputStream;
    1.25 +import java.net.URL;
    1.26 +import java.util.Enumeration;
    1.27 +import java.util.LinkedHashSet;
    1.28 +import java.util.Set;
    1.29 +import java.util.logging.Logger;
    1.30 +import javax.script.Invocable;
    1.31 +import javax.script.ScriptEngine;
    1.32 +import javax.script.ScriptEngineManager;
    1.33 +import javax.script.ScriptException;
    1.34 +import org.apidesign.vm4brwsr.Bck2Brwsr;
    1.35 +
    1.36 +/**
    1.37 + * Tests execution in Java's internal scripting engine.
    1.38 + */
    1.39 +public final class JSLauncher {
    1.40 +    private static final Logger LOG = Logger.getLogger(JSLauncher.class.getName());
    1.41 +    private Set<ClassLoader> loaders = new LinkedHashSet<>();
    1.42 +    private final Res resources = new Res();
    1.43 +    private Invocable code;
    1.44 +    private Object console;
    1.45 +    
    1.46 +    
    1.47 +    public MethodInvocation addMethod(Class<?> clazz, String method) {
    1.48 +        loaders.add(clazz.getClassLoader());
    1.49 +        MethodInvocation mi = new MethodInvocation(clazz.getName(), method);
    1.50 +        try {
    1.51 +            mi.result = code.invokeMethod(
    1.52 +                console,
    1.53 +                "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
    1.54 +                mi.className, mi.methodName).toString();
    1.55 +        } catch (ScriptException scriptException) {
    1.56 +            mi.exception = scriptException;
    1.57 +        } catch (NoSuchMethodException noSuchMethodException) {
    1.58 +            mi.exception = noSuchMethodException;
    1.59 +        }
    1.60 +        return mi;
    1.61 +    }
    1.62 +    
    1.63 +    public void addClassLoader(ClassLoader url) {
    1.64 +        this.loaders.add(url);
    1.65 +    }
    1.66 +
    1.67 +    public void initialize() throws IOException {
    1.68 +        try {
    1.69 +            initRhino();
    1.70 +        } catch (Exception ex) {
    1.71 +            if (ex instanceof IOException) {
    1.72 +                throw (IOException)ex;
    1.73 +            }
    1.74 +            if (ex instanceof RuntimeException) {
    1.75 +                throw (RuntimeException)ex;
    1.76 +            }
    1.77 +            throw new IOException(ex);
    1.78 +        }
    1.79 +    }
    1.80 +    
    1.81 +    private void initRhino() throws IOException, ScriptException, NoSuchMethodException {
    1.82 +        StringBuilder sb = new StringBuilder();
    1.83 +        Bck2Brwsr.generate(sb, new Res());
    1.84 +
    1.85 +        ScriptEngineManager sem = new ScriptEngineManager();
    1.86 +        ScriptEngine mach = sem.getEngineByExtension("js");
    1.87 +
    1.88 +        sb.append(
    1.89 +              "\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.Console.read);"
    1.90 +            + "\nfunction initVM() { return vm; };"
    1.91 +            + "\n");
    1.92 +
    1.93 +        Object res = mach.eval(sb.toString());
    1.94 +        if (!(mach instanceof Invocable)) {
    1.95 +            throw new IOException("It is invocable object: " + res);
    1.96 +        }
    1.97 +        code = (Invocable) mach;
    1.98 +        
    1.99 +        Object vm = code.invokeFunction("initVM");
   1.100 +        console = code.invokeMethod(vm, "loadClass", Console.class.getName());
   1.101 +    }
   1.102 +    
   1.103 +    private class Res implements Bck2Brwsr.Resources {
   1.104 +        @Override
   1.105 +        public InputStream get(String resource) throws IOException {
   1.106 +            for (ClassLoader l : loaders) {
   1.107 +                URL u = null;
   1.108 +                Enumeration<URL> en = l.getResources(resource);
   1.109 +                while (en.hasMoreElements()) {
   1.110 +                    u = en.nextElement();
   1.111 +                }
   1.112 +                if (u != null) {
   1.113 +                    return u.openStream();
   1.114 +                }
   1.115 +            }
   1.116 +            throw new IOException("Can't find " + resource);
   1.117 +        }
   1.118 +    }
   1.119 +}