View Javadoc
1   package edu.asu.cri.MirkE.util;
2   
3   import java.io.File;
4   import java.io.InputStream;
5   import java.net.URL;
6   import java.util.Properties;
7   
8   /***
9    * This is a kind of singleton class for holding all the application level
10   * properties configured for MirkE
11   *  
12   * @author Siva B.S.D.S
13   * @version $Id: MirkeProperties.java,v 1.2 2005/01/11 04:03:57 smenor Exp $
14   */
15  public class MirkeProperties
16  {
17      /***
18       * Name of the mirke properties file
19       */
20      private static final String PROPERTIES_FILE_NAME = "mirke.properties";
21  
22      /***
23       * Time of the properties loaded
24       */
25      private static long loadedTime = 0;
26  
27      /***
28       * Properties file instance
29       */
30      private static File propertiesFile = null;
31  
32      /***
33       * Properties instance
34       */
35      private static Properties properties = null;
36  
37      /***
38       * This method loads all the properties from "mirke.properties"
39       */
40      private static void loadProperties()
41      {
42          URL resource = null;
43          try
44          {
45              //Getting the resource from class path
46              resource =
47                  (new Thread()).getContextClassLoader().getResource(
48                      PROPERTIES_FILE_NAME);
49  
50              //Getting the absolute file (To get the last modified time)
51              propertiesFile = new File(resource.getFile());
52  
53              //Instantiating/clearing the properties
54              if (properties == null)
55              {
56                  properties = new Properties();
57              }
58              else
59              {
60                  properties.clear();
61              }
62  
63              InputStream resourceInputStream = resource.openStream();
64              
65              //Loading the properties from the file 
66              //properties.load(new FileInputStream(propertiesFile));
67              properties.load(resourceInputStream);
68              
69              //Getting the last modified time from the file
70              loadedTime = propertiesFile.lastModified();
71          }
72          catch (Throwable exception)
73          {
74              exception.printStackTrace();
75          }
76      }
77  
78      /***
79       * This method checks if the properties file is modified after loading. 
80       * If it is modified, this will reload the modified properties file. 
81       */
82      private static void checkPropertiesChanged()
83      {
84          if (propertiesFile == null)
85          {
86              loadProperties();
87          }
88          else if (loadedTime != propertiesFile.lastModified())
89          {
90              loadProperties();
91          }
92      }
93  
94      /***
95       * Returns the property value for the given key
96       * 
97       * @param propertyKey property key
98       * @return property value
99       */
100     public static String getProperty(String propertyKey)
101     {
102         checkPropertiesChanged();
103         return properties.getProperty(propertyKey);
104     }
105 }