1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java Tue Feb 26 14:55:55 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,132 +0,0 @@
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 org.apidesign.bck2brwsr.launcher.impl.Console;
1.24 -import java.io.IOException;
1.25 -import java.io.InputStream;
1.26 -import java.net.URL;
1.27 -import java.util.Enumeration;
1.28 -import java.util.LinkedHashSet;
1.29 -import java.util.Set;
1.30 -import java.util.logging.Level;
1.31 -import java.util.logging.Logger;
1.32 -import javax.script.Invocable;
1.33 -import javax.script.ScriptEngine;
1.34 -import javax.script.ScriptEngineManager;
1.35 -import javax.script.ScriptException;
1.36 -import org.apidesign.vm4brwsr.Bck2Brwsr;
1.37 -
1.38 -/**
1.39 - * Tests execution in Java's internal scripting engine.
1.40 - */
1.41 -final class JSLauncher extends Launcher {
1.42 - private static final Logger LOG = Logger.getLogger(JSLauncher.class.getName());
1.43 - private Set<ClassLoader> loaders = new LinkedHashSet<>();
1.44 - private final Res resources = new Res();
1.45 - private Invocable code;
1.46 - private StringBuilder codeSeq;
1.47 - private Object console;
1.48 -
1.49 -
1.50 - @Override InvocationContext runMethod(InvocationContext mi) {
1.51 - loaders.add(mi.clazz.getClassLoader());
1.52 - try {
1.53 - long time = System.currentTimeMillis();
1.54 - LOG.log(Level.FINE, "Invoking {0}.{1}", new Object[]{mi.clazz.getName(), mi.methodName});
1.55 - String res = code.invokeMethod(
1.56 - console,
1.57 - "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
1.58 - mi.clazz.getName(), mi.methodName).toString();
1.59 - time = System.currentTimeMillis() - time;
1.60 - LOG.log(Level.FINE, "Resut of {0}.{1} = {2} in {3} ms", new Object[]{mi.clazz.getName(), mi.methodName, res, time});
1.61 - mi.result(res, null);
1.62 - } catch (ScriptException | NoSuchMethodException ex) {
1.63 - mi.result(null, ex);
1.64 - }
1.65 - return mi;
1.66 - }
1.67 -
1.68 - public void addClassLoader(ClassLoader url) {
1.69 - this.loaders.add(url);
1.70 - }
1.71 -
1.72 - @Override
1.73 - public void initialize() throws IOException {
1.74 - try {
1.75 - initRhino();
1.76 - } catch (Exception ex) {
1.77 - if (ex instanceof IOException) {
1.78 - throw (IOException)ex;
1.79 - }
1.80 - if (ex instanceof RuntimeException) {
1.81 - throw (RuntimeException)ex;
1.82 - }
1.83 - throw new IOException(ex);
1.84 - }
1.85 - }
1.86 -
1.87 - private void initRhino() throws IOException, ScriptException, NoSuchMethodException {
1.88 - StringBuilder sb = new StringBuilder();
1.89 - Bck2Brwsr.generate(sb, new Res());
1.90 -
1.91 - ScriptEngineManager sem = new ScriptEngineManager();
1.92 - ScriptEngine mach = sem.getEngineByExtension("js");
1.93 -
1.94 - sb.append(
1.95 - "\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.impl.Console.read);"
1.96 - + "\nfunction initVM() { return vm; };"
1.97 - + "\n");
1.98 -
1.99 - Object res = mach.eval(sb.toString());
1.100 - if (!(mach instanceof Invocable)) {
1.101 - throw new IOException("It is invocable object: " + res);
1.102 - }
1.103 - code = (Invocable) mach;
1.104 - codeSeq = sb;
1.105 -
1.106 - Object vm = code.invokeFunction("initVM");
1.107 - console = code.invokeMethod(vm, "loadClass", Console.class.getName());
1.108 - }
1.109 -
1.110 - @Override
1.111 - public void shutdown() throws IOException {
1.112 - }
1.113 -
1.114 - @Override
1.115 - public String toString() {
1.116 - return codeSeq.toString();
1.117 - }
1.118 -
1.119 - private class Res implements Bck2Brwsr.Resources {
1.120 - @Override
1.121 - public InputStream get(String resource) throws IOException {
1.122 - for (ClassLoader l : loaders) {
1.123 - URL u = null;
1.124 - Enumeration<URL> en = l.getResources(resource);
1.125 - while (en.hasMoreElements()) {
1.126 - u = en.nextElement();
1.127 - }
1.128 - if (u != null) {
1.129 - return u.openStream();
1.130 - }
1.131 - }
1.132 - throw new IOException("Can't find " + resource);
1.133 - }
1.134 - }
1.135 -}