KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > ac > roe > antigen > utils > Config


1 /*
2  * Created on 29-Dec-2004
3  */

4 package uk.ac.roe.antigen.utils;
5
6 import java.io.IOException JavaDoc;
7 import java.util.Properties JavaDoc;
8 import java.util.logging.Logger JavaDoc;
9
10 /**
11  * Make Properties globally available by wrapping a Properties with a static class
12  * @author jdt
13  */

14 public class Config {
15     /**
16      * Logger for this class
17      */

18     private static final Logger JavaDoc logger = Logger.getLogger(Config.class.getName());
19
20
21     private static Properties JavaDoc props = new Properties JavaDoc();
22     public static void load(String JavaDoc path) {
23         logger.fine("Loading properties off classpath from "+path);
24         try {
25             props.load(Config.class.getResourceAsStream(path));
26         } catch (Exception JavaDoc e) {
27             logger.severe("Unable to load configuration file ...aborting.");
28             throw new RuntimeException JavaDoc("Unable to load properties",e);
29         }
30     }
31     /**
32      * Returns null if the property is the empty string
33      * @param name property key
34      * @return the property with key name
35      */

36     public static String JavaDoc getProperty(String JavaDoc name) {
37         String JavaDoc property = props.getProperty(name);
38         if (property=="") property=null;
39         return property;
40     }
41     /**
42      * Returns the property from the props file, or the default if the property is null or the empty string
43      * @param name property key
44      * @param defolt default value
45      * @return property value
46      */

47     public static String JavaDoc getProperty(String JavaDoc name, String JavaDoc defolt) {
48         String JavaDoc value = getProperty(name);
49         return value !=null ? value : defolt;
50     }
51     /**
52      * Add in a property. And why not.
53      * @param key key
54      * @param value value
55      */

56     public static void setProperty(String JavaDoc key, String JavaDoc value) {
57         props.setProperty(key, value);
58     }
59
60 }
61
Popular Tags