KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 public class Configuration {
12     private String JavaDoc resource;
13
14     /**
15      * Load resource <code>/jiapi.properties</code>
16      */

17     public Configuration() {
18         this.resource = "/jiapi.properties";
19     }
20
21     public Configuration(String JavaDoc resource) {
22         this.resource = resource;
23     }
24
25
26     public boolean getBoolean(String JavaDoc key, boolean deflt) {
27         if (key == null) {
28             return deflt;
29         }
30
31         Properties JavaDoc p = getProperties(resource);
32         String JavaDoc s = p.getProperty(key);
33
34         boolean b = deflt;
35         try {
36             b = Boolean.valueOf(s).booleanValue();
37         }
38         catch(Exception JavaDoc e) {
39         }
40         
41         return b;
42     }
43
44
45     public int getInt(String JavaDoc key, int deflt) {
46         if (key == null) {
47             return deflt;
48         }
49
50         Properties JavaDoc p = getProperties(resource);
51         String JavaDoc s = p.getProperty(key);
52
53         int i = deflt;
54         try {
55             i = Integer.valueOf(s).intValue();
56         }
57         catch(Exception JavaDoc e) {
58         }
59         
60         return i;
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                 System.out.println("Could not find resource: " + name);
74             }
75         }
76         catch (Exception JavaDoc e) {
77             System.out.println("Failed to get property file " + name +
78                                ", " + e);
79         }
80
81         return properties;
82     }
83 }
84
Popular Tags