From bf28f2a75542852a0db0bfe1ce0c8deb27dbf7cf Mon Sep 17 00:00:00 2001
From: Peter Hormanns <peter.hormanns@jalin.de>
Date: Tue, 17 Jun 2014 15:22:44 +0200
Subject: [PATCH] support for multiple backends

---
 hsarjcli/src/de/hsadmin/jscli/ScriptClient.java |    9 +++-
 hsarjcli/src/de/hsadmin/jscli/RpcClient.java    |   52 +++++++++++++++++++-------
 2 files changed, 45 insertions(+), 16 deletions(-)

diff --git a/hsarjcli/src/de/hsadmin/jscli/RpcClient.java b/hsarjcli/src/de/hsadmin/jscli/RpcClient.java
index d6c6c71..3eeb63c 100644
--- a/hsarjcli/src/de/hsadmin/jscli/RpcClient.java
+++ b/hsarjcli/src/de/hsadmin/jscli/RpcClient.java
@@ -3,7 +3,9 @@
 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 @@
 
 	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 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 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);
+		}
+	}
+	
 }
diff --git a/hsarjcli/src/de/hsadmin/jscli/ScriptClient.java b/hsarjcli/src/de/hsadmin/jscli/ScriptClient.java
index 4acc603..300e5ef 100644
--- a/hsarjcli/src/de/hsadmin/jscli/ScriptClient.java
+++ b/hsarjcli/src/de/hsadmin/jscli/ScriptClient.java
@@ -24,11 +24,13 @@
 		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 @@
 			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']";

--
Gitblit v1.9.0-SNAPSHOT