public class ManagedProperties
extends java.lang.Object
This class may disappear in the future, or be moved to another project..
Extend the concept of System properties to a hierarchical scheme based around class loaders. System properties are global in nature, so using them easily violates sound architectural and design principles for maintaining separation between components and runtime environments. Nevertheless, there is a need for properties broader in scope than class or class instance scope.
This class is one solution.
Manage properties according to a secure scheme similar to that used by classloaders:
ClassLoader
s are organized in a tree hierarchy.ClassLoader
has a reference
to a parent ClassLoader
.ClassLoader
er.ClassLoader
instance
ClassLoader
instance take precedence over all properties of the same name bound
to any decendent.
Just to confuse the issue, this is the default case.ClassLoader
instance may be overriden by (default or non-default) properties of
the same name bound to any decendent.
This is not a perfect solution, as it is possible that
different ClassLoader
s load different instances of
ScopedProperties
. The 'higher' this class is loaded
within the ClassLoader
hierarchy, the more usefull
it will be.
Modifier and Type | Class and Description |
---|---|
private static class |
ManagedProperties.Value
INTERNAL IMPLEMENTATION
|
Modifier and Type | Field and Description |
---|---|
private static org.apache.commons.logging.Log |
log |
private static java.util.Map<java.lang.ClassLoader,java.util.Map<java.lang.String,ManagedProperties.Value>> |
propertiesCache
Cache of Properties, keyed by (thread-context) class loaders.
|
Constructor and Description |
---|
ManagedProperties() |
Modifier and Type | Method and Description |
---|---|
private static java.lang.ClassLoader |
getParent(java.lang.ClassLoader classLoader)
Return the parent class loader of the given class loader.
|
static java.util.Properties |
getProperties()
This is an expensive operation.
|
static java.lang.String |
getProperty(java.lang.ClassLoader classLoader,
java.lang.String propertyName)
Get value for property bound to the class loader.
|
static java.lang.String |
getProperty(java.lang.ClassLoader classLoader,
java.lang.String propertyName,
java.lang.String dephault)
Get value for property bound to the class loader.
|
static java.lang.String |
getProperty(java.lang.String propertyName)
Get value for property bound to the current thread context class loader.
|
static java.lang.String |
getProperty(java.lang.String propertyName,
java.lang.String dephault)
Get value for property bound to the current thread context class loader.
|
private static java.lang.ClassLoader |
getThreadContextClassLoader()
Returns the thread context class loader.
|
private static ManagedProperties.Value |
getValueProperty(java.lang.ClassLoader classLoader,
java.lang.String propertyName)
Get value for properties bound to the class loader.
|
static java.util.Enumeration<java.lang.String> |
propertyNames()
Return list of all property names.
|
static void |
setLog(org.apache.commons.logging.Log _log)
Deprecated.
This method is not thread-safe
|
static void |
setProperties(java.util.Map<?,?> newProperties)
Set property values for
Properties bound to the
current thread context class loader. |
static void |
setProperties(java.util.Map<?,?> newProperties,
boolean isDefault)
Set property values for
Properties bound to the
current thread context class loader. |
static void |
setProperty(java.lang.String propertyName,
java.lang.String value)
Set value for property bound to the current thread context class loader.
|
static void |
setProperty(java.lang.String propertyName,
java.lang.String value,
boolean isDefault)
Set value for property bound to the current thread context class loader.
|
private static org.apache.commons.logging.Log log
private static final java.util.Map<java.lang.ClassLoader,java.util.Map<java.lang.String,ManagedProperties.Value>> propertiesCache
HashMap
because it allows 'null' keys, which
allows us to account for the (null) bootstrap classloader.@Deprecated public static void setLog(org.apache.commons.logging.Log _log)
Log
for this class._log
- This class Log
public static java.lang.String getProperty(java.lang.String propertyName)
propertyName
- property name.public static java.lang.String getProperty(java.lang.String propertyName, java.lang.String dephault)
propertyName
- property name.dephault
- default value.public static java.lang.String getProperty(java.lang.ClassLoader classLoader, java.lang.String propertyName)
classLoader
- The classloader used to load resources.propertyName
- property name.public static java.lang.String getProperty(java.lang.ClassLoader classLoader, java.lang.String propertyName, java.lang.String dephault)
classLoader
- The classloader used to load resources.propertyName
- property name.dephault
- default value.public static void setProperty(java.lang.String propertyName, java.lang.String value)
propertyName
- property namevalue
- property value (non-default) If null, remove the property.public static void setProperty(java.lang.String propertyName, java.lang.String value, boolean isDefault)
propertyName
- property namevalue
- property value. If null, remove the property.isDefault
- determines if property is default or not.
A non-default property cannot be overriden.
A default property can be overriden by a property
(default or non-default) of the same name bound to
a decendent class loader.public static void setProperties(java.util.Map<?,?> newProperties)
Properties
bound to the
current thread context class loader.newProperties
- name/value pairs to be boundpublic static void setProperties(java.util.Map<?,?> newProperties, boolean isDefault)
Properties
bound to the
current thread context class loader.newProperties
- name/value pairs to be boundisDefault
- determines if properties are default or not.
A non-default property cannot be overriden.
A default property can be overriden by a property
(default or non-default) of the same name bound to
a decendent class loader.public static java.util.Enumeration<java.lang.String> propertyNames()
public static java.util.Properties getProperties()
java.util.Properties
instance
that is equivalent to the current state of the scoped
properties, in that getProperty() will return the same value.
However, this is a copy, so setProperty on the
returned value will not effect the scoped properties.private static final ManagedProperties.Value getValueProperty(java.lang.ClassLoader classLoader, java.lang.String propertyName)
classLoader
- The class loader as keypropertyName
- The property name to lookupprivate static final java.lang.ClassLoader getThreadContextClassLoader()
private static final java.lang.ClassLoader getParent(java.lang.ClassLoader classLoader)
classLoader
- The class loader from wich the parent has to be extracted