|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object nl.chess.it.util.config.Config
Configuration class. It's most important feature is an extensive self-validation call (
validateConfiguration()
) that not only makes sure that required properties exists, but
also that they are of the correct type and don't cause exceptions.
Partly based on the old Config class by Rolf Heller.
Constructor Summary | |
protected |
Config(java.util.Properties properties)
Constructs a Config based on an existing Properties. |
protected |
Config(java.lang.String resourceName)
Constructs a Config based on a resource. |
Method Summary | |
protected boolean |
getBoolean(java.lang.String key)
Gets the value for the given key. |
protected java.util.Date |
getDate(java.lang.String key)
|
protected long |
getDurationInMs(java.lang.String key)
Returns a duration. |
protected java.io.File |
getExistingReadableFile(java.lang.String key)
|
protected int |
getInt(java.lang.String key)
|
protected java.lang.String |
getMessageFormatPattern(java.lang.String key,
java.lang.Object[] testArguments)
Checks if the value stored under the given key is a pattern that can be used to construct a MessageFormat. |
protected long |
getSizeInBytes(java.lang.String key)
|
protected java.lang.String |
getString(java.lang.String key)
Gets the String value for the given key. |
protected java.lang.String |
getString(java.lang.String key,
java.lang.String expectedType)
Gets the value String for the given key. |
protected java.lang.String[] |
getStringArrayFromCommaString(java.lang.String key)
Gets the value for the given key. |
protected java.lang.String[] |
getStringArrayFromCommaString(java.lang.String key,
java.lang.String itemsDescription)
Gets the value for the given key. |
protected java.net.URL |
getURL(java.lang.String key)
Gets the value for the given key. |
protected java.lang.String |
getValue(java.lang.String key)
This method gives access to the underlying Properties this class is wrapped around. |
protected java.io.File |
getWritableFile(java.lang.String key)
|
protected boolean |
hasValue(java.lang.String key)
Indicates whether or not a value is given for this key. |
protected void |
testMessageFormat(java.lang.String key,
java.lang.String pattern,
java.lang.Object[] testArguments)
Verifies that the given pattern can be used to construct a MessageFormat. |
ConfigValidationResult |
validateConfiguration()
Validates the configuration properties. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
protected Config(java.util.Properties properties)
properties
- Cannot be null
.
java.lang.NullPointerException
- If properties is null
.protected Config(java.lang.String resourceName) throws ConfigurationException
resourceName
- Name of the resource to look for, as used in ClassLoader.getResource(java.lang.String)
. Cannot be null
.
java.lang.NullPointerException
- If resourceName is null
.
ConfigurationException
Method Detail |
public final ConfigValidationResult validateConfiguration()
null
.protected final java.lang.String getString(java.lang.String key)
getValue(String)
for that.
key
- Key to search on.
null
, always trimmed()
.
MissingPropertyException
- if the key does not result in a value.protected final java.lang.String getString(java.lang.String key, java.lang.String expectedType)
key
- Key to search on.expectedType
- Description of the expected type of the value of the key we're looking
for. Used in error messages.
null
, always trimmed()
.
MissingPropertyException
- if the key does not result in a value.protected java.lang.String getValue(java.lang.String key)
null
if such a value cannot be found.protected final int getInt(java.lang.String key)
protected final long getSizeInBytes(java.lang.String key)
protected final long getDurationInMs(java.lang.String key)
key
- Key to search on.
protected final java.lang.String getMessageFormatPattern(java.lang.String key, java.lang.Object[] testArguments)
testMessageFormat(String, String, Object[])
for more information.
key
- String Key that will be used to retrieve the messageformat pattern. Cannot be
null
.testArguments
- Arguments to test the pattern with. Cannot be null
.
protected final void testMessageFormat(java.lang.String key, java.lang.String pattern, java.lang.Object[] testArguments)
Verifies that the given pattern can be used to construct a MessageFormat. The testArguments array should contains values that represent reasonable values that will be used for this messageFormat.
Example: In your properties file you have the following messageformat pattern:
files.description=There {0,choice,0#are no files|1#is one file|1 <are {0,number,integer} files}
You could use the following calls in your getXxx() method to testMessageFormat to verify the
messageformat deals with a variety of numbers correctly:
public String getFilesDescription() {
String key = "files.description";
String value = getString(key, "MessageFormat string");
testMessageFormat(key, value, new Object[] {new Integer(0)}); // tests the {0, choice, 0#} part
testMessageFormat(key, value, new Object[] {new Integer(1)}); // tests the {0, choice ... |1#} part
testMessageFormat(key, value, new Object[] {new Integer(10)});// tests the {0, choice ... <1} part
return value;
}
The above code would for example make sure the following entry in a property-file is not
accepted:
files.description=There {0,choice,0#are no files|1#is one file|1 <are {0,invaliddatatype} files}
key
- Key that was used to retrieve the messageformat pattern.pattern
- The messageformat pattern to test.testArguments
- Arguments to test the pattern with.protected final java.util.Date getDate(java.lang.String key)
protected final java.lang.String[] getStringArrayFromCommaString(java.lang.String key, java.lang.String itemsDescription)
key
- Key to search on.itemsDescription
- Description of individual values, should be written in plural.
Example: "email adresses". Optional, can be null
.
null
, might be empty (zero elements).protected final java.lang.String[] getStringArrayFromCommaString(java.lang.String key)
key
- Key to search on.
null
, might be empty (zero elements).protected final java.net.URL getURL(java.lang.String key)
key
- Key to search on.
null
.protected final boolean getBoolean(java.lang.String key)
true
or
false
. The following texts are recognized: on, true, yes and 1, and off,
false, no and 0. Any other value causes an exception.
key
- Key to search on.
InvalidPropertyException
- If the value cannot be parsed as a boolean.protected final java.io.File getExistingReadableFile(java.lang.String key)
protected final java.io.File getWritableFile(java.lang.String key)
protected final boolean hasValue(java.lang.String key)
key
- Cannot be null
.
true
if a value has been given; false
otherwise.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |