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;
|
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) {
|
public String formatMap(Map<?, ?> map) {
|
||||||
StringBuffer result = new StringBuffer();
|
StringBuffer result = new StringBuffer();
|
||||||
result.append('{');
|
result.append('{');
|
||||||
@ -61,23 +78,6 @@ public class JSONFormatter {
|
|||||||
return result.toString();
|
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() {
|
private void incr() {
|
||||||
indent += 3;
|
indent += 3;
|
||||||
}
|
}
|
||||||
|
@ -20,11 +20,13 @@ public class Main {
|
|||||||
String file = cmdParser.getFile();
|
String file = cmdParser.getFile();
|
||||||
if (file != null && file.length() > 0) {
|
if (file != null && file.length() > 0) {
|
||||||
if ("-".equals(file)) {
|
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 {
|
} else {
|
||||||
File fileHandle = new File(file);
|
File fileHandle = new File(file);
|
||||||
try {
|
try {
|
||||||
console.println(formatter.format(scriptClient.execute(new FileReader(fileHandle))));
|
scriptClient.execute(new FileReader(fileHandle));
|
||||||
|
console.println(formatter.format(scriptClient.getLastRpcResult()));
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
System.err.println("File not found: " + file);
|
System.err.println("File not found: " + file);
|
||||||
}
|
}
|
||||||
@ -32,13 +34,15 @@ public class Main {
|
|||||||
}
|
}
|
||||||
String expr = cmdParser.getExpression();
|
String expr = cmdParser.getExpression();
|
||||||
if (expr != null && expr.length() > 0) {
|
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()) {
|
if (cmdParser.isInteractive()) {
|
||||||
String command = console.readInput();
|
String command = console.readInput();
|
||||||
while (!("bye".equals(command.trim()) || "exit".equals(command.trim()) || "quit".equals(command.trim()))) {
|
while (!("bye".equals(command.trim()) || "exit".equals(command.trim()) || "quit".equals(command.trim()))) {
|
||||||
try {
|
try {
|
||||||
console.println(formatter.format(scriptClient.execute(command)));
|
scriptClient.execute(command);
|
||||||
|
console.println(formatter.format(scriptClient.getLastRpcResult()));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
console.println("Error: " + e.getLocalizedMessage() + "\n");
|
console.println("Error: " + e.getLocalizedMessage() + "\n");
|
||||||
}
|
}
|
||||||
|
@ -23,8 +23,16 @@ public class ScriptClient {
|
|||||||
try {
|
try {
|
||||||
engine.put("casgrantingticket", grantingTicket);
|
engine.put("casgrantingticket", grantingTicket);
|
||||||
engine.put("xmlrpcclient", rpcClient);
|
engine.put("xmlrpcclient", rpcClient);
|
||||||
|
engine.put("xmlrpcLastResult", null);
|
||||||
engine.eval("importClass(java.util.ArrayList);");
|
engine.eval("importClass(java.util.ArrayList);");
|
||||||
engine.eval("importClass(java.util.HashMap);");
|
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) { " +
|
engine.eval("function hsaParseParamArray(o) { " +
|
||||||
"var lst = new ArrayList(); " +
|
"var lst = new ArrayList(); " +
|
||||||
"var val = ''; " +
|
"var val = ''; " +
|
||||||
@ -45,6 +53,25 @@ public class ScriptClient {
|
|||||||
" hsh.put(key, val); " +
|
" hsh.put(key, val); " +
|
||||||
"}; " +
|
"}; " +
|
||||||
"return hsh; " +
|
"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) {
|
} catch (ScriptException e) {
|
||||||
throw new JSCliException(e);
|
throw new JSCliException(e);
|
||||||
@ -72,7 +99,8 @@ public class ScriptClient {
|
|||||||
"if (typeof json === 'undefined') { json = { where:{}, set:{} } };" +
|
"if (typeof json === 'undefined') { json = { where:{}, set:{} } };" +
|
||||||
"if (fct == 'update' || fct == 'add') { params.add(hsaParseParamObject(json['set'])); }; " +
|
"if (fct == 'update' || fct == 'add') { params.add(hsaParseParamObject(json['set'])); }; " +
|
||||||
"if (fct == 'update' || fct == 'delete' || fct == 'search') { params.add(hsaParseParamObject(json['where'])); }; " +
|
"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) {
|
} catch (ScriptException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -94,6 +122,7 @@ public class ScriptClient {
|
|||||||
|
|
||||||
public Object execute(String snippet) throws JSCliException {
|
public Object execute(String snippet) throws JSCliException {
|
||||||
try {
|
try {
|
||||||
|
engine.put("xmlrpcLastResult", null);
|
||||||
return engine.eval(snippet);
|
return engine.eval(snippet);
|
||||||
} catch (ScriptException e) {
|
} catch (ScriptException e) {
|
||||||
throw new JSCliException(e);
|
throw new JSCliException(e);
|
||||||
@ -102,10 +131,15 @@ public class ScriptClient {
|
|||||||
|
|
||||||
public Object execute(Reader rd) throws JSCliException {
|
public Object execute(Reader rd) throws JSCliException {
|
||||||
try {
|
try {
|
||||||
|
engine.put("xmlrpcLastResult", null);
|
||||||
return engine.eval(rd);
|
return engine.eval(rd);
|
||||||
} catch (ScriptException e) {
|
} catch (ScriptException e) {
|
||||||
throw new JSCliException(e);
|
throw new JSCliException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Object getLastRpcResult() {
|
||||||
|
return engine.get("xmlrpcLastResult");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user