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();
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);
- }
- }
- */
}
* 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;
* 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));
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());
/** 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
/**
* 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;
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);
}
}
}
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 */
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 "+
"<osrfConfigFile> <service> <method> [<JSONparam1>, <JSONparam2>]");
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<Object> params = new ArrayList<Object>();
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.");
}
}
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]));
}
}
--- /dev/null
+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);
+ }
+}
*/
private String context;
+ public static Config global() {
+ return config;
+ }
+
+
/**
* @param context The config context
*/
* 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;
}
}
}
+ 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) {
* 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) {
* @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;
* 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);
--- /dev/null
+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();
+ }
+ }
+}
+