hsscript returns regular javascript obejcts on hsadmin server calls

This commit is contained in:
Peter Hormanns 2012-12-20 16:09:53 +01:00
parent d9afe25155
commit 4439e6a3c8
3 changed files with 61 additions and 23 deletions

View File

@ -8,6 +8,23 @@ public class JSONFormatter {
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('{');
@ -61,23 +78,6 @@ public class JSONFormatter {
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() {
indent += 3;
}

View File

@ -20,11 +20,13 @@ public class Main {
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 @@ public class Main {
}
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");
}

View File

@ -23,8 +23,16 @@ public class ScriptClient {
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 @@ public class ScriptClient {
" 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 @@ public class ScriptClient {
"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 class ScriptClient {
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 class ScriptClient {
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");
}
}