From ea1d30eb5b2c473917a220699b3c39eca16e3af5 Mon Sep 17 00:00:00 2001 From: erickson Date: Wed, 16 May 2007 17:27:56 +0000 Subject: [PATCH] added a settings server client and test. made the global config object wrapped in a method instead of using static methods all around git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@891 9efc2488-bf62-4759-914b-345cdb29e865 --- src/java/org/opensrf/ClientSession.java | 35 +----------------- src/java/org/opensrf/Request.java | 2 +- src/java/org/opensrf/Session.java | 2 +- src/java/org/opensrf/Stack.java | 18 ++++++--- src/java/org/opensrf/Sys.java | 8 ++-- src/java/org/opensrf/test/TestClient.java | 34 ++++++++++------- src/java/org/opensrf/test/TestConfig.java | 2 +- src/java/org/opensrf/test/TestSettings.java | 16 ++++++++ src/java/org/opensrf/util/Config.java | 30 ++++++++++++--- src/java/org/opensrf/util/SettingsClient.java | 53 +++++++++++++++++++++++++++ 10 files changed, 135 insertions(+), 65 deletions(-) create mode 100644 src/java/org/opensrf/test/TestSettings.java create mode 100644 src/java/org/opensrf/util/SettingsClient.java diff --git a/src/java/org/opensrf/ClientSession.java b/src/java/org/opensrf/ClientSession.java index 27935ab..cc68505 100644 --- a/src/java/org/opensrf/ClientSession.java +++ b/src/java/org/opensrf/ClientSession.java @@ -42,8 +42,8 @@ public class ClientSession extends Session { this.service = service; /** generate the remote node string */ - domain = (String) Config.getFirst("/domains/domain"); - router = Config.getString("/router_name"); + domain = (String) Config.global().getFirst("/domains/domain"); + router = Config.global().getString("/router_name"); setRemoteNode(router + "@" + domain + "/" + service); origRemoteNode = getRemoteNode(); @@ -145,36 +145,5 @@ public class ClientSession extends Session { if(req == null) return; req.setComplete(); } - - - /** - * Connects to the OpenSRF network so that client sessions may communicate. - * @param configFile The OpenSRF config file - * @param configContext Where in the XML document the config chunk lives. This - * allows an OpenSRF client config chunk to live in XML files where other config - * information lives. - */ - /* - public static void bootstrap(String configFile, String configContext) - throws ConfigException, SessionException { - - Config config = new Config(configContext); - config.parse(configFile); - Config.setConfig(config); - - String username = Config.getString("/username"); - String passwd = Config.getString("/passwd"); - String host = (String) Config.getFirst("/domains/domain"); - int port = Config.getInt("/port"); - - try { - XMPPSession xses = new XMPPSession(host, port); - xses.connect(username, passwd, "test-java"); - XMPPSession.setGlobalSession(xses); - } catch(XMPPException e) { - throw new SessionException("Unable to bootstrap client", e); - } - } - */ } diff --git a/src/java/org/opensrf/Request.java b/src/java/org/opensrf/Request.java index 1222999..e5d0ef6 100644 --- a/src/java/org/opensrf/Request.java +++ b/src/java/org/opensrf/Request.java @@ -69,7 +69,7 @@ public class Request { * negative, this method will wait indefinitely. * @return The result or null if none arrives in time */ - public Result recv(long millis) throws SessionException { + public Result recv(long millis) throws SessionException, MethodException { Result result = null; diff --git a/src/java/org/opensrf/Session.java b/src/java/org/opensrf/Session.java index 3dc81f9..e2b41c3 100644 --- a/src/java/org/opensrf/Session.java +++ b/src/java/org/opensrf/Session.java @@ -60,7 +60,7 @@ public abstract class Session { * all received messages to the stack for processing * @param millis The number of milliseconds to wait for a message to arrive */ - public static void waitForMessage(long millis) throws SessionException { + public static void waitForMessage(long millis) throws SessionException, MethodException { try { Stack.processXMPPMessage( XMPPSession.getGlobalSession().recv(millis)); diff --git a/src/java/org/opensrf/Stack.java b/src/java/org/opensrf/Stack.java index 89649a5..36dba52 100644 --- a/src/java/org/opensrf/Stack.java +++ b/src/java/org/opensrf/Stack.java @@ -8,10 +8,12 @@ import java.util.Iterator; public class Stack { - public static void processXMPPMessage(XMPPMessage msg) { + public static void processXMPPMessage(XMPPMessage msg) throws MethodException { if(msg == null) return; + //System.out.println(msg.getBody()); + /** fetch this session from the cache */ Session ses = Session.findCachedSession(msg.getThread()); @@ -55,7 +57,7 @@ public class Stack { /** LOG the duration */ } - private static void processOSRFMessage(Session ses, Message msg) { + private static void processOSRFMessage(Session ses, Message msg) throws MethodException { if( ses instanceof ClientSession ) processResponse((ClientSession) ses, msg); else @@ -65,8 +67,9 @@ public class Stack { /** * Process a server response */ - private static void processResponse(ClientSession session, Message msg) { + private static void processResponse(ClientSession session, Message msg) throws MethodException { String type = msg.getType(); + if(msg.RESULT.equals(type)) { session.pushResponse(msg); return; @@ -79,8 +82,13 @@ public class Stack { int statusCode = stat.getStatusCode(); String status = stat.getStatus(); - if(statusCode == stat.COMPLETE) { - session.setRequestComplete(msg.getId()); + switch(statusCode) { + case Status.COMPLETE: + session.setRequestComplete(msg.getId()); + break; + case Status.NOTFOUND: + session.setRequestComplete(msg.getId()); + throw new MethodException(status); } } } diff --git a/src/java/org/opensrf/Sys.java b/src/java/org/opensrf/Sys.java index e403879..857008d 100644 --- a/src/java/org/opensrf/Sys.java +++ b/src/java/org/opensrf/Sys.java @@ -22,10 +22,10 @@ public class Sys { Config.setConfig(config); /* set this as the global config */ /** Collect the network connection info from the config */ - String username = Config.getString("/username"); - String passwd = Config.getString("/passwd"); - String host = (String) Config.getFirst("/domains/domain"); - int port = Config.getInt("/port"); + String username = config.getString("/username"); + String passwd = config.getString("/passwd"); + String host = (String) config.getFirst("/domains/domain"); + int port = config.getInt("/port"); try { /** Connect to the Jabber network */ diff --git a/src/java/org/opensrf/test/TestClient.java b/src/java/org/opensrf/test/TestClient.java index df6db94..2625053 100644 --- a/src/java/org/opensrf/test/TestClient.java +++ b/src/java/org/opensrf/test/TestClient.java @@ -9,22 +9,20 @@ import java.io.PrintStream; public class TestClient { + public static void main(String args[]) throws Exception { - + PrintStream out = System.out; - String service; - String method; - - try { - Sys.bootstrapClient(args[0], "/config/opensrf"); - service = args[1]; - method = args[2]; - } catch(ArrayIndexOutOfBoundsException e) { + if(args.length < 3) { out.println( "usage: org.opensrf.test.TestClient "+ " [, ]"); return; } + Sys.bootstrapClient(args[0], "/config/opensrf"); + String service = args[1]; + String method = args[2]; + /** build the client session and send the request */ ClientSession session = new ClientSession(service); List params = new ArrayList(); @@ -33,16 +31,24 @@ public class TestClient { for(int i = 3; i < args.length; i++) /* add the params */ params.add(new JSONReader(args[i]).read()); - Request request = session.request(method, params); Result result; + long start = new Date().getTime(); - while( (result = request.recv(60000)) != null ) { - out.println("status = " + result.getStatus()); - out.println("status code = " + result.getStatusCode()); + Request request = session.request(method, params); + + while( (result = request.recv(60000)) != null ) { + /** loop over the results and print the JSON version of the content */ + + if(result.getStatusCode() != 200) { /* make sure the request succeeded */ + out.println("status = " + result.getStatus()); + out.println("status code = " + result.getStatusCode()); + continue; + } + out.println("result JSON: " + new JSONWriter(result.getContent()).write()); } - out.println("Request took: " + (new Date().getTime() - start)); + out.println("Request round trip took: " + (new Date().getTime() - start) + " ms."); } } diff --git a/src/java/org/opensrf/test/TestConfig.java b/src/java/org/opensrf/test/TestConfig.java index 5cd4eb3..f65a84f 100644 --- a/src/java/org/opensrf/test/TestConfig.java +++ b/src/java/org/opensrf/test/TestConfig.java @@ -11,6 +11,6 @@ public class TestConfig { System.out.println(""); for(int i = 1; i < args.length; i++) - System.out.println("Found config value: " + args[i] + ": " + Config.get(args[i])); + System.out.println("Found config value: " + args[i] + ": " + Config.global().get(args[i])); } } diff --git a/src/java/org/opensrf/test/TestSettings.java b/src/java/org/opensrf/test/TestSettings.java new file mode 100644 index 0000000..d176acf --- /dev/null +++ b/src/java/org/opensrf/test/TestSettings.java @@ -0,0 +1,16 @@ +package org.opensrf.test; +import org.opensrf.*; +import org.opensrf.util.*; + +public class TestSettings { + public static void main(String args[]) throws Exception { + Sys.bootstrapClient(args[0], "/config/opensrf"); + SettingsClient client = SettingsClient.instance(); + //Object obj = client.get("/apps"); + //System.out.println(new JSONWriter(obj).write()); + String lang = client.getString("/apps/opensrf.settings/language"); + String impl = client.getString("/apps/opensrf.settings/implementation"); + System.out.println("opensrf.settings language = " + lang); + System.out.println("opensrf.settings implementation = " + impl); + } +} diff --git a/src/java/org/opensrf/util/Config.java b/src/java/org/opensrf/util/Config.java index 00b703a..ddac9c0 100644 --- a/src/java/org/opensrf/util/Config.java +++ b/src/java/org/opensrf/util/Config.java @@ -23,6 +23,11 @@ public class Config { */ private String context; + public static Config global() { + return config; + } + + /** * @param context The config context */ @@ -34,7 +39,7 @@ public class Config { * Sets the global config object. * @param c The config object to use. */ - public static void setConfig(Config c) { + public static void setGlobalConfig(Config c) { config = c; } @@ -52,13 +57,26 @@ public class Config { } } + public static void setConfig(Config conf) { + config = conf; + } + + public void setConfigObject(Map config) { + this.configObject = config; + } + + protected Map getConfigObject() { + return this.configObject; + } + + /** * Returns the configuration value found at the requested path. * @param path The search path * @return The config value, or null if no value exists at the given path. * @throws ConfigException thrown if nothing is found at the path */ - public static String getString(String path) throws ConfigException { + public String getString(String path) throws ConfigException { try { return (String) get(path); } catch(Exception e) { @@ -71,7 +89,7 @@ public class Config { * Gets the int value at the given path * @param path The search path */ - public static int getInt(String path) throws ConfigException { + public int getInt(String path) throws ConfigException { try { return Integer.parseInt(getString(path)); } catch(Exception e) { @@ -86,9 +104,9 @@ public class Config { * @return The config value * @throws ConfigException thrown if nothing is found at the path */ - public static Object get(String path) throws ConfigException { + public Object get(String path) throws ConfigException { try { - Object obj = Utils.findPath(config.configObject, config.context + path); + Object obj = Utils.findPath(configObject, context + path); if(obj == null) throw new ConfigException(""); return obj; @@ -103,7 +121,7 @@ public class Config { * no list is found, ConfigException is thrown. * @param path The search path */ - public static Object getFirst(String path) throws ConfigException { + public Object getFirst(String path) throws ConfigException { Object obj = get(path); if(obj instanceof List) return ((List) obj).get(0); diff --git a/src/java/org/opensrf/util/SettingsClient.java b/src/java/org/opensrf/util/SettingsClient.java new file mode 100644 index 0000000..cd70ebb --- /dev/null +++ b/src/java/org/opensrf/util/SettingsClient.java @@ -0,0 +1,53 @@ +package org.opensrf.util; +import org.opensrf.*; +import java.util.Map; + +/** + * Connects to the OpenSRF Settings server to fetch the settings config. + * Provides a Config interface for fetching settings via path + */ +public class SettingsClient extends Config { + + /** Singleton SettingsClient instance */ + private static SettingsClient client = new SettingsClient(); + + public SettingsClient() { + super(""); + } + + /** + * @return The global settings client instance + */ + public static SettingsClient instance() throws ConfigException { + if(client.getConfigObject() == null) + client.fetchConfig(); + return client; + } + + /** + * Fetches the settings object from the settings server + */ + private void fetchConfig() throws ConfigException { + + ClientSession ses = new ClientSession("opensrf.settings"); + try { + + Request req = ses.request( + "opensrf.settings.host_config.get", + new String[]{(String)Config.global().getFirst("/domains/domain")}); + + Result res = req.recv(12000); + if(res == null) { + /** throw exception */ + } + setConfigObject((Map) res.getContent()); + + } catch(Exception e) { + throw new ConfigException("Error fetching settings config", e); + + } finally { + ses.cleanup(); + } + } +} + -- 2.11.0