hsscript returns regular javascript obejcts on hsadmin server calls
This commit is contained in:
parent
d9afe25155
commit
4439e6a3c8
@ -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;
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
|
@ -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);
|
||||
@ -72,7 +99,8 @@ public class ScriptClient {
|
||||
"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); " +
|
||||
"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");
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user