54708ce50ec8c58f4c89b947f2200b4abc442e84
[jpim.git] / src / de / j32 / pimstuff / conduit / Config.java
1 package de.j32.pimstuff.conduit;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.util.InvalidPropertiesFormatException;
7 import java.util.Properties;
8
9 public class Config
10 {
11         static Config instance;
12         Properties config = new Properties();
13         
14         public static String get(String key, String defaultValue)
15         {
16                 load();
17                 
18                 if (instance == null)
19                         return defaultValue;
20                 else {
21                         String rv = instance.config.getProperty(key);
22                         if (rv == null)
23                                 return defaultValue;
24                         return rv;
25                 }
26         }
27
28         public static String get(String key)
29         {
30                 String rv = get(key, null);
31                 if (rv == null)
32                         throw new ConfigurationException("missing configuration parameter: " + key);
33                 return rv;
34         }
35         
36         static void load()
37         {
38                         if (instance == null)
39                                 try {
40                                         instance = new Config();
41                                 }
42                                 catch (InvalidPropertiesFormatException e) {}
43                                 catch (FileNotFoundException e) {}
44                                 catch (IOException e) {}
45         }
46         
47         Config() 
48                 throws InvalidPropertiesFormatException, FileNotFoundException, IOException
49         {
50                 config.loadFromXML(new FileInputStream("config.xml"));
51         }
52         
53 }