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.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient; 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 static final String XMLRPC_URL = "https://config.hostsharing.net:443/hsar/xmlrpc/hsadmin";
private final String xmlrpcURL; private final List<XmlRpcClient> clientList;
private final XmlRpcClient client; private final Map<String, XmlRpcClient> clientMap;
public RpcClient(final CASTicketProvider tgt) throws JSCliException { public RpcClient(final CASTicketProvider tgt) throws JSCliException {
clientList = new ArrayList<XmlRpcClient>();
clientMap = new HashMap<String, XmlRpcClient>();
try { try {
xmlrpcURL = Config.getInstance().getProperty("xmlrpcURL", XMLRPC_URL); final String xmlrpcURLsString = Config.getInstance().getProperty("xmlrpcURL", XMLRPC_URL);
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); final String[] xmlrpcURLsArray = xmlrpcURLsString.split(",");
config.setServerURL(new URL(xmlrpcURL)); for (final String xmlrpcURL : xmlrpcURLsArray) {
config.setEnabledForExtensions(true); final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
client = new XmlRpcClient(); config.setServerURL(new URL(xmlrpcURL));
client.setConfig(config); config.setEnabledForExtensions(true);
final XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
clientList.add(client);
}
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
throw new JSCliException(e); throw new JSCliException(e);
} }
@ -35,18 +43,25 @@ public class RpcClient {
public List<String> listMethods() throws JSCliException { public List<String> listMethods() throws JSCliException {
final List<String> methodList = new ArrayList<String>(); final List<String> methodList = new ArrayList<String>();
final List<Object> execute = execute("system.listMethods"); for (final XmlRpcClient client : clientList) {
for (final Object obj : execute) { final List<Object> execute = execute(client, "system.listMethods");
methodList.add(obj.toString()); 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; return methodList;
} }
private List<Object> execute(final String method) throws JSCliException { private List<Object> execute(final XmlRpcClient client, final String method) throws JSCliException {
return execute(method, new ArrayList<Object>()); 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 { try {
final Object execute = client.execute(method, params); final Object execute = client.execute(method, params);
final ArrayList<Object> list = new ArrayList<Object>(); 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 CASTicketProvider ticketProvider = new CASTicketProvider(console, user, runAs);
final RpcClient rpcClient = new RpcClient(ticketProvider); final RpcClient rpcClient = new RpcClient(ticketProvider);
final ScriptEngineManager engineManager = new ScriptEngineManager(); final ScriptEngineManager engineManager = new ScriptEngineManager();
completionStrings = new HashSet<String>();
engine = engineManager.getEngineByName("js"); engine = engineManager.getEngineByName("js");
engine.put("casgrantingticket", ticketProvider); engine.put("casgrantingticket", ticketProvider);
engine.put("xmlrpcclient", rpcClient); engine.put("xmlrpcclient", rpcClient);
engine.put("xmlrpcLastResult", null); engine.put("xmlrpcLastResult", null);
completionStrings = new HashSet<String>();
completionStrings.add("set");
completionStrings.add("where");
try { try {
final InputStream inputResource = getClass().getClassLoader().getResourceAsStream("js/functions.js"); final InputStream inputResource = getClass().getClassLoader().getResourceAsStream("js/functions.js");
engine.eval(new InputStreamReader(inputResource)); engine.eval(new InputStreamReader(inputResource));
@ -40,8 +42,11 @@ public class ScriptClient {
final String[] parts = method.split("\\."); final String[] parts = method.split("\\.");
if (parts.length == 2) { if (parts.length == 2) {
final String module = parts[0]; final String module = parts[0];
completionStrings.add(module);
final String function = parts[1]; final String function = parts[1];
if ("system".equals(module) || "getModuleLookup".equals(function) || "createValueObject".equals(function)) {
continue;
}
completionStrings.add(module);
final String jsFunctionIdent; final String jsFunctionIdent;
if ("delete".equals(function)) { if ("delete".equals(function)) {
jsFunctionIdent = module + "['remove']"; jsFunctionIdent = module + "['remove']";