View Javadoc

1   package net.sourceforge.argval.utils.impl;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.FileNotFoundException;
6   import java.io.IOException;
7   import java.util.Properties;
8   
9   import net.sourceforge.argval.utils.LocateFile;
10  import net.sourceforge.argval.utils.PropertiesLoader;
11  
12  import org.slf4j.Logger;
13  import org.slf4j.LoggerFactory;
14  
15  /**
16   * This PropertiesLoader provides a standard way for loading (test) configuration(s). It provides
17   * a static method to load the configuration as a {@link Properties} object.
18   * <p>
19   * The configuration is loaded as follows:
20   * <ul>
21   *  <li>The properties are loaded from the configuration file, but any of these properties may be
22   *  overruled by system properties.
23   *  <li>The configuration file is located as follows
24   *  <ul>
25   *    <li>If the system property <code>property-key-name-configuration-file</code> is set, that 
26   *        file is used.
27   *    <li>Otherwise, if there exist a file 'default-properties-filename' in the current working 
28   *        directory that file is used
29   *    <li>Otherwise, if there exist an application configuration directory (under the users home 
30   *        directory '${user.home}/configuration-path'), containing a default configuration file,
31   *        that file is used.
32   *    <li>Otherwise, the loading will fail with a runtime exception with a descriptive message.
33   *  </ul>
34   * </ul>
35   * 
36   * An example of using the PropertiesLoader:
37   * <iframe  src="{@docRoot}/example/net/sourceforge/argval/example/Application.html" 
38   *   width="100%"  height="400">
39   * <pre>
40   *     public Application() {
41   *         String propKeyConfigurationFile = "application-configuration"; 
42   *         String configDirName = ".application"; 
43   *         String defaultFileName = "application.properties";
44   *         propertiesLoader = new PropertiesLoaderImpl(propKeyConfigurationFile, configDirName, defaultFileName);
45   *     }
46   *
47   *     public Properties getConfiguration() throws ApplicationException {
48   *         try {
49   *             return propertiesLoader.loadConfiguration();
50   *         } 
51   *         catch (IOException ioe) {
52   *             try {
53   *             throw new ApplicationException("Unable to load configuration file " 
54   *                     + propertiesLoader.getPropertiesFile(), ioe);
55   *             } 
56   *             catch (FileNotFoundException fnfe) {
57   *             throw new ApplicationException(fnfe);
58   *             }
59   *         }
60   *     }
61   * </pre>
62   * </iframe>
63   *   
64   * @author T. Verhagen
65   */
66  public class PropertiesLoaderImpl implements PropertiesLoader {
67  	/** The logging instance. */
68      private static final Logger logger = LoggerFactory.getLogger(PropertiesLoaderImpl.class);
69      /** The location of the properties file. */
70  	private final LocateFile locateFile;
71  
72  
73      /**
74       * Creates a <code>PropertiesLoader</code> specific for an application / utility.
75       * <p>
76       * Specify the <code>propKeyConfigurationFile</code> as a property key. This key can be used 
77       * to specify the location of the configuration file, as system property / environment variable.
78       * As in:
79       * <pre>java  -Dmy-application-name.configuration.path=<the location of the properties file>
80       *   org.organisation.application.Start</pre>
81       * The <code>folderName</code> is optional, use it when the application has a default configuration folder 
82       * in the users home directory. Like <code>${user.home/.my-application-config/</code>. 
83       * The <code>defaultFileName</code> required, used to find load the default configuration file, from the 
84       * configuration application configuration directory 
85       * (<code>${user.home}/.my-application-config/defaultFileName</code>). Or the current working directory
86       * (<code>${user.dir}/defaultFileName</code>).
87       * 
88       * @param propKeyConfigurationFile - the system property key, for locating the configuration file (not <code>null</code>)
89       * @param configDirName - the applications configuration directory (when it has one) (can be <code>null</code>)
90       * @param defaultFileName - the default configuration file name (not <code>null</code>).
91       */
92      public PropertiesLoaderImpl(String propKeyConfigurationFile, String configDirName, String defaultFileName) {
93          locateFile = new PropertyLocatedFile(propKeyConfigurationFile, configDirName, defaultFileName, logger);
94      }
95  
96      /* (non-Javadoc)
97       * @see net.sourceforge.argval.utils.PropertiesLoader#loadConfiguration()
98       */
99      public Properties loadConfiguration() throws IOException {
100     	return loadConfiguration(Boolean.TRUE);
101     }
102     
103     /* (non-Javadoc)
104      * @see net.sourceforge.argval.utils.PropertiesLoader#loadConfiguration(Boolean includeSystemProperties)
105      */
106     public Properties loadConfiguration(Boolean includeSystemProperties) throws IOException
107     {
108         final File file = getPropertiesFile();
109         logger.info("Reading configuration from '" + file.getAbsolutePath() + "'");
110         Properties configProp = new Properties();
111         configProp.load(new FileInputStream(file));
112         
113         logger.debug("includeSystemProperties " + includeSystemProperties);
114         if (includeSystemProperties != null && includeSystemProperties == Boolean.TRUE) {
115         	// System properties overrule properties from the configuration file
116 //        	Properties newConfigProp = new Properties(configProp);
117 //        	newConfigProp.putAll(System.getProperties());
118 //        	configProp = newConfigProp;
119         	configProp.putAll(System.getProperties());
120         }
121         return configProp;
122     }
123 
124     
125     public File getPropertiesFile() throws FileNotFoundException {
126     	return locateFile.getFile();
127     }
128 
129     
130     public String getPropertiesFileMessage() {
131     	return locateFile.getFileMessage();
132     }
133     
134 }