support for multiple backends

This commit is contained in:
Peter Hormanns 2014-06-17 15:22:44 +02:00
parent d480089a81
commit bf28f2a755
2 changed files with 45 additions and 16 deletions

View File

@ -3,7 +3,9 @@ package de.hsadmin.jscli;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
@ -17,17 +19,23 @@ public class RpcClient {
private static final String XMLRPC_URL = "https://config.hostsharing.net:443/hsar/xmlrpc/hsadmin";
private final String xmlrpcURL;
private final XmlRpcClient client;
private final List<XmlRpcClient> clientList;
private final Map<String, XmlRpcClient> clientMap;
public RpcClient(final CASTicketProvider tgt) throws JSCliException {
clientList = new ArrayList<XmlRpcClient>();
clientMap = new HashMap<String, XmlRpcClient>();
try {
xmlrpcURL = Config.getInstance().getProperty("xmlrpcURL", XMLRPC_URL);
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL(xmlrpcURL));
config.setEnabledForExtensions(true);
client = new XmlRpcClient();
client.setConfig(config);
final String xmlrpcURLsString = Config.getInstance().getProperty("xmlrpcURL", XMLRPC_URL);
final String[] xmlrpcURLsArray = xmlrpcURLsString.split(",");
for (final String xmlrpcURL : xmlrpcURLsArray) {
final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL(xmlrpcURL));
config.setEnabledForExtensions(true);
final XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
clientList.add(client);
}
} catch (MalformedURLException e) {
throw new JSCliException(e);
}
@ -35,18 +43,25 @@ public class RpcClient {
public List<String> listMethods() throws JSCliException {
final List<String> methodList = new ArrayList<String>();
final List<Object> execute = execute("system.listMethods");
for (final Object obj : execute) {
methodList.add(obj.toString());
for (final XmlRpcClient client : clientList) {
final List<Object> execute = execute(client, "system.listMethods");
for (final Object obj : execute) {
final String methodString = obj.toString();
final String[] path = methodString.split("\\.");
if (path.length == 2) {
clientMap.put(path[0], client);
}
methodList.add(methodString);
}
}
return methodList;
}
private List<Object> execute(final String method) throws JSCliException {
return execute(method, new ArrayList<Object>());
private List<Object> execute(final XmlRpcClient client, final String method) throws JSCliException {
return execute(client, method, new ArrayList<Object>());
}
public List<Object> execute(final String method, final List<?> params) throws JSCliException {
private List<Object> execute(final XmlRpcClient client, final String method, final List<?> params) throws JSCliException {
try {
final Object execute = client.execute(method, params);
final ArrayList<Object> list = new ArrayList<Object>();
@ -62,4 +77,13 @@ public class RpcClient {
}
}
public List<Object> execute(final String method, final List<?> params) throws JSCliException {
final String[] path = method.split("\\.");
if (path.length == 2) {
return execute(clientMap.get(path[0]), method, params);
} else {
throw new JSCliException("method not found: " + method);
}
}
}

View File

@ -24,11 +24,13 @@ public class ScriptClient {
final CASTicketProvider ticketProvider = new CASTicketProvider(console, user, runAs);
final RpcClient rpcClient = new RpcClient(ticketProvider);
final ScriptEngineManager engineManager = new ScriptEngineManager();
completionStrings = new HashSet<String>();
engine = engineManager.getEngineByName("js");
engine.put("casgrantingticket", ticketProvider);
engine.put("xmlrpcclient", rpcClient);
engine.put("xmlrpcLastResult", null);
completionStrings = new HashSet<String>();
completionStrings.add("set");
completionStrings.add("where");
try {
final InputStream inputResource = getClass().getClassLoader().getResourceAsStream("js/functions.js");
engine.eval(new InputStreamReader(inputResource));
@ -40,8 +42,11 @@ public class ScriptClient {
final String[] parts = method.split("\\.");
if (parts.length == 2) {
final String module = parts[0];
completionStrings.add(module);
final String function = parts[1];
if ("system".equals(module) || "getModuleLookup".equals(function) || "createValueObject".equals(function)) {
continue;
}
completionStrings.add(module);
final String jsFunctionIdent;
if ("delete".equals(function)) {
jsFunctionIdent = module + "['remove']";