View Javadoc

1   /*
2    * Created on May 19, 2005
3    *
4    * The default implementation of PropertyManager
5    */
6   package net.sf.tlc.core.impl;
7   
8   import java.io.File;
9   import java.io.FileInputStream;
10  import java.io.FileNotFoundException;
11  import java.io.IOException;
12  import java.util.Properties;
13  import java.util.logging.Level;
14  import java.util.logging.Logger;
15  
16  import net.sf.tlc.core.PropertyManager;
17  
18  /***
19   * The default implementation of PropertyManager
20   * 
21   * @author aisrael
22   */
23  public final class DefaultPropertyManager implements PropertyManager {
24      
25      private static final Logger logger = Logger.getLogger(DefaultPropertyManager.class.getName());
26  
27      private final Properties properties;
28  
29      /***
30       * @param file
31       *            File
32       */
33      public DefaultPropertyManager(final File file) {
34          properties = new Properties();
35          try {
36              properties.load(new FileInputStream(file));
37          } catch (FileNotFoundException e) {
38              logger.log(Level.WARNING, "File not found: \"" + file.getName() + "\"", e);
39          } catch (IOException e) {
40              logger.log(Level.SEVERE, "IOException", e);
41          }
42      }
43  
44      /***
45       * @param propertyFile
46       *            String
47       */
48      public DefaultPropertyManager(final String propertyFile) {
49          this(new File(propertyFile));
50      }
51  
52      /***
53       * (non-Javadoc)
54       * 
55       * @see net.sf.tlc.core.PropertyManager#getProperties()
56       */
57      public Properties getProperties() {
58          return this.properties;
59      }
60  
61      /***
62       * (non-Javadoc)
63       * 
64       * @see net.sf.tlc.core.PropertyManager#getProperty(java.lang.String)
65       */
66      public String getProperty(final String key) {
67          return this.properties.getProperty(key);
68      }
69  
70      /***
71       * (non-Javadoc)
72       * @see net.sf.tlc.core.PropertyManager#getProperty(java.lang.String, java.lang.String)
73       */
74      public String getProperty(final String key, final String defaultValue) {
75          String result = getProperty(key);
76          if (null == result) {
77              result = defaultValue;
78          }
79          return result;
80      }
81  
82  }