KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > file > Configuration


1 package alt.jiapi.file;
2
3 import java.io.InputStream JavaDoc;
4 import java.util.Properties JavaDoc;
5
6 import org.apache.log4j.Category;
7
8 /**
9  * Class Configuration.
10  *
11  * @author Mika Riekkinen
12  */

13 class Configuration {
14     private static final Category log = Category.getInstance(Configuration.class);
15     private String JavaDoc resource;
16
17     Configuration() {
18         this.resource = "/jiapi.properties";
19     }
20
21     Configuration(String JavaDoc resource) {
22         this.resource = resource;
23     }
24
25
26     boolean getBoolean(String JavaDoc key) {
27         if (key == null) {
28             return false;
29         }
30
31         Properties JavaDoc p = getProperties(resource);
32         String JavaDoc s = p.getProperty(key);
33
34         boolean b = false;
35         try {
36             b = Boolean.valueOf(s).booleanValue();
37         }
38         catch(Exception JavaDoc e) {
39         }
40         
41         return b;
42     }
43
44
45     boolean getBoolean(String JavaDoc key, boolean dflt) {
46         if (key == null) {
47             return dflt;
48         }
49
50         Properties JavaDoc p = getProperties(resource);
51         String JavaDoc s = p.getProperty(key);
52
53         boolean b = dflt;
54         try {
55             b = Boolean.valueOf(s).booleanValue();
56         }
57         catch(Exception JavaDoc e) {
58         }
59         
60         return b;
61     }
62
63
64     private Properties JavaDoc getProperties(String JavaDoc name) {
65         Properties JavaDoc properties = new Properties JavaDoc();
66
67         try {
68             InputStream JavaDoc is = getClass().getResourceAsStream(name);
69             if (is != null) {
70                 properties.load(is);
71             }
72             else {
73                 log.warn("Could not find resource: " + name);
74             }
75         }
76         catch (Exception JavaDoc e) {
77             log.error("Failed to get property file " + name + ", " + e);
78         }
79
80         return properties;
81     }
82 }
83
Popular Tags