]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/util/SettingsClient.java
added a settings server client and test. made the global config object wrapped in...
[OpenSRF.git] / src / java / org / opensrf / util / SettingsClient.java
1 package org.opensrf.util;
2 import org.opensrf.*;
3 import java.util.Map;
4
5 /**
6  * Connects to the OpenSRF Settings server to fetch the settings config.  
7  * Provides a Config interface for fetching settings via path
8  */
9 public class SettingsClient extends Config {
10
11     /** Singleton SettingsClient instance */
12     private static SettingsClient client = new SettingsClient();
13
14     public SettingsClient() {
15         super("");
16     }
17
18     /**
19      * @return The global settings client instance 
20      */
21     public static SettingsClient instance() throws ConfigException {
22         if(client.getConfigObject() == null) 
23             client.fetchConfig();
24         return client;
25     }
26
27     /**
28      * Fetches the settings object from the settings server
29      */
30     private void fetchConfig() throws ConfigException {
31
32         ClientSession ses = new ClientSession("opensrf.settings");
33         try {
34
35             Request req = ses.request(
36                 "opensrf.settings.host_config.get", 
37                 new String[]{(String)Config.global().getFirst("/domains/domain")});
38     
39             Result res = req.recv(12000);
40             if(res == null) {
41                 /** throw exception */
42             }
43             setConfigObject((Map) res.getContent());
44
45         } catch(Exception e) {
46             throw new ConfigException("Error fetching settings config", e);
47
48         } finally {
49             ses.cleanup();
50         }
51     }
52 }
53