com.shimari.framework
Class Config

java.lang.Object
  extended bycom.shimari.framework.Config

public class Config
extends java.lang.Object

This class is a replacement for java.util.Properties. It establishes an application's configuration based on a properties file that is loaded from the classpath, or a URL, or a supplied SortedMap. It provides a range of methods to parse the properties as various specific types. .properties files. Unlike java.util.Properties it does not expose a modifiable interface; it throws better error messages; it does the loading from the classpath for you; it is capable of translating properties into objects; and it does strict checking on values.


Field Summary
static java.text.SimpleDateFormat DATE_FORMAT
           
static char SEPARATOR
           
 
Constructor Summary
Config(java.lang.String propertyResource)
          Create a new config from a file on the current context class loader.
Config(java.net.URL configurationSource)
          Create a new config loading from the supplied URL.
 
Method Summary
 boolean containsKey(java.lang.String key)
          Return true if the key is defined in this Config
 java.net.InetSocketAddress getAddress(java.lang.String key)
          Return the value as an inet socket address.
 java.net.InetSocketAddress getAddress(java.lang.String key, java.net.InetSocketAddress defaultValue)
          If the key does not exist in the configuration, return the default value instead.
 java.lang.Object getBean(java.lang.String key)
          Equivalent to getBean(key,Object.class)
 java.lang.Object getBean(java.lang.String key, java.lang.Class type)
          Get a property as an Object.
 java.lang.Object[] getBeanArray(java.lang.String key)
          Equivalent to getBeanArray(key,Object.class)
 java.lang.Object[] getBeanArray(java.lang.String key, java.lang.Class type)
          The key denotes a prefix; all the roots under this prefix will be loaded as beans and returned as an array.
 java.lang.Class getClass(java.lang.String key)
          Return the value as a Class.
 java.lang.Class getClass(java.lang.String key, java.lang.Class defaultValue)
          If the key does not exist in the configuration, return the default value instead.
 java.lang.Class[] getClassArray(java.lang.String key)
          Get a property as a Class[].
 Registry getComponentRegistry()
          Return the components created under this configuration
 Config getConfig(java.lang.String key)
          Get a copy of the Config, only operating on the subset of keys beginning with the supplied prefix.
 java.util.Date getDate(java.lang.String key)
          Return the value as a date.
 java.util.Date getDate(java.lang.String key, java.util.Date defaultValue)
          If the key does not exist in the configuration, return the default value instead.
 java.io.File getDirectory(java.lang.String key)
          Return the value as a File, checking that it is also a directory.
 java.io.File getDirectory(java.lang.String key, java.io.File defaultValue)
          If the key does not exist in the configuration, return the default value instead.
 double getDouble(java.lang.String key)
          Return the value as a double.
 double getDouble(java.lang.String key, double defaultValue)
          If the configuration does not contain the key return the default value instead.
 double[] getDoubleArray(java.lang.String key)
          Get a property as a double[].
 java.io.File getFile(java.lang.String key)
          Return the value as a File.
 java.io.File getFile(java.lang.String key, java.io.File defaultValue)
          If the key does not exist in the configuration, return the default value instead.
 int getInteger(java.lang.String key)
          Return the value as an int.
 int getInteger(java.lang.String key, int defaultValue)
          If the configuration does not contain the supplied key, return the default value.
 int[] getIntegerArray(java.lang.String key)
          Get a property as an int[].
 long getLong(java.lang.String key)
          Return the value as a long.
 long getLong(java.lang.String key, long defaultValue)
          If the configuration does not contain the supplied key, return the default value instead.
 long[] getLongArray(java.lang.String key)
          Get a property as an int[].
 java.util.regex.Pattern getPattern(java.lang.String key)
          Return a regular expression pattern.
 java.util.regex.Pattern getPattern(java.lang.String key, java.util.regex.Pattern defaultValue)
          If the key does not exist in the configuration, return the default value instead.
 java.net.URL getSource()
          Return the source of the configuration
 java.lang.String getString(java.lang.String key)
          Get a property as a String.
 java.lang.String getString(java.lang.String key, java.lang.String defaultValue)
          Instead of throwing a ConfigException, return the default value if the string is not in the configuration.
 java.lang.String[] getStringArray(java.lang.String key)
          Get a property as a String[].
 java.lang.String[] getStringarray(java.lang.String key, java.lang.String regx)
          Similar to getStringArray(key) but you get to choose the splitting regular expression
 java.net.URL getURL(java.lang.String key)
          Return the value as a URL.
 java.net.URL getURL(java.lang.String key, java.net.URL defaultValue)
          If the key does not exist in the configuration, return the default value instead.
 boolean isEmpty()
          Return true if the Config contains no values
 java.util.Iterator keys()
          Get the keys.
 java.util.Set keySet()
          Get a set containing the keys of this configuration.
 java.util.Set rootSet()
          Get the root keys.
 int size()
          Return the number of keys in this Config.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SEPARATOR

public static final char SEPARATOR
See Also:
Constant Field Values

DATE_FORMAT

public static final java.text.SimpleDateFormat DATE_FORMAT
Constructor Detail

Config

public Config(java.net.URL configurationSource)
       throws ConfigException
Create a new config loading from the supplied URL.


Config

public Config(java.lang.String propertyResource)
       throws ConfigException
Create a new config from a file on the current context class loader.

Method Detail

getString

public final java.lang.String getString(java.lang.String key)
                                 throws ConfigException
Get a property as a String. Throw an exception if undefined.

Throws:
ConfigException

getString

public java.lang.String getString(java.lang.String key,
                                  java.lang.String defaultValue)
Instead of throwing a ConfigException, return the default value if the string is not in the configuration.


getStringArray

public java.lang.String[] getStringArray(java.lang.String key)
                                  throws ConfigException
Get a property as a String[]. Throw an exception if undefined. The string will be split on commas. A comma can be escaped by preceding it with a \ so that it won't be used to split if it's wanted.

Throws:
ConfigException

getStringarray

public java.lang.String[] getStringarray(java.lang.String key,
                                         java.lang.String regx)
                                  throws ConfigException
Similar to getStringArray(key) but you get to choose the splitting regular expression

Throws:
ConfigException

getInteger

public int getInteger(java.lang.String key)
               throws ConfigException
Return the value as an int. Throw an exception if it is undefined or not parseable as an int.

Throws:
ConfigException

getInteger

public int getInteger(java.lang.String key,
                      int defaultValue)
               throws ConfigException
If the configuration does not contain the supplied key, return the default value. Will still throw ConfigException if the key exists but cannot be parsed as an integer.

Throws:
ConfigException

getIntegerArray

public int[] getIntegerArray(java.lang.String key)
                      throws ConfigException
Get a property as an int[]. Throw an exception if undefined. The string will be split on commas.

Throws:
ConfigException

getLong

public long getLong(java.lang.String key)
             throws ConfigException
Return the value as a long. Throw an exception if it is undefined or not parseable as a long.

Throws:
ConfigException

getLong

public long getLong(java.lang.String key,
                    long defaultValue)
             throws ConfigException
If the configuration does not contain the supplied key, return the default value instead. Will still throw a ConfigException if there is a value but it cannot be parsed as a long.

Throws:
ConfigException

getLongArray

public long[] getLongArray(java.lang.String key)
                    throws ConfigException
Get a property as an int[]. Throw an exception if undefined. The string will be split on commas.

Throws:
ConfigException

getDouble

public double getDouble(java.lang.String key)
                 throws ConfigException
Return the value as a double. Throw an exception if it is undefined or not parseable as a double.

Throws:
ConfigException

getDouble

public double getDouble(java.lang.String key,
                        double defaultValue)
                 throws ConfigException
If the configuration does not contain the key return the default value instead. Can still throw a ConfigException if there is such a key but it cannot be parsed as a double.

Throws:
ConfigException

getDoubleArray

public double[] getDoubleArray(java.lang.String key)
                        throws ConfigException
Get a property as a double[]. Throw an exception if undefined. The string will be split on commas.

Throws:
ConfigException

getBean

public java.lang.Object getBean(java.lang.String key,
                                java.lang.Class type)
                         throws ConfigException
Get a property as an Object. The referenced property names a class, which will be instantiated. If the type has a constructor which takes a Config argument that constructor will be used to instantiate the object, otherwise the default constructor will be called. The Config object passed will contain the sub-properties (entries prefixed with the supplied key). A check will be performed to ensure that the bean implements or extends the supplied type, if not a ConfigException will be raised.

Throws:
ConfigException

getBean

public java.lang.Object getBean(java.lang.String key)
                         throws ConfigException
Equivalent to getBean(key,Object.class)

Throws:
ConfigException

getBeanArray

public java.lang.Object[] getBeanArray(java.lang.String key,
                                       java.lang.Class type)
                                throws ConfigException
The key denotes a prefix; all the roots under this prefix will be loaded as beans and returned as an array. For example, if you defined a prefix called "runnables", then getBeanArray("runnables", Runnable.class) would instantiate and configure all of the runnables and return them. The array returned will be of the supplied class type. If any of the loaded beans fail to implement or extend this type a ConfigException will be raised. If you want to allow any type, pass in either null or Object.class as the type argument.

Throws:
ConfigException

getBeanArray

public java.lang.Object[] getBeanArray(java.lang.String key)
                                throws ConfigException
Equivalent to getBeanArray(key,Object.class)

Throws:
ConfigException

getDate

public java.util.Date getDate(java.lang.String key)
                       throws ConfigException
Return the value as a date. Throw an exception if it is undefined or not parseable as a date. The correct format for a date is: yyyy-MM-dd HH:mm:ss (from most specific to least).

Throws:
ConfigException

getDate

public java.util.Date getDate(java.lang.String key,
                              java.util.Date defaultValue)
                       throws ConfigException
If the key does not exist in the configuration, return the default value instead. Will still throw an exception if there is a value in the configuration that cannot be parsed.

Throws:
ConfigException

getURL

public java.net.URL getURL(java.lang.String key)
                    throws ConfigException
Return the value as a URL. Throw an exception if it is undefined or not parseable as a URL.

Throws:
ConfigException

getURL

public java.net.URL getURL(java.lang.String key,
                           java.net.URL defaultValue)
                    throws ConfigException
If the key does not exist in the configuration, return the default value instead. Will still throw an exception if there is a value in the configuration that cannot be parsed.

Throws:
ConfigException

getFile

public java.io.File getFile(java.lang.String key)
                     throws ConfigException
Return the value as a File. Throw an exception if it is undefined or not parseable as a file or if the file does not exist, or if the file is not readable.

Throws:
ConfigException

getFile

public java.io.File getFile(java.lang.String key,
                            java.io.File defaultValue)
                     throws ConfigException
If the key does not exist in the configuration, return the default value instead. Will still throw an exception if there is a value in the configuration that cannot be parsed.

Throws:
ConfigException

getDirectory

public java.io.File getDirectory(java.lang.String key)
                          throws ConfigException
Return the value as a File, checking that it is also a directory. Throws an exception if it is undefined or not a readable directory.

Throws:
ConfigException

getDirectory

public java.io.File getDirectory(java.lang.String key,
                                 java.io.File defaultValue)
                          throws ConfigException
If the key does not exist in the configuration, return the default value instead. Will still throw an exception if there is a value in the configuration that cannot be parsed.

Throws:
ConfigException

getClass

public java.lang.Class getClass(java.lang.String key)
                         throws ConfigException
Return the value as a Class. Throw an exception if it is undefined or the Class cannot be found.

Throws:
ConfigException

getClass

public java.lang.Class getClass(java.lang.String key,
                                java.lang.Class defaultValue)
                         throws ConfigException
If the key does not exist in the configuration, return the default value instead. Will still throw an exception if there is a value in the configuration that cannot be resolved.

Throws:
ConfigException

getClassArray

public java.lang.Class[] getClassArray(java.lang.String key)
                                throws ConfigException
Get a property as a Class[]. Throw an exception if undefined, or if any class is not found. The string will be split on commas.

Throws:
ConfigException

getAddress

public java.net.InetSocketAddress getAddress(java.lang.String key)
                                      throws ConfigException
Return the value as an inet socket address. Throw an exception if it is undefined or not parseable as a host:port string. Does not check whether the host actually resolves.

Throws:
ConfigException

getAddress

public java.net.InetSocketAddress getAddress(java.lang.String key,
                                             java.net.InetSocketAddress defaultValue)
                                      throws ConfigException
If the key does not exist in the configuration, return the default value instead. Will still throw an exception if there is a value in the configuration that cannot be parsed.

Throws:
ConfigException

getPattern

public java.util.regex.Pattern getPattern(java.lang.String key)
                                   throws ConfigException
Return a regular expression pattern. Throw an exception if it is undefine or not parseable.

Throws:
ConfigException

getPattern

public java.util.regex.Pattern getPattern(java.lang.String key,
                                          java.util.regex.Pattern defaultValue)
                                   throws ConfigException
If the key does not exist in the configuration, return the default value instead. Will still throw an exception if there is a value in the configuration that cannot be parsed.

Throws:
ConfigException

keys

public java.util.Iterator keys()
Get the keys. The returned set is unmodifiable. The keys will be in sorted order.


rootSet

public java.util.Set rootSet()
Get the root keys. These are keys which contain no separator. For example, if the config contains the keys a.one, a.two, b.three, c, c.four, and d.five.six then the root set is (a,b,c,d). Note that not all values in the root set are actual keys, 'd' for example is in the root set but it is not a valid key.


keySet

public java.util.Set keySet()
Get a set containing the keys of this configuration. This essentially provides a java.util.collections interface to the size(), keys(), and containsKey(String) methods.

See Also:
rootSet().

getConfig

public Config getConfig(java.lang.String key)
                 throws ConfigException
Get a copy of the Config, only operating on the subset of keys beginning with the supplied prefix. This operation is only valid if the supplied key is in the root set.

Throws:
ConfigException
See Also:
rootSet()

containsKey

public boolean containsKey(java.lang.String key)
Return true if the key is defined in this Config


size

public int size()
Return the number of keys in this Config. Note that this counts every key--including all sub-keys for class definitions.


isEmpty

public boolean isEmpty()
Return true if the Config contains no values


getSource

public java.net.URL getSource()
Return the source of the configuration


getComponentRegistry

public Registry getComponentRegistry()
Return the components created under this configuration