javascript cli client calls server

This commit is contained in:
Peter Hormanns 2012-07-24 16:25:52 +00:00
parent eb2800ad1c
commit 44494fe269
5 changed files with 92 additions and 16 deletions

View File

@ -10,9 +10,13 @@ public class Scripting {
ScriptEngineManager engineManager = new ScriptEngineManager();
ScriptEngine engine = engineManager.getEngineByName("js");
try {
engine.eval("var user = { };");
engine.eval("if (typeof user === 'undefined') { user = { }; };");
engine.eval("user['name'] = 'Klaus-Dieter';");
engine.eval("print(user.name);");
engine.eval("if (typeof user === 'undefined') { user = { }; };");
engine.eval("user['age'] = 44;");
engine.eval("print(user.name + '\\n');");
engine.eval("print(user.age + '\\n');");
engine.eval("print(user + '\\n');");
} catch (ScriptException e) {
System.err.println(e.getMessage());
}

View File

@ -20,8 +20,10 @@ public class CASTicket {
private String loginURL;
private String backendURL;
private String grantingTicket;
private String runAs;
public CASTicket(String user) throws JSCliException {
public CASTicket(String user, String runAs) throws JSCliException {
this.runAs = runAs;
loginURL = Config.getInstance().getProperty("loginURL", LOGIN_URL);
backendURL = Config.getInstance().getProperty("backendURL", BACKEND_URL);
grantingTicket = getGrantingTicket(user);
@ -37,6 +39,10 @@ public class CASTicket {
}
}
public String getRunAs() {
return runAs;
}
private String getGrantingTicket(String user) throws JSCliException {
grantingTicket = null;
char[] password = null;

View File

@ -1,6 +1,5 @@
package de.hsadmin.jscli;
import java.util.List;
public class Main {
@ -8,13 +7,12 @@ public class Main {
try {
CommandlineParser cmdParser = new CommandlineParser(args);
String user = cmdParser.getUser();
CASTicket tgt = new CASTicket(user);
System.out.println(tgt);
RpcClient client = new RpcClient(tgt);
List<String> methods = client.listMethods();
for (String m : methods) {
System.out.println(m);
}
String runAs = cmdParser.getRunAs();
CASTicket tgt = new CASTicket(user, runAs);
RpcClient rpcClient = new RpcClient(tgt);
ScriptClient scriptClient = new ScriptClient(tgt, rpcClient);
Object o = scriptClient.execute(cmdParser.getExpression());
System.out.println(o);
} catch (JSCliException e) {
System.err.println(e.getMessage());
System.exit(-1);

View File

@ -31,20 +31,20 @@ public class RpcClient {
public List<String> listMethods() throws JSCliException {
List<String> methodList = new ArrayList<String>();
List<Object> execute = execute();
List<Object> execute = execute("system.listMethods");
for (Object obj : execute) {
methodList.add(obj.toString());
}
return methodList;
}
private List<Object> execute() throws JSCliException {
return execute(new ArrayList<Object>());
private List<Object> execute(String method) throws JSCliException {
return execute(method, new ArrayList<Object>());
}
private List<Object> execute(List<?> params) throws JSCliException {
public List<Object> execute(String method, List<?> params) throws JSCliException {
try {
Object execute = client.execute("system.listMethods", params);
Object execute = client.execute(method, params);
ArrayList<Object> list = new ArrayList<Object>();
if (execute instanceof Object[]) {
Object[] resArray = (Object[]) execute;

View File

@ -0,0 +1,68 @@
package de.hsadmin.jscli;
import java.util.List;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class ScriptClient {
private ScriptEngine engine;
public ScriptClient(CASTicket grantingTicket, RpcClient rpcClient) throws JSCliException {
ScriptEngineManager engineManager = new ScriptEngineManager();
engine = engineManager.getEngineByName("js");
try {
engine.put("casgrantingticket", grantingTicket);
engine.put("xmlrpcclient", rpcClient);
engine.eval("importClass(java.util.ArrayList);");
engine.eval("importClass(java.util.HashMap);");
engine.eval("function hsaParseParamObject(o) { " +
"var hsh = new HashMap(); " +
"for (var key in o) { " +
" var val = o[key]; " +
" if (typeof val === 'object') { val = hsaParseParamObject(val); }; " +
" hsh.put(key, val); " +
"}; " +
"return hsh; " +
"};");
} catch (ScriptException e) {
throw new JSCliException(e);
}
List<String> methods = rpcClient.listMethods();
for (String method : methods) {
String[] parts = method.split("\\.");
if (parts.length == 2) {
String module = parts[0];
String function = parts[1];
try {
engine.eval("if (typeof " + module + " === 'undefined') " +
"{ var " + module + " = { }; };\n" +
module + "['" + function + "'] = function(json) { " +
"var mod = '" + module + "'; " +
"var fct = '" + function + "'; " +
"var params = new ArrayList(); " +
"params.add(casgrantingticket.getRunAs()); " +
"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); " +
"};");
} catch (ScriptException e) {
e.printStackTrace();
}
}
}
}
public Object execute(String snippet) throws JSCliException {
try {
return engine.eval(snippet);
} catch (ScriptException e) {
throw new JSCliException(e);
}
}
}