HSAdmin Backend Domains, E-Mail, Datenbanken
Peter Hormanns
2012-12-20 4439e6a3c8d6a045c27ddf840ab591db176bce19
hsscript returns regular javascript obejcts on hsadmin server calls
3 files modified
84 ■■■■ changed files
hsarjcli/src/de/hsadmin/jscli/JSONFormatter.java 34 ●●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/jscli/Main.java 12 ●●●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/jscli/ScriptClient.java 38 ●●●●● patch | view | raw | blame | history
hsarjcli/src/de/hsadmin/jscli/JSONFormatter.java
@@ -8,6 +8,23 @@
    
    private int indent = 1;
    public String format(Object object) {
        if (object == null) return "";
        if (object instanceof List<?>) {
            return formatList((List<?>) object);
        }
        if (object instanceof Map<?, ?>) {
            return formatMap((Map<?, ?>) object);
        }
        if (object instanceof String) {
            return formatString((String) object);
        }
        if (object instanceof Object[]) {
            return formatArr((Object[]) object);
        }
        return "an instance of " + object.getClass().getCanonicalName();
    }
    public String formatMap(Map<?, ?> map) {
        StringBuffer result = new StringBuffer();
        result.append('{');
@@ -59,23 +76,6 @@
        }
        result.append(']');
        return result.toString();
    }
    public String format(Object object) {
        if (object == null) return "";
        if (object instanceof List<?>) {
            return formatList((List<?>) object);
        }
        if (object instanceof Map<?, ?>) {
            return formatMap((Map<?, ?>) object);
        }
        if (object instanceof String) {
            return formatString((String) object);
        }
        if (object instanceof Object[]) {
            return formatArr((Object[]) object);
        }
        return "an instance of " + object.getClass().getCanonicalName();
    }
    
    private void incr() {
hsarjcli/src/de/hsadmin/jscli/Main.java
@@ -20,11 +20,13 @@
            String file = cmdParser.getFile();
            if (file != null && file.length() > 0) {
                if ("-".equals(file)) {
                    console.println(formatter.format(scriptClient.execute(new InputStreamReader(System.in))));
                    scriptClient.execute(new InputStreamReader(System.in));
                    console.println(formatter.format(scriptClient.getLastRpcResult()));
                } else {
                    File fileHandle = new File(file);
                    try {
                        console.println(formatter.format(scriptClient.execute(new FileReader(fileHandle))));
                        scriptClient.execute(new FileReader(fileHandle));
                        console.println(formatter.format(scriptClient.getLastRpcResult()));
                    } catch (FileNotFoundException e) {
                        System.err.println("File not found: " + file);
                    }
@@ -32,13 +34,15 @@
            }
            String expr = cmdParser.getExpression();
            if (expr != null && expr.length() > 0) {
                console.println(formatter.format(scriptClient.execute(expr)));
                scriptClient.execute(expr);
                console.println(formatter.format(scriptClient.getLastRpcResult()));
            }
            if (cmdParser.isInteractive()) {
                String command = console.readInput();
                while (!("bye".equals(command.trim()) || "exit".equals(command.trim()) || "quit".equals(command.trim()))) {
                    try {
                        console.println(formatter.format(scriptClient.execute(command)));
                        scriptClient.execute(command);
                        console.println(formatter.format(scriptClient.getLastRpcResult()));
                    } catch (Exception e) {
                        console.println("Error: " + e.getLocalizedMessage() + "\n");
                    }
hsarjcli/src/de/hsadmin/jscli/ScriptClient.java
@@ -23,8 +23,16 @@
        try {
            engine.put("casgrantingticket", grantingTicket);
            engine.put("xmlrpcclient", rpcClient);
            engine.put("xmlrpcLastResult", null);
            engine.eval("importClass(java.util.ArrayList);");
            engine.eval("importClass(java.util.HashMap);");
            engine.eval("function hsaParseParam(val) { " +
                    "if (val instanceof java.util.List) return val;" +
                    "if (val instanceof java.util.Map) return val;" +
                    "if (typeof val === 'object' && val.constructor === Array) { res = hsaParseParamArray(val); } " +
                    "else if (typeof val === 'object') { res = hsaParseParamObject(val); }; " +
                    "return res; " +
                    "}");
            engine.eval("function hsaParseParamArray(o) { " +
                    "var lst = new ArrayList(); " +
                    "var val = ''; " +
@@ -45,6 +53,25 @@
                    "  hsh.put(key, val); " +
                    "}; " +
                    "return hsh; " +
                    "}");
            engine.eval("function hsaToNativeJSObject(val) { " +
                    "if (val instanceof java.util.List) {" +
                    "    var res = [];" +
                    "    for (i = 0; i < val.size(); i++) {" +
                    "        res[i] = hsaToNativeJSObject(val.get(i));" +
                    "    }" +
                    "    return res;" +
                    "}" +
                    "if (val instanceof java.util.Map) {" +
                    "    var res = {};" +
                    "    var iter = val.keySet().iterator();" +
                    "    while (iter.hasNext()) {" +
                    "        var key = iter.next();" +
                    "        res[key] = hsaToNativeJSObject(val.get(key));" +
                    "    }" +
                    "    return res;" +
                    "}" +
                    "return val;" +
                    "};");
        } catch (ScriptException e) {
            throw new JSCliException(e);
@@ -71,8 +98,9 @@
                                "params.add(casgrantingticket.getTicket()); " + 
                                "if (typeof json === 'undefined') { json = { where:{}, set:{} } };" +
                                "if (fct == 'update' || fct == 'add') { params.add(hsaParseParamObject(json['set'])); }; " + 
                                "if (fct == 'update' || fct == 'delete' || fct == 'search') { params.add(hsaParseParamObject(json['where'])); }; " +
                                "return xmlrpcclient.execute(mod + '.' + fct, params); " +
                                "if (fct == 'update' || fct == 'delete' || fct == 'search') { params.add(hsaParseParamObject(json['where'])); }; " +
                                "xmlrpcLastResult = xmlrpcclient.execute(mod + '.' + fct, params);" +
                                "return hsaToNativeJSObject(xmlrpcLastResult); " +
                                "};");
                } catch (ScriptException e) {
                    e.printStackTrace();
@@ -94,6 +122,7 @@
    public Object execute(String snippet) throws JSCliException {
        try {
            engine.put("xmlrpcLastResult", null);
            return engine.eval(snippet);
        } catch (ScriptException e) {
            throw new JSCliException(e);
@@ -102,10 +131,15 @@
    
    public Object execute(Reader rd) throws JSCliException {
        try {
            engine.put("xmlrpcLastResult", null);
            return engine.eval(rd);
        } catch (ScriptException e) {
            throw new JSCliException(e);
        }
    }
    
    public Object getLastRpcResult() {
        return engine.get("xmlrpcLastResult");
    }
}