|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectutil.PropertyContainer
public class PropertyContainer
An implementation of the PropertyBearing interface. Provides a
protected constructor with a single Map object. All of
the interface methods are implemented with a Map object.
As of version 1.1, this class already uses the Syslog object for
logging rather than the old Message class.
As of version 2.0, this class does not store String values but rather
objects of the appropriate type. String values are permitted, though, and
are converted (either using number parsing or construction with a
single-string constructor) on first retrieval.
As of version 2.1, characters are supported as primitive types.
As of version 3.0, the special key PropertyBearing.KEY_URLRESOURCES may point
to a comma-separated list of url-resources. If present, a
URLClassLoader is constructed from the urls specified, and
resources can be queried using getResource(java.lang.String) and
getResourceAsStream(java.lang.String).
A note on serializing property containers: The generic class does not
implement the Serializable interface, though many daughters
might need this behaviour. If you only make a daughter class serializable,
the (de)serializing process acts on the default (=empty) contructor of
PropertyContainer thus The properties in the info map are empty!
Think of writing a customized #writeObject
and #readObject method instead.
PropertySyslog| Nested Class Summary | |
|---|---|
static class |
PropertyContainer.URLResource
A test class for url-class loaders. |
| Field Summary | |
|---|---|
private List<String> |
bundlenames
The list of bundle names. |
private Map<String,ResourceBundle> |
bundles
A mapping of resource bundle names to resource bundle objects. |
private static boolean |
DEFURLUSECONFIG
Default, not search the current directory for resources. |
private static boolean |
DEFURLUSECURRENT
Default, not search the current directory for resources. |
private static boolean |
DEFURLUSEHOME
Default, not search user's home directory for resources. |
private Map<String,Object> |
info
The properties of this object. |
static String |
KEY_LISTSEPARATOR
The key for list separators. |
static String |
KEY_LOCALECOUNTRY
The key to the locale country. |
static String |
KEY_LOCALELANGUAGE
The key to the locale language. |
static String |
KEY_MAPKEYVALUECHAR
The key for map key-value separator. |
static String |
KEY_MAPSEPARATOR
The key for list separators. |
static String |
KEY_NOINITONCREATE
If true, new instances are not initalized during creation. |
static String |
KEY_RESOURCEBUNDLES
The key listing resource bundles. |
private Set<URL> |
localurl
A private set of URLS if the PropertyBearing.KEY_URLRESOURCES is defined. |
private static ClassLoader |
locate
An url-class loader constructed for all property containers. |
private Locale |
region
The locale used for this property container. |
private static Set<URL> |
urlset
A set of URLs for the class loader, for all property containers. |
| Fields inherited from interface util.PropertyBearing |
|---|
CONFIG, KEY_CLASS, KEY_URLRESOURCES, KEY_URLUSECONFIG, KEY_URLUSECURRENT, KEY_URLUSEHOME |
| Constructor Summary | |
|---|---|
protected |
PropertyContainer()
Default constructor. |
|
PropertyContainer(Map<String,String> props)
Constructs a new property container. |
| Method Summary | ||
|---|---|---|
static Map<String,String> |
augment(Map<String,String> def,
Map<String,String> tokens)
Augments or overrides parts of a mapping which may be a default mapping gained by the non-static method #getDefaultProperties by key-value pairs
given in the argumental map. |
|
static Map<String,String> |
augment(Map<String,String> def,
String tokens)
Augments or overrides parts of a mapping which may be a default mapping gained by the non-static method #getDefaultProperties by key-value pairs
given in the argumental string. |
|
static Map<String,String> |
augment(Map<String,String> def,
String tokens,
String delim,
String key)
Augments or overrides parts of a mapping which may be a default mapping gained by the non-static method #getDefaultProperties by key-value pairs
given in the argumental string. |
|
private void |
checkProperties()
We check the mapping found in our properties against any defined public static fields starting with KEY_. |
|
Object |
clone()
Clones this property container. |
|
static Object |
createFrom(Map<String,String> prop)
Constructs a new property container from a given mapping. |
|
static
|
createFrom(Map<String,String> prop,
Class<T> to)
If the PropertyBearing.KEY_CLASS is not set in the properties, the name of the
argumental class is put into the mapping before a new object is
created using the mian method. |
|
static Object |
createFrom(String classkey,
Map<String,String> prop)
Constructs a new property container from a given mapping. |
|
private static Object |
createWith(Constructor<?> create,
Object with)
Create an object with a constructor and a single argument. |
|
boolean |
defaultBoolean(String key,
boolean def)
Tests if the given key is present in the properties of this container. |
|
boolean |
defaultChar(String key,
char def)
Tests if the given key is present in the properties of this container. |
|
boolean |
defaultDouble(String key,
double def)
Tests if the given key is present in the properties of this container. |
|
boolean |
defaultFloat(String key,
float def)
Tests if the given key is present in the properties of this container. |
|
boolean |
defaultInt(String key,
int def)
Tests if the given key is present in the properties of this container. |
|
boolean |
defaultLong(String key,
long def)
Tests if the given key is present in the properties of this container. |
|
boolean |
defaultObject(String key,
Class def,
String defstring)
Tests if the given key is present in the properties of this container. |
|
boolean |
defaultObject(String key,
Object def)
Tests if the given key is present in the properties of this container. |
|
Map<String,Object> |
defaultProperties(Map<String,String> defaults)
Sets default mappings for this object. |
|
boolean |
defaultProperty(String key,
String def)
Tests if the given key is present as a map to a string in this container. |
|
boolean |
getAsBoolean(String key)
Returns the property mapped to the given key as a primitive boolean. |
|
char |
getAsChar(String key)
Returns the property mapped to the given key as a primitive char. |
|
double |
getAsDouble(String key)
Returns the property mapped to the given key as a primitive double. |
|
|
getAsEnums(String key,
Class<T> cls)
|
|
float |
getAsFloat(String key)
Returns the property mapped to the given key as a primitive float. |
|
int |
getAsInt(String key)
Returns the property mapped to the given key as a primitive int. |
|
List<String> |
getAsList(String key)
Returns a comma-separated string as a list of Strings. |
|
long |
getAsLong(String key)
Returns the property mapped to the given key as a primitive long. |
|
Map<String,String> |
getAsMap(String key)
Returns a colon-equals devided string as a mapping of string keys to string values. |
|
protected Map<String,String> |
getAsMap(String key,
String delim,
String col)
Returns a colon-equals devided string as a mapping of string keys to string values. |
|
Object |
getAsObject(String key)
Returns the direct mapping of the key. |
|
|
getAsObject(String key,
Class<T> oftype)
Returns the direct mapping of the key. |
|
private static Object |
getInstance(Class<?> cls,
String init)
Tries to instantiate a new instance of a class using a single-string constructor. |
|
static ClassLoader |
getLocalClassLoader()
Returns the local class loader. |
|
Object |
getLocalized(String key)
Scans all known resource bundles for the specified key. |
|
Object |
getLocalized(String key,
String bundlename)
Loades a localized resource from a resource bundle. |
|
String |
getLocalizedString(String key)
Scans all known resource bundles for the specified key. |
|
String |
getLocalizedString(String key,
String bundlename)
Gets a localized string version from a resource. |
|
Map<String,Object> |
getProperties()
Returns the entire properties of this object as a map. |
|
static Map<String,String> |
getPropertiesToKey(PropertyBearing p,
String key)
Retrieves properties to the stated key, or null, if the key is not linked. |
|
Map<String,String> |
getPropertiesToKey(String key)
Scans my properties for the resource. |
|
String |
getProperty(String key)
Returns a special property of this property container. |
|
URL |
getResource(String value)
Locates a resource using either the default class loader or the URLClassLoader defined with PropertyBearing.KEY_URLRESOURCES. |
|
InputStream |
getResourceAsStream(String key)
Locates a resource using either the default class loader or the URLClassLoader defined with PropertyBearing.KEY_URLRESOURCES. |
|
URL |
getResourceFromKey(String key)
Locates a resource from a propty in this property container. |
|
Enumeration |
getResources(String key)
Locates a resource using either the default class loader or the URLClassLoader defined with PropertyBearing.KEY_URLRESOURCES. |
|
boolean |
has(String key)
Tests if this property container contains a certain property. |
|
void |
init()
Loads URL-resources, if there are any. |
|
protected boolean |
isNew(String oldval,
String newval)
Returns true, if either, but not both strings are null or if they are different. |
|
static Object |
keyCreate(PropertyBearing p,
String key)
Creates an object from a key name in a property container. |
|
protected Object |
keyCreate(String key)
Creates an object from a property key name. |
|
private ResourceBundle |
loadBundle(String name)
Loads a resource bundle using a locale defined with the two KEY_LOCALELANGUAGE and KEY_LOCALECOUNTRY keys. |
|
private Set<URL> |
loadResource()
This method parses through the URL's provided in PropertyBearing.KEY_URLRESOURCES. |
|
Object |
parseObject(String key,
Class forclass)
Parses a property. |
|
double |
reload()
Does a reload of myself, if I'm reloadable. |
|
double |
reload(Reloadable self)
Use my properties, see if I can establish the state of the resource my properties were constructed from, and if their state has changed, reload them and call with each key found in the newly loaded properties the rescanned method of the reloadable object. |
|
Object |
removeProperty(String key)
Removes the property of the given name. |
|
protected double |
rescanned(String key,
String old,
String newval)
We provide this abstract method for subclasses that want to be Reloadable instances, but do not implement this interface
itself. |
|
private static Constructor<?> |
scanFor(Constructor[] all,
Class[] params)
Scans and returns the constructr with the given argument map. |
|
Object |
setObject(String key,
Object val)
Sets a special property to an object rather then a string. |
|
protected void |
setProperties(Map<String,Object> prop)
Needed for daughter classes that have to set the properites directly. |
|
String |
setProperty(String key,
String val)
Sets a special property of this property container. |
|
Map<String,String> |
stringProperties()
Retruns a mapping where all objects are replaced by their string representations. |
|
String |
toString()
A string description, returning the string description of the properties of this container. |
|
| Methods inherited from class java.lang.Object |
|---|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final String KEY_NOINITONCREATE
public static final String KEY_LOCALELANGUAGE
public static final String KEY_LOCALECOUNTRY
public static final String KEY_RESOURCEBUNDLES
public static final String KEY_LISTSEPARATOR
public static final String KEY_MAPSEPARATOR
public static final String KEY_MAPKEYVALUECHAR
private static final boolean DEFURLUSEHOME
private static final boolean DEFURLUSECURRENT
private static final boolean DEFURLUSECONFIG
private static Set<URL> urlset
private static ClassLoader locate
private Map<String,Object> info
private Locale region
private Set<URL> localurl
PropertyBearing.KEY_URLRESOURCES is defined.
private List<String> bundlenames
private Map<String,ResourceBundle> bundles
| Constructor Detail |
|---|
protected PropertyContainer()
PropertyContainer(Map) with
a null-argument.
public PropertyContainer(Map<String,String> props)
PropertyBearing.KEY_URLRESOURCES is present in the properties, a new
URLClassLoader is constructed using the tokenized value of the key
as URL-addresses. If no such value is set, the default class loader is
used, which allows access to resources in the classpath.
| Method Detail |
|---|
public static final Map<String,String> augment(Map<String,String> def,
String tokens)
#getDefaultProperties by key-value pairs
given in the argumental string. The formatting must follow the
StringTools#propertize(String) syntax.
tokens - key=value pairs, separated by a colon.
public static final Map<String,String> augment(Map<String,String> def,
String tokens,
String delim,
String key)
#getDefaultProperties by key-value pairs
given in the argumental string. The formatting must follow the
StringTools#propertize(String) syntax.
tokens - key=value pairs, separated by a colon.
public static final Map<String,String> augment(Map<String,String> def,
Map<String,String> tokens)
#getDefaultProperties by key-value pairs
given in the argumental map. This method returns the new mapping
and thus can be used in constructors, unlike Map.putAll(java.util.Map extends K, ? extends V>) that
returns void.
public void init()
init in interface Initializableprivate void checkProperties()
public static fields starting with KEY_.
public Object clone()
clone in class Objectpublic Map<String,Object> getProperties()
getProperties in interface PropertyBearingpublic Map<String,String> stringProperties()
Patterned are
converted via Patterned.toPattern(), objects that implement
a method like this are also converted likewise. The toString()
method is only used as a last resort.
stringProperties in interface PropertyBearingpublic Map<String,Object> defaultProperties(Map<String,String> defaults)
defaultProperties in interface PropertyBearingdefaults - Default mappings of keys to objects.public double reload()
Reloadable interface.
public double reload(Reloadable self)
protected double rescanned(String key,
String old,
String newval)
Reloadable instances, but do not implement this interface
itself. Subclasses should always call this method before rescanning
on their own. We throw an illegal arguement exception if the
PropertyBearing.KEY_CLASS property has changed, but one on a change of
protected boolean isNew(String oldval,
String newval)
public String getProperty(String key)
getProperty in interface PropertyBearingkey - The key for the Properties object.
public Object parseObject(String key,
Class forclass)
parseObject in interface PropertyBearing
public String setProperty(String key,
String val)
setProperty in interface PropertyBearingkey - The key for this Property.value - The value of this Property.public Object removeProperty(String key)
removeProperty in interface PropertyBearing
public Object setObject(String key,
Object val)
setObject in interface PropertyBearingpublic boolean has(String key)
has in interface PropertyBearingpublic Object getAsObject(String key)
defaultObject(java.lang.String, java.lang.Object).
getAsObject in interface PropertyBearing
public <T> T getAsObject(String key,
Class<T> oftype)
defaultObject(java.lang.String, java.lang.Object).
getAsObject in interface PropertyBearing
public <T extends Enum<T>> List<T> getAsEnums(String key,
Class<T> cls)
public List<String> getAsList(String key)
getAsList in interface PropertyBearingpublic Map<String,String> getAsMap(String key)
getAsMap in interface PropertyBearing
protected Map<String,String> getAsMap(String key,
String delim,
String col)
public boolean getAsBoolean(String key)
getAsBoolean in interface PropertyBearingpublic int getAsInt(String key)
getAsInt in interface PropertyBearingpublic char getAsChar(String key)
getAsChar in interface PropertyBearingpublic long getAsLong(String key)
getAsLong in interface PropertyBearingpublic double getAsDouble(String key)
getAsDouble in interface PropertyBearingpublic float getAsFloat(String key)
getAsFloat in interface PropertyBearing
public boolean defaultProperty(String key,
String def)
defaultProperty in interface PropertyBearingkey - The properties key to test for a present mapping.def - The default value of the key. Set if not present.
public boolean defaultInt(String key,
int def)
defaultInt in interface PropertyBearingkey - The properties key to test for a present mapping.def - The default value of the key. Set if not present.
public boolean defaultChar(String key,
char def)
defaultChar in interface PropertyBearingkey - The properties key to test for a present mapping.def - The default value of the key. Set if not present.
public boolean defaultDouble(String key,
double def)
defaultDouble in interface PropertyBearingkey - The properties key to test for a present mapping.def - The default value of the key. Set if not present.
public boolean defaultLong(String key,
long def)
defaultLong in interface PropertyBearingkey - The properties key to test for a present mapping.def - The default value of the key. Set if not present.
public boolean defaultFloat(String key,
float def)
defaultFloat in interface PropertyBearingkey - The properties key to test for a present mapping.def - The default value of the key. Set if not present.
public boolean defaultBoolean(String key,
boolean def)
defaultBoolean in interface PropertyBearingkey - The properties key to test for a present mapping.def - The default value of the key. Set if not present.
public boolean defaultObject(String key,
Object def)
defaultObject in interface PropertyBearingkey - The properties key to test for a present mapping.def - The default value of the key. Set if not present.
public boolean defaultObject(String key,
Class def,
String defstring)
defaultObject in interface PropertyBearingkey - The properties key to test for a present mapping.def - The default value of the key. Set if not present.defstring - The default constructor string.
public URL getResource(String value)
PropertyBearing.KEY_URLRESOURCES. As for the
general contract for resource-finding, first the main class loader
tries to locate the resource and only if it failes, the URL's are
searched.
getResource in interface PropertyBearingpublic URL getResourceFromKey(String key)
getResource(java.lang.String).
public static ClassLoader getLocalClassLoader()
public Enumeration getResources(String key)
throws IOException
PropertyBearing.KEY_URLRESOURCES. As for the
general contract for resource-finding, first the main class loader
tries to locate the resource and only if it failes, the URL's are
searched.
getResources in interface PropertyBearingIOExceptionpublic InputStream getResourceAsStream(String key)
PropertyBearing.KEY_URLRESOURCES. As for the
general contract for resource-finding, first the main class loader
tries to locate the resource and only if it failes, the URL's are
searched.
getResourceAsStream in interface PropertyBearing
public Object getLocalized(String key,
String bundlename)
public String getLocalizedString(String key,
String bundlename)
public Object getLocalized(String key)
KEY_RESOURCEBUNDLES order. As a last resort, the properties
of the property container are queried.
getLocalized in interface PropertyBearingpublic String getLocalizedString(String key)
KEY_RESOURCEBUNDLES order. As a last resort, the properties
of the property container are queried.
public String toString()
toString in class Objectpublic Map<String,String> getPropertiesToKey(String key)
PropertyLoader.load(URL), which currently returns a map with
at least three keys, even if the reource is empty.
public static Map<String,String> getPropertiesToKey(PropertyBearing p,
String key)
p - A property bearing instance, mostly myself.key - the key to scan for.
public static final <T> T createFrom(Map<String,String> prop,
Class<T> to)
PropertyBearing.KEY_CLASS is not set in the properties, the name of the
argumental class is put into the mapping before a new object is
created using the mian method. This object is then cast to the
class reference type. This means, if the class key is present, the argument class can also be a superclass/interface of the desired object. If the key is missing, the class must be instantiable.
protected Object keyCreate(String key)
public static final Object keyCreate(PropertyBearing p,
String key)
public static final Object createFrom(Map<String,String> prop)
PropertyBearing.KEY_CLASS key must at least be present. If the object
constructed implements the Initializable field, the init method
is called unless a KEY_NOINITONCREATE property is
present and points to true.
prop - A mapping, most likely a properties object.
public static final Object createFrom(String classkey,
Map<String,String> prop)
Initializable field, the init method
is called unless a KEY_NOINITONCREATE property is
present and points to true.
prop - A mapping, most likely a properties object.
private static Constructor<?> scanFor(Constructor[] all,
Class[] params)
private static final Object createWith(Constructor<?> create,
Object with)
throws InvocationTargetException,
InstantiationException,
IllegalAccessException
InvocationTargetException
InstantiationException
IllegalAccessExceptionprotected void setProperties(Map<String,Object> prop)
private Set<URL> loadResource()
PropertyBearing.KEY_URLRESOURCES. Each valid URL found is added to a local set.
If this class defines new URLs, as checked against the #urls
set, the class-loader is reconstructed to reflect the new class loader.
private ResourceBundle loadBundle(String name)
KEY_LOCALELANGUAGE and KEY_LOCALECOUNTRY keys.
Additionally, the PropertyBearing.KEY_URLRESOURCES are used for locating the
named resource bundle.
private static final Object getInstance(Class<?> cls,
String init)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||