Package de.mid.innovator.ui
Interface PropertyProvider
- All Known Implementing Classes:
PropertyProviderDefault
public interface PropertyProvider
A property provider is used to access an application's
arguments at runtime. It synchronizes command line
arguments with Java runtime properties and provides
a unified way to access, configure and parse properties.
The following example code shows how to use a property provider
to handle command line arguments:
private static final Property[] localProps = {
new PropertyCfg<String>("de.mid.myArg", "this is the description for de.mid.myArg"),
new PropertyCfg<Boolean>("de.mid.myFlag", false, "this is the description for de.mid.myFlag")
}
static public main(String[] argv) {
PropertyProvider propProv = new PropertyProviderDefault();
List<Property> tmp = Arrays.asList(localProps);
propProv.providePropertyList(tmp);
// further initialization
...
// provide and parse command line arguments
propProv.InitWithCommandLineArguments(argv);
}
- Author:
- borschet
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidDisables stringent validation of the configuration.voidEnables stringent validation of the configuration.booleangetBooleanProperty(String key) A property getter for Boolean properties.Class<?>[]Gets a list of classes from a given string property.Class<?> getClassProperty(String key) A property getter for class properties.<T extends Enum<T>>
TgetEnumProperty(Class<T> cls, String key) A property getter for enumeration type.File[]Gets a list of files given with a string property value.getFileProperty(String key) A property getter forFileproperties.intgetIntegerProperty(String key) A property getter for integer properties.booleanTries to invokegetBooleanProperty(String)and returns the given value, otherwise the configured default value is returned.Class<?>[]getOptionalClassListProperty(String key, Class<?>[] defaultValue) InvokesgetClassListProperty()Class<?> Tries to invokegetClassProperty(String)<T extends Enum<T>>
TgetOptionalEnumProperty(Class<T> cls, String key) Tries to invokegetEnumProperty(Class, String)File[]getOptionalFileListProperty(String key, File[] defaultValue) InvokesgetFileListProperty()Tries to invokegetFileProperty(String)intInvokesgetIntegerProperty()InvokesgetStringProperty()getStringProperty(String key) A property getter for string properties.voidInitWithCommandLineArguments(String[] args) Initializes the property provider with an array of command line argument strings, checks the given values against the configuration and prepares the values for use with the typed getter methods.booleanisPropertySet(String key) Returns true if the property with the given key is existing and set, else returns false.voidprovideProperty(Property prop) Adds precisely one property that can be used at runtime.voidprovidePropertyList(List<Property> props) Configures a list of valid properties to be used at runtime.voidsetProperty(String key, String value) Set the value for an existing property.
-
Method Details
-
InitWithCommandLineArguments
Initializes the property provider with an array of command line argument strings, checks the given values against the configuration and prepares the values for use with the typed getter methods. This means that the command line arguments are parsed according to the format "<key>=<value>". The property provider checks every given string argument so that they correspond with a valid configured property. If no such property can be found, an exception is raised. This behavior can be prevented by callingdisableValidateConfig()before the property provider is initialized.- Throws:
UnknownArgumentException- if the list of command line arguments contains a property that was not previously configured and this check is not disabled.
-
provideProperty
Adds precisely one property that can be used at runtime. -
providePropertyList
Configures a list of valid properties to be used at runtime. -
getAllValidProperties
- Returns:
- all previously configured properties.
-
enableValidateConfig
void enableValidateConfig()Enables stringent validation of the configuration.- See Also:
-
disableValidateConfig
void disableValidateConfig()Disables stringent validation of the configuration.- See Also:
-
getBooleanProperty
A property getter for Boolean properties.- Throws:
MissingArgumentException- if no such property could be found.InvalidArgumentException
-
getClassProperty
A property getter for class properties.- Throws:
InvalidArgumentException- if the property value cannot be converted into a valid class.MissingArgumentException- if no such property can be found.
-
getFileProperty
A property getter forFileproperties.- Returns:
- A file object created with
File(java.lang.String). - Throws:
MissingArgumentException- if no such property could be found.InvalidArgumentException
-
getIntegerProperty
A property getter for integer properties.- Throws:
InvalidArgumentException- if the property value cannot be converted into an integer.MissingArgumentException- if no such property can be found.
-
getStringProperty
A property getter for string properties.- Throws:
MissingArgumentException- if no such property can be found.
-
getEnumProperty
<T extends Enum<T>> T getEnumProperty(Class<T> cls, String key) throws MissingArgumentException, InvalidArgumentException A property getter for enumeration type.- Parameters:
cls- the property's enumeration typekey- the key value- Returns:
- an instance of the enumeration
- Throws:
InvalidArgumentExceptionMissingArgumentException- if no such property can be found.
-
getOptionalBooleanProperty
Tries to invokegetBooleanProperty(String)and returns the given value, otherwise the configured default value is returned.- Parameters:
key- the argument key value- Returns:
- true, if the property's string value is "true", otherwise false
- Throws:
InvalidArgumentException
-
getOptionalClassProperty
Tries to invokegetClassProperty(String)- Parameters:
key- the argument key value- Returns:
- the given value, otherwise the configured default value
- Throws:
InvalidArgumentException- in case the given string cannot be converted into a valid class.
-
getOptionalFileProperty
Tries to invokegetFileProperty(String)- Returns:
- the given value, otherwise the configured default value
- Throws:
InvalidArgumentException
-
getOptionalIntegerProperty
InvokesgetIntegerProperty()- Returns:
- its result if defined, otherwise the configured default value
- Throws:
InvalidArgumentException- If the property's string value cannot be converted into an integer.
-
getOptionalStringProperty
InvokesgetStringProperty()- Returns:
- its result if defined, otherwise the configured default value
-
getOptionalEnumProperty
<T extends Enum<T>> T getOptionalEnumProperty(Class<T> cls, String key) throws InvalidArgumentException Tries to invokegetEnumProperty(Class, String)- Parameters:
cls- the property's enumeration typekey- the key value- Returns:
- the given value, otherwise the configured default value
- Throws:
InvalidArgumentException- if the conversion from the value to a valid member of the given enumeration fails.
-
getClassListProperty
Class<?>[] getClassListProperty(String key) throws MissingArgumentException, InvalidArgumentException Gets a list of classes from a given string property. Each class specifications must be separated by aFile.pathSeparator.- Returns:
- an array of classes.
- Throws:
InvalidArgumentException- if an entry of the string list cannot be converted into a valid class.MissingArgumentException- if no such property can be found.
-
getFileListProperty
Gets a list of files given with a string property value. The single file specifications must be separated byFile.pathSeparator.- Returns:
- an array of file instances.
- Throws:
MissingArgumentException- if no such property can be found.InvalidArgumentException
-
getOptionalClassListProperty
Class<?>[] getOptionalClassListProperty(String key, Class<?>[] defaultValue) throws InvalidArgumentException InvokesgetClassListProperty()- Returns:
- its result if defined, otherwise the configured default value
- Throws:
InvalidArgumentException- if the property string value cannot be converted into a list of classes.
-
getOptionalFileListProperty
InvokesgetFileListProperty()- Returns:
- its result if defined, otherwise the configured default value
-
isPropertySet
Returns true if the property with the given key is existing and set, else returns false.- Parameters:
key- key of the property- Returns:
- true if property with key is set.
-
setProperty
Set the value for an existing property. If the property does not exist, nothing is done.- Parameters:
key-value-
-