Coverage Report - us.daveread.basicquery.util.Configuration
 
Classes in this File Line Coverage Branch Coverage Complexity
Configuration
80%
43/54
100%
8/8
2.625
 
 1  
 package us.daveread.basicquery.util;
 2  
 
 3  
 import java.io.BufferedReader;
 4  
 import java.io.File;
 5  
 import java.io.FileInputStream;
 6  
 import java.io.FileOutputStream;
 7  
 import java.io.FileReader;
 8  
 import java.io.FileWriter;
 9  
 import java.io.PrintWriter;
 10  
 import java.util.Properties;
 11  
 
 12  
 import org.apache.log4j.Logger;
 13  
 
 14  
 /**
 15  
  * <p>
 16  
  * Title: Configuration management
 17  
  * </p>
 18  
  * 
 19  
  * <p>
 20  
  * Description:
 21  
  * </p>
 22  
  * 
 23  
  * <p>
 24  
  * Copyright: Copyright (c) 2006-2014
 25  
  * </p>
 26  
  * 
 27  
  * <p>
 28  
  * Company:
 29  
  * </p>
 30  
  * 
 31  
  * @author David Read
 32  
  */
 33  
 public class Configuration extends Properties {
 34  
   /**
 35  
    * Serial UID
 36  
    */
 37  
   private static final long serialVersionUID = 8439104437125048420L;
 38  
 
 39  
   /**
 40  
    * Instance of this class - Singleton
 41  
    */
 42  
   private static Configuration config;
 43  
 
 44  
   /**
 45  
    * Logger
 46  
    */
 47  1
   private static final Logger LOGGER = Logger.getLogger(Configuration.class);
 48  
 
 49  
   /**
 50  
    * Sub-directory for the configuration files
 51  
    */
 52  
   private static final String PROPERTIES_DIRECTORY = "BasicQuery";
 53  
 
 54  
   /**
 55  
    * Property file name
 56  
    */
 57  
   private static final String FILENAME_PROPERTIES = "BasicQuery.Properties";
 58  
 
 59  
   /**
 60  
    * System property containing the user's home directory
 61  
    */
 62  
   private static final String PROP_SYSTEM_USERHOMEDIR = "user.home";
 63  
 
 64  
   /**
 65  
    * The user's home directory
 66  
    */
 67  
   private static String userHomeDirectory;
 68  
 
 69  
   /**
 70  
    * Setup the configuration instance
 71  
    */
 72  1
   private Configuration() {
 73  1
     userHomeDirectory = System.getProperty(PROP_SYSTEM_USERHOMEDIR);
 74  
 
 75  1
     if (userHomeDirectory == null) {
 76  0
       userHomeDirectory = PROPERTIES_DIRECTORY;
 77  
     } else {
 78  1
       userHomeDirectory += "/" + PROPERTIES_DIRECTORY;
 79  1
       new File(userHomeDirectory).mkdirs();
 80  
     }
 81  1
   }
 82  
 
 83  
   /**
 84  
    * Get the instance of this class
 85  
    * 
 86  
    * @return The Singleton instance
 87  
    */
 88  
   public static synchronized Configuration instance() {
 89  3
     if (config == null) {
 90  1
       config = new Configuration();
 91  
     }
 92  
 
 93  
     // TODO Shouldn't this be done in the constructor?
 94  3
     config.setupProperties();
 95  
 
 96  3
     return config;
 97  
   }
 98  
 
 99  
   /**
 100  
    * Store the configuration into the configuration file
 101  
    */
 102  
   public void store() {
 103  
     File configFile;
 104  
 
 105  1
     configFile = getFile(FILENAME_PROPERTIES);
 106  
 
 107  
     try {
 108  1
       store(new FileOutputStream(configFile, false), "BasicQuery Configuration");
 109  0
     } catch (Throwable any) {
 110  0
       LOGGER.error("Unable to store properties file ("
 111  
           + configFile.getAbsolutePath() + ")", any);
 112  1
     }
 113  1
   }
 114  
 
 115  
   /**
 116  
    * Load the properties from the properties file
 117  
    */
 118  
   private void setupProperties() {
 119  
     File propFile;
 120  
 
 121  3
     propFile = getFile(FILENAME_PROPERTIES);
 122  
 
 123  
     try {
 124  3
       config.load(new FileInputStream(propFile));
 125  0
     } catch (Throwable any) {
 126  0
       LOGGER.error("Unable to load properties file ("
 127  
           + propFile.getAbsolutePath() + ")", any);
 128  3
     }
 129  3
   }
 130  
 
 131  
   /**
 132  
    * Setup a file based on the file name
 133  
    * 
 134  
    * @param fileName
 135  
    *          The file to create a file reference for
 136  
    * 
 137  
    * @see #getFilePath(String)
 138  
    * @see #userDefaultFile(String)
 139  
    * 
 140  
    * @return The file reference to the named file
 141  
    */
 142  
   public File getFile(String fileName) {
 143  
     File configFile;
 144  
 
 145  8
     configFile = new File(getFilePath(fileName));
 146  8
     if (!configFile.exists()) {
 147  3
       userDefaultFile(fileName);
 148  
     }
 149  
 
 150  8
     return configFile;
 151  
   }
 152  
 
 153  
   /**
 154  
    * Prepend the user's home directory path the the file name and create a file
 155  
    * instance
 156  
    * 
 157  
    * @param fileName
 158  
    *          The file name
 159  
    * 
 160  
    * @return The file name prepended with the user's home directory
 161  
    */
 162  
   private String getFilePath(String fileName) {
 163  8
     return userHomeDirectory + "/" + fileName;
 164  
   }
 165  
 
 166  
   /**
 167  
    * Prepend the user's home directory path the the file name and create a file
 168  
    * instance. If the file is not found, copy the default version of the file
 169  
    * into the target file in order to initialize the application
 170  
    * 
 171  
    * @param fileName
 172  
    *          The file name
 173  
    */
 174  
   private void userDefaultFile(String fileName) {
 175  
     File realFile, defaultFile;
 176  
 
 177  3
     realFile = new File(userHomeDirectory + "/" + fileName);
 178  3
     if (!realFile.exists()) {
 179  3
       defaultFile = new File(fileName + ".txt");
 180  3
       if (defaultFile.exists()) {
 181  1
         copyFile(defaultFile, realFile);
 182  
       }
 183  
     }
 184  3
   }
 185  
 
 186  
   /**
 187  
    * Copy the content of a text file into another text file
 188  
    * 
 189  
    * @param source
 190  
    *          The source file
 191  
    * @param dest
 192  
    *          The target file
 193  
    */
 194  
   private static void copyFile(File source, File dest) {
 195  
     BufferedReader in;
 196  
     PrintWriter out;
 197  
     String data;
 198  
 
 199  1
     in = null;
 200  1
     out = null;
 201  
 
 202  
     try {
 203  1
       in = new BufferedReader(new FileReader(source));
 204  1
       out = new PrintWriter(new FileWriter(dest));
 205  
 
 206  3
       while ((data = in.readLine()) != null) {
 207  2
         out.println(data);
 208  
       }
 209  0
     } catch (Throwable any) {
 210  0
       LOGGER.warn("Unable to copy default file (" + source.getAbsolutePath()
 211  
           + ") to configuration file (" + dest.getAbsolutePath() + ")", any);
 212  
     } finally {
 213  1
       if (in != null) {
 214  
         try {
 215  1
           in.close();
 216  0
         } catch (Throwable any) {
 217  0
           LOGGER.warn("Unable to close the input file: " + source, any);
 218  1
         }
 219  
       }
 220  1
       if (out != null) {
 221  
         try {
 222  1
           out.close();
 223  0
         } catch (Throwable any) {
 224  0
           LOGGER.warn("Unable to close the output file: " + dest, any);
 225  1
         }
 226  
       }
 227  
     }
 228  1
   }
 229  
 }