com.googlecode.fascinator.common
Class JsonSimpleConfig

java.lang.Object
  extended by com.googlecode.fascinator.common.JsonSimple
      extended by com.googlecode.fascinator.common.JsonSimpleConfig

public class JsonSimpleConfig
extends JsonSimple

An extension of the JsonSimple class specifically to access configuration.

Aside from offering a constructor that takes care of finding and accessing the system configuration file, this class also offers a selection of methods for management of the system configuration, such as backup and version testing.

Finally, whatever configuration is provided to this class, it will be backed by the full system configuration file. Nodes not found in the provided config will also be checked in the System config.

Author:
Greg Pendlebury

Constructor Summary
JsonSimpleConfig()
          Creates JSON Configuration object from the system config file
JsonSimpleConfig(java.io.File jsonFile)
          Creates JSON Configuration object from the provided config file
JsonSimpleConfig(java.io.InputStream jsonIn)
          Creates JSON Configuration object from the provided input stream
JsonSimpleConfig(java.lang.String jsonString)
          Creates JSON Configuration object from the provided config string
 
Method Summary
static java.io.File backupSystemFile()
          Performs a backup on the system-wide configuration file from the default config dir if it exists.
 org.json.simple.JSONArray getArray(java.lang.Object... path)
          Walk down the JSON nodes specified by the path and retrieve the target JSONArray.
 java.lang.Boolean getBoolean(java.lang.Boolean defaultValue, java.lang.Object... path)
          Retrieve the Boolean value on the given path.
 java.lang.Integer getInteger(java.lang.Integer defaultValue, java.lang.Object... path)
          Retrieve the Integer value on the given path.
 java.util.List<JsonSimple> getJsonSimpleList(java.lang.Object... path)
           Retrieve a list of JsonSimple objects found on the given path.
 java.util.Map<java.lang.String,JsonSimple> getJsonSimpleMap(java.lang.Object... path)
           Retrieve a map of JsonSimple objects found on the given path.
 JsonObject getObject(java.lang.Object... path)
          Walk down the JSON nodes specified by the path and retrieve the target JsonObject.
 java.lang.Object getPath(java.lang.Object... path)
          Walk down the JSON nodes specified by the path and retrieve the target.
 java.lang.String getString(java.lang.String defaultValue, java.lang.Object... path)
          Retrieve the String value on the given path.
 java.util.List<java.lang.String> getStringList(java.lang.Object... path)
           Retrieve a list of Strings found on the given path.
static java.io.File getSystemFile()
          Gets the system-wide configuration file from the default config dir.
 boolean isConfigured()
          Tests whether or not the system-config has been properly configured.
 boolean isOutdated()
          To check if configuration file is outdated
 java.util.List<java.lang.Object> search(java.lang.String node)
           Search through the JSON for any nodes (at any depth) matching the requested name and return them.
 void storeSystemConfig()
           Store the underlying system configuration on disk in the appropriate location.
 JsonObject writableSystemConfig()
           Returns a reference to the underlying system configuration object.
 
Methods inherited from class com.googlecode.fascinator.common.JsonSimple
fromJavaMap, getJsonArray, getJsonObject, getStringList, getStringList, setPropertySubstitution, toJavaList, toJavaMap, toString, toString, writeArray, writeObject
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JsonSimpleConfig

public JsonSimpleConfig()
                 throws java.io.IOException
Creates JSON Configuration object from the system config file

Throws:
java.io.IOException - if there was an error during creation

JsonSimpleConfig

public JsonSimpleConfig(java.io.File jsonFile)
                 throws java.io.IOException
Creates JSON Configuration object from the provided config file

Parameters:
jsonFile - : The file containing JSON
Throws:
java.io.IOException - if there was an error during creation

JsonSimpleConfig

public JsonSimpleConfig(java.io.InputStream jsonIn)
                 throws java.io.IOException
Creates JSON Configuration object from the provided input stream

Parameters:
jsonIn - : The input stream to read
Throws:
java.io.IOException - if there was an error during creation

JsonSimpleConfig

public JsonSimpleConfig(java.lang.String jsonString)
                 throws java.io.IOException
Creates JSON Configuration object from the provided config string

Parameters:
jsonString - : The JSON in string form
Throws:
java.io.IOException - if there was an error during creation
Method Detail

backupSystemFile

public static java.io.File backupSystemFile()
                                     throws java.io.IOException
Performs a backup on the system-wide configuration file from the default config dir if it exists. Returns a reference to the backed up file.

Returns:
the backed up system JSON file
Throws:
java.io.IOException - if there was an error reading or writing either file

getSystemFile

public static java.io.File getSystemFile()
                                  throws java.io.IOException
Gets the system-wide configuration file from the default config dir. If the file doesn't exist, a default is copied to the config dir.

Returns:
the system JSON file
Throws:
java.io.IOException - if there was an error reading or writing the system configuration file

isConfigured

public boolean isConfigured()
Tests whether or not the system-config has been properly configured.

Returns:
true if configured, false if still using defaults

isOutdated

public boolean isOutdated()
To check if configuration file is outdated

Returns:
true if outdated, false otherwise

getArray

public org.json.simple.JSONArray getArray(java.lang.Object... path)
Walk down the JSON nodes specified by the path and retrieve the target JSONArray.

Overrides:
getArray in class JsonSimple
Parameters:
path - : Variable length array of path segments
Returns:
JSONArray : The target node, or NULL if path invalid or not an array

getObject

public JsonObject getObject(java.lang.Object... path)
Walk down the JSON nodes specified by the path and retrieve the target JsonObject.

Overrides:
getObject in class JsonSimple
Parameters:
path - : Variable length array of path segments
Returns:
JsonObject : The target node, or NULL if path invalid or not an object

getPath

public java.lang.Object getPath(java.lang.Object... path)
Walk down the JSON nodes specified by the path and retrieve the target.

Overrides:
getPath in class JsonSimple
Parameters:
path - : Variable length array of path segments
Returns:
Object : The target node, or NULL if invalid

getBoolean

public java.lang.Boolean getBoolean(java.lang.Boolean defaultValue,
                                    java.lang.Object... path)
Retrieve the Boolean value on the given path. IMPORTANT: The default value only applies if the path is not found. If a string on the path is found it will be considered false unless the value is 'true' (ignoring case). This is the default behaviour of the Boolean.parseBoolean() method.

Overrides:
getBoolean in class JsonSimple
Parameters:
defaultValue - : The fallback value to use if the path is invalid or not found
path - : An array of indeterminate length to use as the path
Returns:
Boolean : The Boolean value found on the given path, or null if no default provided

getInteger

public java.lang.Integer getInteger(java.lang.Integer defaultValue,
                                    java.lang.Object... path)
Retrieve the Integer value on the given path.

Overrides:
getInteger in class JsonSimple
Parameters:
defaultValue - : The fallback value to use if the path is invalid or not found
path - : An array of indeterminate length to use as the path
Returns:
Integer : The Integer value found on the given path, or null if no default provided

getString

public java.lang.String getString(java.lang.String defaultValue,
                                  java.lang.Object... path)
Retrieve the String value on the given path.

Overrides:
getString in class JsonSimple
Parameters:
defaultValue - : The fallback value to use if the path is invalid or not found
path - : An array of indeterminate length to use as the path
Returns:
String : The String value found on the given path, or null if no default provided

getStringList

public java.util.List<java.lang.String> getStringList(java.lang.Object... path)

Retrieve a list of Strings found on the given path. Note that this is a utility function, and not designed for data traversal. It will only retrieve Strings found on the provided node, and the node must be a JSONArray.

Overrides:
getStringList in class JsonSimple
Parameters:
path - : An array of indeterminate length to use as the path
Returns:
List : A list of Strings, null if the node is not found

getJsonSimpleList

public java.util.List<JsonSimple> getJsonSimpleList(java.lang.Object... path)

Retrieve a list of JsonSimple objects found on the given path. Note that this is a utility function, and not designed for data traversal. It will only retrieve valid JsonObjects found on the provided node, and wrap them in JsonSimple objects.

Other objects found on that path will be ignored, and if the path itself is not a JSONArray or not found, the function will return NULL.

Overrides:
getJsonSimpleList in class JsonSimple
Parameters:
path - : An array of indeterminate length to use as the path
Returns:
List : A list of JSONSimple objects, or null

getJsonSimpleMap

public java.util.Map<java.lang.String,JsonSimple> getJsonSimpleMap(java.lang.Object... path)

Retrieve a map of JsonSimple objects found on the given path. Note that this is a utility function, and not designed for data traversal. It will only retrieve valid JsonObjects found on the provided node, and wrap them in JsonSimple objects.

Other objects found on that path will be ignored, and if the path itself is not a JsonObject or not found, the function will return NULL.

Overrides:
getJsonSimpleMap in class JsonSimple
Parameters:
path - : An array of indeterminate length to use as the path
Returns:
Map : A map of JSONSimple objects, or null

search

public java.util.List<java.lang.Object> search(java.lang.String node)

Search through the JSON for any nodes (at any depth) matching the requested name and return them. The returned List will be of type Object and require type interrogation for detailed use, but will be implemented as a LinkedList to preserve order.

Overrides:
search in class JsonSimple
Parameters:
node - : The node name we are looking for
Returns:
List : A list of matching Objects from the data

writableSystemConfig

public JsonObject writableSystemConfig()

Returns a reference to the underlying system configuration object. This is meant to be used on conjunction with storeSystemConfig() to make changes to the config file on disk.

Normal modifications to this objects JSON are not written to disk unless they they are made via writableSystemConfig().

Returns:
JsonObject : A reference to the system configuration JSON object

storeSystemConfig

public void storeSystemConfig()
                       throws java.io.IOException

Store the underlying system configuration on disk in the appropriate location.

Normal modifications to this objects JSON are not written to disk unless they they are made via writableSystemConfig().

Throws:
java.io.IOException


Copyright © 2009-2013. All Rights Reserved.