launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 16 Dec 2012 20:11:18 +0100
branchlauncher
changeset 332 6949044415df
parent 323 d41cfd77842d
child 342 60f9aad12731
permissions -rw-r--r--
First steps towards execution harness
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@332
    20
import java.io.InputStream;
jaroslav@332
    21
import java.lang.reflect.InvocationTargetException;
jaroslav@323
    22
import java.lang.reflect.Method;
jaroslav@332
    23
import java.net.URL;
jaroslav@323
    24
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@323
    25
jaroslav@323
    26
/**
jaroslav@323
    27
 *
jaroslav@323
    28
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@323
    29
 */
jaroslav@323
    30
public class Console {
jaroslav@323
    31
    public static String welcome() {
jaroslav@323
    32
        final String msg = "Hello from Bck2Brwsr!";
jaroslav@323
    33
        alert(msg);
jaroslav@323
    34
        return msg;
jaroslav@323
    35
    }
jaroslav@323
    36
    
jaroslav@323
    37
    @JavaScriptBody(args = "msg", body = "alert(msg);")
jaroslav@323
    38
    private static native void alert(String msg);
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@323
    47
    
jaroslav@323
    48
    public static void execute() throws Exception {
jaroslav@323
    49
        String clazz = (String) getAttr("clazz", "value");
jaroslav@323
    50
        String method = (String) getAttr("method", "value");
jaroslav@332
    51
        Object res = invokeMethod(clazz, method);
jaroslav@332
    52
        setAttr("result", "value", res);
jaroslav@332
    53
    }
jaroslav@332
    54
    
jaroslav@332
    55
    public static void harness() {
jaroslav@332
    56
        try {
jaroslav@332
    57
            URL u = new URL("/execute/data");
jaroslav@332
    58
            String data = (String) u.getContent(new Class[] { String.class });
jaroslav@332
    59
            setAttr("result", "value", data);
jaroslav@332
    60
        } catch (Exception ex) {
jaroslav@332
    61
            setAttr("result", "value", ex.getMessage());
jaroslav@332
    62
        }
jaroslav@332
    63
    }
jaroslav@323
    64
jaroslav@332
    65
    private static Object invokeMethod(String clazz, String method) 
jaroslav@332
    66
    throws ClassNotFoundException, InvocationTargetException, 
jaroslav@332
    67
    SecurityException, IllegalAccessException, IllegalArgumentException {
jaroslav@323
    68
        Method found = null;
jaroslav@323
    69
        Class<?> c = Class.forName(clazz);
jaroslav@323
    70
        for (Method m : c.getMethods()) {
jaroslav@323
    71
            if (m.getName().equals(method)) {
jaroslav@323
    72
                found = m;
jaroslav@323
    73
            }
jaroslav@323
    74
        }
jaroslav@323
    75
        Object res;
jaroslav@323
    76
        if (found != null) {
jaroslav@323
    77
            res = found.invoke(null);
jaroslav@323
    78
        } else {
jaroslav@323
    79
            res = "Can't find method " + method + " in " + clazz;
jaroslav@323
    80
        }
jaroslav@332
    81
        return res;
jaroslav@323
    82
    }
jaroslav@323
    83
}