]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/util/Config.java
fixed some doc strings. added sample makefile
[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 Exception {
46         String xml = Utils.fileToString(filename);
47         JSONObject jobj = XML.toJSONObject(xml);
48         configObject = (Map) new JSONReader(jobj.toString()).readObject();
49     }
50
51     /**
52      * Returns the configuration value found at the requested path.
53      * @param path The search path
54      * @return The config value, or null if no value exists at the given path.  
55      * @throws ConfigException thrown if nothing is found at the path
56      */
57     public static String getString(String path) throws ConfigException {
58         try {
59             return (String) get(path);
60         } catch(Exception e) {
61             throw new 
62                 ConfigException("No config string found at " + path);
63         }
64     }
65
66     /**
67      * Gets the int value at the given path
68      * @param path The search path
69      */
70     public static int getInt(String path) throws ConfigException {
71         try {
72             return Integer.parseInt(getString(path));
73         } catch(Exception e) {
74             throw new
75                 ConfigException("No config int found at " + path);
76         }
77     }
78
79     /**
80      * Returns the configuration object found at the requested path.
81      * @param path The search path
82      * @return The config value
83      * @throws ConfigException thrown if nothing is found at the path
84      */
85     public static Object get(String path) throws ConfigException {
86         try {
87             Object obj = Utils.findPath(config.configObject, config.context + path);
88             if(obj == null)
89                 throw new ConfigException("");
90             return obj;
91         } catch(Exception e) {
92             e.printStackTrace();
93             throw new ConfigException("No config object found at " + path);
94         }
95     }
96
97     /**
98      * Returns the first item in the list found at the given path.  If
99      * no list is found, ConfigException is thrown.
100      * @param path The search path
101      */
102     public static Object getFirst(String path) throws ConfigException {
103         Object obj = get(path); 
104         if(obj instanceof List) 
105             return ((List) obj).get(0);
106         return obj;
107     }
108
109
110     /**
111      * Returns the config as a JSON string
112      */
113     public String toString() {
114         return new JSONWriter(configObject).write();
115     }
116 }
117