launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 22 Dec 2012 21:35:14 +0100
branchlauncher
changeset 366 ca2be963f3b9
parent 360 86f3ea771e24
child 381 70d15cf323ba
permissions -rw-r--r--
Closing the window when data are processed.
jaroslav@323
     1
/**
jaroslav@323
     2
 * Back 2 Browser Bytecode Translator
jaroslav@323
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@323
     4
 *
jaroslav@323
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@323
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@323
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@323
     8
 *
jaroslav@323
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@323
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@323
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@323
    12
 * GNU General Public License for more details.
jaroslav@323
    13
 *
jaroslav@323
    14
 * You should have received a copy of the GNU General Public License
jaroslav@323
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@323
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@323
    17
 */
jaroslav@323
    18
package org.apidesign.bck2brwsr.launcher;
jaroslav@323
    19
jaroslav@360
    20
import java.io.IOException;
jaroslav@360
    21
import java.io.InputStream;
jaroslav@332
    22
import java.lang.reflect.InvocationTargetException;
jaroslav@323
    23
import java.lang.reflect.Method;
jaroslav@332
    24
import java.net.URL;
jaroslav@360
    25
import java.util.Enumeration;
jaroslav@323
    26
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@323
    27
jaroslav@323
    28
/**
jaroslav@323
    29
 *
jaroslav@323
    30
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@323
    31
 */
jaroslav@323
    32
public class Console {
jaroslav@323
    33
    public static String welcome() {
jaroslav@342
    34
        return "HellofromBck2Brwsr";
jaroslav@323
    35
    }
jaroslav@342
    36
    public static String multiply() {
jaroslav@342
    37
        return String.valueOf(Integer.MAX_VALUE / 2 + Integer.MAX_VALUE);
jaroslav@342
    38
    }
jaroslav@323
    39
    
jaroslav@323
    40
    @JavaScriptBody(args = {"id", "attr"}, body = 
jaroslav@323
    41
        "return window.document.getElementById(id)[attr].toString();")
jaroslav@323
    42
    private static native Object getAttr(String id, String attr);
jaroslav@323
    43
jaroslav@323
    44
    @JavaScriptBody(args = {"id", "attr", "value"}, body = 
jaroslav@323
    45
        "window.document.getElementById(id)[attr] = value;")
jaroslav@323
    46
    private static native void setAttr(String id, String attr, Object value);
jaroslav@366
    47
    
jaroslav@366
    48
    @JavaScriptBody(args = {}, body = "window.close();")
jaroslav@366
    49
    private static native void closeWindow();
jaroslav@342
    50
jaroslav@343
    51
    private static void log(String newText) {
jaroslav@343
    52
        String id = "result";
jaroslav@343
    53
        String attr = "value";
jaroslav@342
    54
        setAttr(id, attr, getAttr(id, attr) + "\n" + newText);
jaroslav@343
    55
        setAttr(id, "scrollTop", getAttr(id, "scrollHeight"));
jaroslav@342
    56
    }
jaroslav@323
    57
    
jaroslav@323
    58
    public static void execute() throws Exception {
jaroslav@323
    59
        String clazz = (String) getAttr("clazz", "value");
jaroslav@323
    60
        String method = (String) getAttr("method", "value");
jaroslav@332
    61
        Object res = invokeMethod(clazz, method);
jaroslav@332
    62
        setAttr("result", "value", res);
jaroslav@332
    63
    }
jaroslav@332
    64
    
jaroslav@342
    65
    public static void harness(String url) {
jaroslav@343
    66
        log("Connecting to " + url);
jaroslav@332
    67
        try {
jaroslav@342
    68
            URL u = new URL(url);
jaroslav@342
    69
            for (;;) {
jaroslav@342
    70
                String data = (String) u.getContent(new Class[] { String.class });
jaroslav@344
    71
                log("\nGot \"" + data + "\"");
jaroslav@342
    72
                if (data.isEmpty()) {
jaroslav@343
    73
                    log("No data, exiting");
jaroslav@366
    74
                    closeWindow();
jaroslav@342
    75
                    break;
jaroslav@342
    76
                }
jaroslav@342
    77
                
jaroslav@342
    78
                Case c = Case.parseData(data);
jaroslav@344
    79
                log("Invoking " + c.getClassName() + '.' + c.getMethodName() + " as request: " + c.getRequestId());
jaroslav@342
    80
jaroslav@342
    81
                Object result = invokeMethod(c.getClassName(), c.getMethodName());
jaroslav@355
    82
                
jaroslav@344
    83
                log("Result: " + result);
jaroslav@355
    84
                log("Sending back: " + url + "?request=" + c.getRequestId() + "&result=" + result);
jaroslav@355
    85
                u = new URL(url + "?request=" + c.getRequestId() + "&result=" + result);
jaroslav@342
    86
            }
jaroslav@342
    87
            
jaroslav@342
    88
            
jaroslav@332
    89
        } catch (Exception ex) {
jaroslav@343
    90
            log(ex.getMessage());
jaroslav@332
    91
        }
jaroslav@332
    92
    }
jaroslav@356
    93
    
jaroslav@356
    94
    static String invoke(String clazz, String method) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException {
jaroslav@356
    95
        final Object r = invokeMethod(clazz, method);
jaroslav@356
    96
        return r == null ? "null" : r.toString().toString();
jaroslav@356
    97
    }
jaroslav@323
    98
jaroslav@360
    99
    /** Helper method that inspects the classpath and loads given resource
jaroslav@360
   100
     * (usually a class file). Used while running tests in Rhino.
jaroslav@360
   101
     * 
jaroslav@360
   102
     * @param name resource name to find
jaroslav@360
   103
     * @return the array of bytes in the given resource
jaroslav@360
   104
     * @throws IOException I/O in case something goes wrong
jaroslav@360
   105
     */
jaroslav@360
   106
    public static byte[] read(String name) throws IOException {
jaroslav@360
   107
        URL u = null;
jaroslav@360
   108
        Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
jaroslav@360
   109
        while (en.hasMoreElements()) {
jaroslav@360
   110
            u = en.nextElement();
jaroslav@360
   111
        }
jaroslav@360
   112
        if (u == null) {
jaroslav@360
   113
            throw new IOException("Can't find " + name);
jaroslav@360
   114
        }
jaroslav@360
   115
        try (InputStream is = u.openStream()) {
jaroslav@360
   116
            byte[] arr;
jaroslav@360
   117
            arr = new byte[is.available()];
jaroslav@360
   118
            int offset = 0;
jaroslav@360
   119
            while (offset < arr.length) {
jaroslav@360
   120
                int len = is.read(arr, offset, arr.length - offset);
jaroslav@360
   121
                if (len == -1) {
jaroslav@360
   122
                    throw new IOException("Can't read " + name);
jaroslav@360
   123
                }
jaroslav@360
   124
                offset += len;
jaroslav@360
   125
            }
jaroslav@360
   126
            return arr;
jaroslav@360
   127
        }
jaroslav@360
   128
    }
jaroslav@360
   129
   
jaroslav@332
   130
    private static Object invokeMethod(String clazz, String method) 
jaroslav@332
   131
    throws ClassNotFoundException, InvocationTargetException, 
jaroslav@332
   132
    SecurityException, IllegalAccessException, IllegalArgumentException {
jaroslav@323
   133
        Method found = null;
jaroslav@323
   134
        Class<?> c = Class.forName(clazz);
jaroslav@323
   135
        for (Method m : c.getMethods()) {
jaroslav@323
   136
            if (m.getName().equals(method)) {
jaroslav@323
   137
                found = m;
jaroslav@323
   138
            }
jaroslav@323
   139
        }
jaroslav@323
   140
        Object res;
jaroslav@323
   141
        if (found != null) {
jaroslav@323
   142
            res = found.invoke(null);
jaroslav@323
   143
        } else {
jaroslav@323
   144
            res = "Can't find method " + method + " in " + clazz;
jaroslav@323
   145
        }
jaroslav@332
   146
        return res;
jaroslav@323
   147
    }
jaroslav@342
   148
    
jaroslav@342
   149
    private static final class Case {
jaroslav@342
   150
        private final Object data;
jaroslav@342
   151
jaroslav@342
   152
        private Case(Object data) {
jaroslav@342
   153
            this.data = data;
jaroslav@342
   154
        }
jaroslav@342
   155
        
jaroslav@342
   156
        public static Case parseData(String s) {
jaroslav@342
   157
            return new Case(toJSON(s));
jaroslav@342
   158
        }
jaroslav@342
   159
        
jaroslav@342
   160
        public String getMethodName() {
jaroslav@342
   161
            return value("methodName", data);
jaroslav@342
   162
        }
jaroslav@342
   163
jaroslav@342
   164
        public String getClassName() {
jaroslav@342
   165
            return value("className", data);
jaroslav@342
   166
        }
jaroslav@342
   167
        
jaroslav@342
   168
        public String getRequestId() {
jaroslav@342
   169
            return value("request", data);
jaroslav@342
   170
        }
jaroslav@342
   171
        
jaroslav@342
   172
        @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
jaroslav@342
   173
        private static native Object toJSON(String s);
jaroslav@342
   174
        
jaroslav@342
   175
        @JavaScriptBody(args = {"p", "d"}, body = "return d[p].toString();")
jaroslav@342
   176
        private static native String value(String p, Object d);
jaroslav@342
   177
    }
jaroslav@323
   178
}