]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/util/Config.java
implemented enough of the stack/session logic to make stateless requests and receive...
[OpenSRF.git] / src / java / org / opensrf / util / Config.java
1 package org.opensrf.util;
2
3 import org.json.*;
4 import java.util.Map;
5 import java.util.List;
6
7
8 /**
9  * Config reader and accesor module.  This module reads an XML config file,
10  * then loads the file into an internal config, whose values may be accessed
11  * by xpath-style lookup paths.
12  */
13 public class Config {
14
15     /** The globl config instance */
16     private static Config config;
17     /** The object form of the parsed config */
18     private Map configObject;
19     private String context;
20
21     public Config(String context) {
22         this.context = context;
23     }
24
25     /**
26      * Sets the global config object.
27      * @param c The config object to use.
28      */
29     public static void setConfig(Config c) {
30         config = c;
31     }
32
33     /**
34      * Parses an XML config file.
35      * @param filename The path to the file to parse.
36      */
37     public void parse(String filename) throws Exception {
38         String xml = Utils.fileToString(filename);
39         JSONObject jobj = XML.toJSONObject(xml);
40         configObject = (Map) new JSONReader(jobj.toString()).readObject();
41     }
42
43     /**
44      * Returns the configuration value found at the requested path.
45      * @see org.opensrf.util.Utils.findPath for path description.
46      * @param path The search path
47      * @return The config value, or null if no value exists at the given path.  
48      * @throws ConfigException thrown if nothing is found at the path
49      */
50     public static String getString(String path) throws ConfigException {
51         try {
52             return (String) get(path);
53         } catch(Exception e) {
54             throw new 
55                 ConfigException("No config string found at " + path);
56         }
57     }
58
59     public static int getInt(String path) throws ConfigException {
60         return Integer.parseInt(getString(path));
61     }
62
63     /**
64      * Returns the configuration object found at the requested path.
65      * @see org.opensrf.util.Utils.findPath for path description.
66      * @param path The search path
67      * @return The config value
68      * @throws ConfigException thrown if nothing is found at the path
69      */
70     public static Object get(String path) throws ConfigException {
71         try {
72             Object obj = Utils.findPath(config.configObject, config.context + path);
73             if(obj == null)
74                 throw new ConfigException("");
75             return obj;
76         } catch(Exception e) {
77             e.printStackTrace();
78             throw new ConfigException("No config object found at " + path);
79         }
80     }
81
82     public static Object getFirst(String path) throws ConfigException {
83         Object obj = get(path); 
84         if(obj instanceof List) 
85             return ((List) obj).get(0);
86         return obj;
87     }
88
89
90     /**
91      * Returns the config as a JSON string
92      */
93     public String toString() {
94         return new JSONWriter(configObject).write();
95     }
96 }
97