1.1 --- a/dew/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java Wed Jan 23 13:56:54 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,126 +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 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 -final class JSLauncher extends Launcher {
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 StringBuilder codeSeq;
1.45 - private Object console;
1.46 -
1.47 -
1.48 - @Override MethodInvocation addMethod(Class<?> clazz, String method, String html) {
1.49 - loaders.add(clazz.getClassLoader());
1.50 - MethodInvocation mi = new MethodInvocation(clazz.getName(), method, html);
1.51 - try {
1.52 - mi.result(code.invokeMethod(
1.53 - console,
1.54 - "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
1.55 - mi.className, mi.methodName).toString(), null);
1.56 - } catch (ScriptException | NoSuchMethodException ex) {
1.57 - mi.result(null, ex);
1.58 - }
1.59 - return mi;
1.60 - }
1.61 -
1.62 - public void addClassLoader(ClassLoader url) {
1.63 - this.loaders.add(url);
1.64 - }
1.65 -
1.66 - @Override
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 - codeSeq = sb;
1.99 -
1.100 - Object vm = code.invokeFunction("initVM");
1.101 - console = code.invokeMethod(vm, "loadClass", Console.class.getName());
1.102 - }
1.103 -
1.104 - @Override
1.105 - public void shutdown() throws IOException {
1.106 - }
1.107 -
1.108 - @Override
1.109 - public String toString() {
1.110 - return codeSeq.toString();
1.111 - }
1.112 -
1.113 - private class Res implements Bck2Brwsr.Resources {
1.114 - @Override
1.115 - public InputStream get(String resource) throws IOException {
1.116 - for (ClassLoader l : loaders) {
1.117 - URL u = null;
1.118 - Enumeration<URL> en = l.getResources(resource);
1.119 - while (en.hasMoreElements()) {
1.120 - u = en.nextElement();
1.121 - }
1.122 - if (u != null) {
1.123 - return u.openStream();
1.124 - }
1.125 - }
1.126 - throw new IOException("Can't find " + resource);
1.127 - }
1.128 - }
1.129 -}