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