andyLib v0.9

it.andynaz.config
Class ConfigMgr

java.lang.Object
  extended by it.andynaz.config.ConfigMgr

public class ConfigMgr
extends Object

Class to centralize the configuration of a program as a set of parameters. Parameters can be stored (for instance, at the beginning of the execution) and then retrived

The parameters can be stored in the class using one (or more) of the init methods provided or the setConfigParam(String, String) method (to store them manually). When storing a value, only the last one is saved (so different invocation of one of the init methods override old values of the same parameter - this can be used also to choose the priority of different source of config parameters).
Note that the init methods consider only the parameter which name begin with he suffix "it.andynaz.", and when storing the value the suffix is removed.

Parameters can be retrived using the getConfigParam(String) method.

All of its methods are static, as there have to be only one instance of the configuration. However, the class can return a copy of all the params stored as a Properties object with the getConfigParams() method.

Version:
2013/09/17
Author:
andynaz

Field Summary
private static Properties p
           
private static String prefix
           
 
Constructor Summary
ConfigMgr()
           
 
Method Summary
static String getConfigParam(String propName)
          Return the config parameter associated with the name.
static Properties getConfigParams()
          Returns a Properties object with all the properties stored.
static void init()
          Loads the parameters stored in System.getProperties().
static void init(Properties props)
          Loads the parameters stored in the Properties object.
static void list()
          List all the parameters currently stored.
static void removeConfigParam(String name)
          Remove a parameter value.
static void setConfigParam(String name, String value)
          Set a parameter value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

prefix

private static String prefix

p

private static Properties p
Constructor Detail

ConfigMgr

public ConfigMgr()
Method Detail

init

public static void init()
Loads the parameters stored in System.getProperties(). This method consider only the properties that began with the prefix "it.andynaz." and store them without the prefix.


init

public static void init(Properties props)
Loads the parameters stored in the Properties object. This method consider only the properties that began with the prefix "it.andynaz." and store them without the prefix.

Parameters:
props -
Throws:
NullPointerException - if props is null

getConfigParam

public static String getConfigParam(String propName)
Return the config parameter associated with the name.

Returns:
the config parameter associated with the name, or null if the parameter does not exists

getConfigParams

public static Properties getConfigParams()
Returns a Properties object with all the properties stored.

Returns:
a copy of the internal Properties object

setConfigParam

public static void setConfigParam(String name,
                                  String value)
Set a parameter value. The value is set only of it is not null.

If the parameter already exists, its value is overwritten.

Parameters:
name - parameter's name
value - parameter's value
Throws:
NullPointerException - if name is null

removeConfigParam

public static void removeConfigParam(String name)
Remove a parameter value.

Parameters:
name - parameter's name

list

public static void list()
List all the parameters currently stored. The parameters are printed on the standard output. Used for debug.


andyLib v0.9

© 2013 andynaz - http://andynaz.altervista.org