1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.whatsnew.config;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22
23 import java.util.MissingResourceException;
24 import java.util.Properties;
25 import java.util.ResourceBundle;
26
27
28 /***
29 * <p>
30 * Config file wrapper. Supports user given properties file
31 * </p>
32 *
33 * @author <a href="mailto:dquintela@users.sourceforge.net">Diogo Quintela</a>
34 * @version $Id: Config.java,v 1.1 2004/05/13 01:22:36 dquintela Exp $
35 */
36 public class Config {
37 /*** Bundle name/location */
38 private static final String CONFIG_NAME = "net.sf.whatsnew.config.config";
39
40 /*** Resource bundle for config options */
41 private static final ResourceBundle MESSAGES_BUNDLE = ResourceBundle
42 .getBundle(CONFIG_NAME);
43
44 /*** Resource bundle for user set options! Overrides default options */
45 private Properties confProps;
46
47 /***
48 * Creates a new Config object.
49 */
50 public Config() {
51 confProps = new Properties();
52 }
53
54 /***
55 * Creates a new Config object.
56 *
57 * @param configProperties The external properties files to load
58 *
59 * @throws FileNotFoundException If file doesn't exist
60 * @throws IOException in case of IO error
61 */
62 public Config(File configProperties)
63 throws FileNotFoundException, IOException {
64 this();
65 confProps.load(new FileInputStream(configProperties));
66 }
67
68 /***
69 * Obtain config entry based on key value
70 *
71 * @param key The key to lookup for
72 *
73 * @return The config entry
74 */
75 public String getString(String key) {
76 try {
77 return confProps.getProperty(key, MESSAGES_BUNDLE.getString(key));
78 } catch (MissingResourceException e) {
79 return '!' + key + '!';
80 }
81 }
82 }
83