launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 15 Dec 2012 16:25:28 +0100
branchlauncher
changeset 323 d41cfd77842d
child 332 6949044415df
permissions -rw-r--r--
Basic sketch of an HTTP server for launching bck2brwsr applications
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@323
    20
import java.lang.reflect.Method;
jaroslav@323
    21
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@323
    22
jaroslav@323
    23
/**
jaroslav@323
    24
 *
jaroslav@323
    25
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@323
    26
 */
jaroslav@323
    27
public class Console {
jaroslav@323
    28
    public static String welcome() {
jaroslav@323
    29
        final String msg = "Hello from Bck2Brwsr!";
jaroslav@323
    30
        alert(msg);
jaroslav@323
    31
        return msg;
jaroslav@323
    32
    }
jaroslav@323
    33
    
jaroslav@323
    34
    @JavaScriptBody(args = "msg", body = "alert(msg);")
jaroslav@323
    35
    private static native void alert(String msg);
jaroslav@323
    36
    
jaroslav@323
    37
    @JavaScriptBody(args = {"id", "attr"}, body = 
jaroslav@323
    38
        "return window.document.getElementById(id)[attr].toString();")
jaroslav@323
    39
    private static native Object getAttr(String id, String attr);
jaroslav@323
    40
jaroslav@323
    41
    @JavaScriptBody(args = {"id", "attr", "value"}, body = 
jaroslav@323
    42
        "window.document.getElementById(id)[attr] = value;")
jaroslav@323
    43
    private static native void setAttr(String id, String attr, Object value);
jaroslav@323
    44
    
jaroslav@323
    45
    public static void execute() throws Exception {
jaroslav@323
    46
        String clazz = (String) getAttr("clazz", "value");
jaroslav@323
    47
        String method = (String) getAttr("method", "value");
jaroslav@323
    48
jaroslav@323
    49
        Method found = null;
jaroslav@323
    50
        Class<?> c = Class.forName(clazz);
jaroslav@323
    51
        for (Method m : c.getMethods()) {
jaroslav@323
    52
            if (m.getName().equals(method)) {
jaroslav@323
    53
                found = m;
jaroslav@323
    54
            }
jaroslav@323
    55
        }
jaroslav@323
    56
        Object res;
jaroslav@323
    57
        if (found != null) {
jaroslav@323
    58
            res = found.invoke(null);
jaroslav@323
    59
        } else {
jaroslav@323
    60
            res = "Can't find method " + method + " in " + clazz;
jaroslav@323
    61
        }
jaroslav@323
    62
        
jaroslav@323
    63
        setAttr("result", "value", res);
jaroslav@323
    64
    }
jaroslav@323
    65
}