|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.base.apps.util.config.ResourceManager
public class ResourceManager
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.
// 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);
}
}
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.
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.
// 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
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
foreach ( Option opt : ConditionalProperty.options )
{
if ( opt.ConditionKey.value == Comparison )
{
PropertyValue = opt.ConditionalValue
return;
}
PropertyValue = opt.DefaultValue
}
| 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 |
|---|
protected static Pattern REGEX
protected static Pattern CONDITIONS
| Constructor Detail |
|---|
protected ResourceManager()
protected ResourceManager(Named rsrcName)
ResourceManager and loads its resources.
rsrcName - | Method Detail |
|---|
public String getString(CharSequence property,
Object... tkns)
MessageFormat.format(String, Object...)).
property - The property to read.tkns - The optional replacement tokens.
public Integer getInteger(CharSequence property,
Object... tkns)
MessageFormat.format(String, Object...)),
and returns the property value as an Integer.
property - The property to read.tkns - The optional replacement tokens.
null if the value is not numeric.
public boolean getBoolean(CharSequence property,
Object... tkns)
MessageFormat.format(String, Object...)),
and returns the property value as an Boolean.
property - The property to read.tkns - The optional replacement tokens.
BooleanUtils.toBoolean(String)public EnvConfig getEnv()
public String getName()
Named
getName in interface NamedNamed.getName()public void removeProperty(CharSequence property)
property - The property to remove (unset)
public void setProperty(CharSequence property,
String value)
property - The property to setvalue - The property's valuepublic String toString()
toString in class ObjectObject.toString()protected ResourceManager getParent()
protected void setParent(ResourceManager parent)
parent - the parent to setprotected ResourceManager.BaseProperties getProps()
protected void setProps(ResourceManager.BaseProperties props)
props - The loaded configurationprotected ListMap<String,String> getConditions()
protected void setConditions(ListMap<String,String> conditions)
conditions - the conditions to setprotected String getRawValue(CharSequence property)
property - The property to fetch
null
protected String replaceVars(String value,
Object... tkns)
value - The given valuetkns - The subsitution tokens
protected void loadBundles(Named rsrc)
The resource file is sought via ClassLoaders in the following order:
Thread.getContextClassLoader()ClassLoader.getSystemResourceAsStream(String)Named class, via Class.getResourceAsStream(String)
rsrc - The resource name.protected void deriveValues()
protected void updateConditionalProperties(String key)
protected void setConditionalProperty(String key)
protected void setName(String name)
name - the name to setpublic static EnvConfig getEnvConfig()
public static ResourceManager getResourceManager(Named name)
ResourceManagers.
name - The name of the resource to manage, which must be in the classpath
OR relative to the given Named resource.
ResourceManager
public static ResourceManager getResourceManager(Named parent,
Named child)
ResourceManagers that are children
of, and thus inherit properties from, a parent ResourceManager.
parent - The name of the parent resource.child - The name of the child resource.
ResourceManager with the given parent.public static ResourceManager getResourceManager(CharSequence name)
ResourceManagers.
name - The name of the resource to manage, which must be in the classpath.
ResourceManager
|
Please visit Base Apps, hosted on Sourceforge.net. | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||