]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/util/Config.java
implemented client bootstrap. created a status object to model connection statuses...
[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     /** 
20      * The log parsing context.  This is used as a prefix to the
21      * config item search path.  This allows config XML chunks to 
22      * be inserted into arbitrary XML files.
23      */
24     private String context;
25
26     /**
27      * @param context The config context
28      */
29     public Config(String context) {
30         this.context = context;
31     }
32
33     /**
34      * Sets the global config object.
35      * @param c The config object to use.
36      */
37     public static void setConfig(Config c) {
38         config = c;
39     }
40
41     /**
42      * Parses an XML config file.
43      * @param filename The path to the file to parse.
44      */
45     public void parse(String filename) throws ConfigException {
46         try {
47             String xml = Utils.fileToString(filename);
48             JSONObject jobj = XML.toJSONObject(xml);
49             configObject = (Map) new JSONReader(jobj.toString()).readObject();
50         } catch(Exception e) {
51             throw new ConfigException("Error parsing config", e);
52         }
53     }
54
55     /**
56      * Returns the configuration value found at the requested path.
57      * @param path The search path
58      * @return The config value, or null if no value exists at the given path.  
59      * @throws ConfigException thrown if nothing is found at the path
60      */
61     public static String getString(String path) throws ConfigException {
62         try {
63             return (String) get(path);
64         } catch(Exception e) {
65             throw new 
66                 ConfigException("No config string found at " + path);
67         }
68     }
69
70     /**
71      * Gets the int value at the given path
72      * @param path The search path
73      */
74     public static int getInt(String path) throws ConfigException {
75         try {
76             return Integer.parseInt(getString(path));
77         } catch(Exception e) {
78             throw new
79                 ConfigException("No config int found at " + path);
80         }
81     }
82
83     /**
84      * Returns the configuration object found at the requested path.
85      * @param path The search path
86      * @return The config value
87      * @throws ConfigException thrown if nothing is found at the path
88      */
89     public static Object get(String path) throws ConfigException {
90         try {
91             Object obj = Utils.findPath(config.configObject, config.context + path);
92             if(obj == null)
93                 throw new ConfigException("");
94             return obj;
95         } catch(Exception e) {
96             e.printStackTrace();
97             throw new ConfigException("No config object found at " + path);
98         }
99     }
100
101     /**
102      * Returns the first item in the list found at the given path.  If
103      * no list is found, ConfigException is thrown.
104      * @param path The search path
105      */
106     public static Object getFirst(String path) throws ConfigException {
107         Object obj = get(path); 
108         if(obj instanceof List) 
109             return ((List) obj).get(0);
110         return obj;
111     }
112
113
114     /**
115      * Returns the config as a JSON string
116      */
117     public String toString() {
118         return new JSONWriter(configObject).write();
119     }
120 }
121