org.base.apps.util.config
Class ResourceManager

java.lang.Object
  extended by org.base.apps.util.config.ResourceManager
All Implemented Interfaces:
Named
Direct Known Subclasses:
EnvConfig

public class ResourceManager
extends Object
implements Named

Utility to provide typecasting access, variable substitution, and token replacement capability to a resource bundle. ResourceManagers acquired via getResourceManager(Named) are cached so as to avoid duplicate loading of their respective resources.

Usage:

 // assumes a foo.properties file in the classpath (e.g. Foo's package)
 public class Foo implements org.base.apps.util.Named {
     private ResourceManager rMgr;
     
     public Foo() {
         // initialize via factory method
         rMgr = ResourceManager.getResourceManager(this);
     }
     
     // identifies the name of the properties resource
     public String getName() {
         return "foo";
     }
  
     // use the ResourceManager
     public String getFooUrl(String aRelativePath) {
         // the 'aRelativePath' parameter is inserted into 
         //    the property value via token replacement
         return rMgr.getString("foo.url.key", aRelativePath);
     }
     }
 

Properties Configuration

Token Replacement

Via MessageFormat, tokens in property values can be replaced. Given the property,
  tkn=This is a {0}.
the following code will output:
  String foo = rMgr.getString("tkn", "foo");
  System.out.println(foo);
  
  // output
  This is a foo.
 

Variable Substitution

Much like ANT, variables can be inserted in property values. Given the properties,
  tkn=This is a ${foo}.
  foo=variable
the following code will output:
  String foo = rMgr.getString("tkn");
  System.out.println(foo);
  
  // output
  This is a variable.
 

Conditional Properties

A custom syntax was created to allow property values to be conditionally set. Given the properties,
      // 1st opt     // 2nd opt     // default, if no conditions match
  tkn=[key(foo)(bar)][key(bar)(baz)]foo
  key=foo
the following code will output:
  System.out.println( "foo = "+ rMgr.getString("tkn") );
  // change the value of the conditional property 'key'
  rMgr.setProperty( "key", "bar" );
  System.out.println( "foo = "+ rMgr.getString("tkn") );
  // change the value of 'key' again to a non-match to acquire the default value
  rMgr.setProperty( "key", "doesNotMatchAnyConditions" );
  System.out.println( "foo = "+ rMgr.getString("tkn") );
  
  // output
  foo = bar
  foo = baz
  foo = foo
 

Conditional Property Syntax

ResourceManager requires for conditional properties at least one Option configured, using the following syntax:
  ConditionalProperty:
          Option1 Option... Optionn DefaultValue
          
  Option:
          [ ConditionKey ( Comparison ) ( ConditionalValue ) ]
          
  ConditionKey:
          UnquotedString
          
  Comparison:
          UnquotedString
          
  ConditionalValue:
          String
          
  String:
          StringPartopt
          String StringPart

  StringPart:
          ''
          ' QuotedString '
          UnquotedString
          
  DefaultValue:
          String
 

Conditional Property Syntax Evaluation

The values of conditional properties are set as follows:
  foreach ( Option opt : ConditionalProperty.options ) 
  {
      if ( opt.ConditionKey.value == Comparison ) 
      {
          PropertyValue = opt.ConditionalValue
          return;
      }
      
      PropertyValue = opt.DefaultValue
  }
 

Author:
Kevan Simpson

Nested Class Summary
static class ResourceManager.BaseProperties
           
 
Field Summary
protected static Pattern CONDITIONS
          Matches condition brackets for mapped properties.
protected static Pattern REGEX
          Variable matching regex.
 
Constructor Summary
protected ResourceManager()
          Void constructor, provided only in case subclasses need to be instantiated anonymously.
protected ResourceManager(Named rsrcName)
          Constructs a ResourceManager and loads its resources.
 
Method Summary
protected  void deriveValues()
          Evaluates and sets conditional property values.
 boolean getBoolean(CharSequence property, Object... tkns)
          Reads a property from the configuration, optionally inserting the given tokens (akin to MessageFormat.format(String, Object...)), and returns the property value as an Boolean.
protected  ListMap<String,String> getConditions()
           
 EnvConfig getEnv()
          Fetches the global resource manager.
static EnvConfig getEnvConfig()
          Static accessor of global resource configuration.
 Integer getInteger(CharSequence property, Object... tkns)
          Reads a property from the configuration, optionally inserting the given tokens (akin to MessageFormat.format(String, Object...)), and returns the property value as an Integer.
 String getName()
          Fetches the name of this instance.
protected  ResourceManager getParent()
          Fetches this manager's parent.
protected  ResourceManager.BaseProperties getProps()
          Fetches the loaded configuration.
protected  String getRawValue(CharSequence property)
          Searches the configuration hierarchy (this, then parents) for a property value, returning one without performing variable substitution or token replacement.
static ResourceManager getResourceManager(CharSequence name)
          Factory method for ResourceManagers.
static ResourceManager getResourceManager(Named name)
          Factory method for named ResourceManagers.
static ResourceManager getResourceManager(Named parent, Named child)
          Factory method for named ResourceManagers that are children of, and thus inherit properties from, a parent ResourceManager.
 String getString(CharSequence property, Object... tkns)
          Reads a property from the configuration, optionally inserting the given tokens (akin to MessageFormat.format(String, Object...)).
protected  void loadBundles(Named rsrc)
          Loads the configuration resources by name.
 void removeProperty(CharSequence property)
          Removes a programmatically-set property value, without affecting initial (loaded) configuration.
protected  String replaceVars(String value, Object... tkns)
          Performs variable substitution on the given value.
protected  void setConditionalProperty(String key)
           
protected  void setConditions(ListMap<String,String> conditions)
           
protected  void setName(String name)
           
protected  void setParent(ResourceManager parent)
          Sets this manager's parent.
 void setProperty(CharSequence property, String value)
          Sets a property value, overriding the intial (loaded) value.
protected  void setProps(ResourceManager.BaseProperties props)
          Sets the loaded configuration.
 String toString()
           
protected  void updateConditionalProperties(String key)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

REGEX

protected static Pattern REGEX
Variable matching regex.


CONDITIONS

protected static Pattern CONDITIONS
Matches condition brackets for mapped properties.

Constructor Detail

ResourceManager

protected ResourceManager()
Void constructor, provided only in case subclasses need to be instantiated anonymously.


ResourceManager

protected ResourceManager(Named rsrcName)
Constructs a ResourceManager and loads its resources.

Parameters:
rsrcName -
Method Detail

getString

public String getString(CharSequence property,
                        Object... tkns)
Reads a property from the configuration, optionally inserting the given tokens (akin to MessageFormat.format(String, Object...)).

Parameters:
property - The property to read.
tkns - The optional replacement tokens.
Returns:
The property value.

getInteger

public Integer getInteger(CharSequence property,
                          Object... tkns)
Reads a property from the configuration, optionally inserting the given tokens (akin to MessageFormat.format(String, Object...)), and returns the property value as an Integer.

Parameters:
property - The property to read.
tkns - The optional replacement tokens.
Returns:
The numeric property value or null if the value is not numeric.

getBoolean

public boolean getBoolean(CharSequence property,
                          Object... tkns)
Reads a property from the configuration, optionally inserting the given tokens (akin to MessageFormat.format(String, Object...)), and returns the property value as an Boolean.

Parameters:
property - The property to read.
tkns - The optional replacement tokens.
Returns:
The boolean property value
See Also:
BooleanUtils.toBoolean(String)

getEnv

public EnvConfig getEnv()
Fetches the global resource manager.

Returns:
the global resource manager.

getName

public String getName()
Description copied from interface: Named
Fetches the name of this instance.

Specified by:
getName in interface Named
Returns:
the name of this instance.
See Also:
Named.getName()

removeProperty

public void removeProperty(CharSequence property)
Removes a programmatically-set property value, without affecting initial (loaded) configuration.

Parameters:
property - The property to remove (unset)

setProperty

public void setProperty(CharSequence property,
                        String value)
Sets a property value, overriding the intial (loaded) value.

Parameters:
property - The property to set
value - The property's value

toString

public String toString()
Overrides:
toString in class Object
See Also:
Object.toString()

getParent

protected ResourceManager getParent()
Fetches this manager's parent.

Returns:
the parent

setParent

protected void setParent(ResourceManager parent)
Sets this manager's parent.

Parameters:
parent - the parent to set

getProps

protected ResourceManager.BaseProperties getProps()
Fetches the loaded configuration.

Returns:
the loaded configuration.

setProps

protected void setProps(ResourceManager.BaseProperties props)
Sets the loaded configuration.

Parameters:
props - The loaded configuration

getConditions

protected ListMap<String,String> getConditions()
Returns:
the conditions

setConditions

protected void setConditions(ListMap<String,String> conditions)
Parameters:
conditions - the conditions to set

getRawValue

protected String getRawValue(CharSequence property)
Searches the configuration hierarchy (this, then parents) for a property value, returning one without performing variable substitution or token replacement.

Parameters:
property - The property to fetch
Returns:
The property value or null

replaceVars

protected String replaceVars(String value,
                             Object... tkns)
Performs variable substitution on the given value.

Parameters:
value - The given value
tkns - The subsitution tokens
Returns:
The value, with variables substituted for their respective tokens

loadBundles

protected void loadBundles(Named rsrc)
Loads the configuration resources by name.

The resource file is sought via ClassLoaders in the following order:

  1. The current thread, via Thread.getContextClassLoader()
  2. The system, via ClassLoader.getSystemResourceAsStream(String)
  3. The resource name itself, using the Named class, via Class.getResourceAsStream(String)

Parameters:
rsrc - The resource name.

deriveValues

protected void deriveValues()
Evaluates and sets conditional property values.


updateConditionalProperties

protected void updateConditionalProperties(String key)

setConditionalProperty

protected void setConditionalProperty(String key)

setName

protected void setName(String name)
Parameters:
name - the name to set

getEnvConfig

public static EnvConfig getEnvConfig()
Static accessor of global resource configuration.

Returns:
the global configuration.

getResourceManager

public static ResourceManager getResourceManager(Named name)
Factory method for named ResourceManagers.

Parameters:
name - The name of the resource to manage, which must be in the classpath OR relative to the given Named resource.
Returns:
A configured ResourceManager

getResourceManager

public static ResourceManager getResourceManager(Named parent,
                                                 Named child)
Factory method for named ResourceManagers that are children of, and thus inherit properties from, a parent ResourceManager.

Parameters:
parent - The name of the parent resource.
child - The name of the child resource.
Returns:
A child ResourceManager with the given parent.

getResourceManager

public static ResourceManager getResourceManager(CharSequence name)
Factory method for ResourceManagers.

Parameters:
name - The name of the resource to manage, which must be in the classpath.
Returns:
A configured ResourceManager

Please visit Base Apps, hosted on Sourceforge.net.

Copyright 2011, Blue Agate Software Entity (BASE)