edu.ntnu.item.jt.configloader
Class PropertiesReader

java.lang.Object
  extended by edu.ntnu.item.jt.configloader.PropertiesReader

public class PropertiesReader
extends Object

Singleton responsible for maintaining a Properties object. Provides static access via getters. Contains logic for reading a properties file from disk. The properties reader is only intended as a means for providing access to constants, and thus does not provide any setProperty() functions.

Author:
Jøran Vagnby Lillesand
See Also:
Properties

Method Summary
 boolean getBool(String key, boolean defaultValue)
          Reads the value stored with a given key and attempts to cast it to boolean.
 double getDouble(String key, double defaultValue)
          Reads the value stored with a given key and attempts to cast it to double.
static PropertiesReader getInstance()
          Returns the properties reader instance.
 int getInt(String key, int defaultValue)
          Reads the value stored with a given key and attempts to cast it to int.
 String getString(String key, String defaultValue)
          Attempts to read the string associated with a given key.
static PropertiesReader initialize(String configFile)
          Initialize a properties reader object, reading the properties from a given location on the local file system.
static void main(String[] args)
          Contains simple test reads.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

initialize

public static PropertiesReader initialize(String configFile)
                                   throws AlreadyInitializedException,
                                          IOException
Initialize a properties reader object, reading the properties from a given location on the local file system. This function should ideally be called before getInstance() function is called, as a call to getInstance() will try to load a default properties file, and will load a blank one if the default properties file cannot be found on disk.

Parameters:
configFile - path to the properties file on the local file system. Note that any backslashes will have to be escaped. For example "c:\powerscan\powerscan.properties" becomes "c:\\powerscan\\powerscan.properties". The file will have to be structured on a format accepted by the java Properties class.
Returns:
properties reader instance.
Throws:
AlreadyInitializedException - if the properties reader has already been initialized.
IOException - if reading from the file failed. In this case, the properties reader may be initialized again, or calls to getInstance() will try to load the default properties file.
See Also:
Properties

getInstance

public static PropertiesReader getInstance()
Returns the properties reader instance. If the properties reader has not been initialized, the default properties file is attempted loaded. If the default properties file cannot be loaded, an empty Properties object is used, so that the default values will be used for every constant.

Returns:
properties reader instance.

getInt

public int getInt(String key,
                  int defaultValue)
Reads the value stored with a given key and attempts to cast it to int. If reading the value or casting fails, the default value is returned.

Parameters:
key - key from which to read the value.
defaultValue - default value to be returned if reading fails.
Returns:
value associated with key or default value.

getDouble

public double getDouble(String key,
                        double defaultValue)
Reads the value stored with a given key and attempts to cast it to double. If reading the value or casting fails, the default value is returned.

Parameters:
key - key from which to read the value.
defaultValue - default value to be returned if reading fails.
Returns:
value associated with key or default value.

getBool

public boolean getBool(String key,
                       boolean defaultValue)
Reads the value stored with a given key and attempts to cast it to boolean. If reading the value or casting fails, the default value is returned.

Parameters:
key - key from which to read the value.
defaultValue - default value to be returned if reading fails.
Returns:
value associated with key or default value.

getString

public String getString(String key,
                        String defaultValue)
Attempts to read the string associated with a given key. Returns a default value if the value cannot be read.

Parameters:
key - key from which to read the value.
defaultValue - default value to be returned if reading fails.
Returns:
value associated with key or default value.

main

public static void main(String[] args)
                 throws Exception
Contains simple test reads.

Parameters:
args -
Throws:
Exception