This class is a container of objects of type ConfigurationPlugin, sorted by their priority and provides a single user interface to query configuration data from those.
Interface methods are implemented synchronized.
By default, all category and variable names are case insensitive. This is at least true for the default plug-ins delivered with ALib.
Variables by default can contain other variables, which are recursively substituted by this method. For more information about the (adjustable) syntax, see Variable Substitution.
See documentation of namespace cs.aworx.lib.config for more information on ALib external configuration variables.
Inner Classes | |
class | PluginAndPrio |
Public Static Fields | |
const int | PrioAutodetect = 500 |
const int | PrioCLIArgs = 40000 |
const int | PrioDefaultValues = 10000 |
const int | PrioEnvironment = 30000 |
const int | PrioProtectedValues = int.MaxValue |
const int | PrioStandard = 20000 |
Public Fields | |
NumberFormat | NumberFormat = new NumberFormat() |
char[] | SubstitutionVariableDelimiters |
AString | SubstitutionVariableEnd = new AString() |
AString | SubstitutionVariableStart = new AString("$") |
List< AString > | TrueValues |
Public Methods | |
Configuration (bool addDefaultPlugins=true) | |
int | FetchFromDefault (ConfigurationPlugin dest, String sectionName=null) |
ConfigurationPlugin | GetPlugin (int priority) |
TPluginType | GetPluginTypeSafe< TPluginType > () |
TPluginType | GetPluginTypeSafe< TPluginType > (int priority) |
bool | HasPlugins () |
void | InsertPlugin (ConfigurationPlugin plugin, int priority) |
bool | IsTrue (AString value) |
int | Load (Variable variable) |
int | LoadFromString (Variable variable, Object externalizedValue) |
int | loadImpl (Variable variable, bool substitute) |
int | Protect (Variable variable, Object externalizedValue=null) |
bool | RemovePlugin (ConfigurationPlugin plugin) |
void | SetCommandLineArgs (String[] args=null) |
int | Store (Variable variable, Object externalizedValue=null) |
int | StoreDefault (Variable variable, Object externalizedValue=null) |
Protected Fields | |
List< PluginAndPrio > | plugins = new List<PluginAndPrio>() |
AString | tmpConversion = new AString() |
Substring | tmpReplVarAll = new Substring() |
Substring | tmpReplVarCategory = new Substring() |
Substring | tmpReplVarName = new Substring() |
|
inline |
Constructs a Configuration. If addDefaultPlugins is true
, registers the initial plug-ins as follows
Priority | Plug-in Type |
---|---|
Configuration.PrioDefaultValues | InMemoryPlugin |
Configuration.PrioEnvironment | Environment |
Configuration.PrioCLIArgs | CLIArgs |
Configuration.PrioProtectedValues | InMemoryPlugin |
addDefaultPlugins | If true , the default plugins are added. Defaults to true . |
|
inline |
This method fetches all values from a plug-in of priority PrioDefaultValues, which are not present in the given plug-in dest and stores them in that. This is useful to collect all generated default values and store them in a users' configuration file. This way, the user can identify configurable options easily.
dest | The destination plug-in. |
sectionName | Optional string denoting a section to fetch. Defaults to null . |
|
inline |
Returns the plug-in with the given priority. If the plug-in does not exist, null
is returned.
Usually this method is used in the termination section of a software, when no parallel threads are active.
It may be used for example to fetch default variables into a configuration file.
priority | The priority of the plug-in to return. |
null
if not available.
|
inline |
Searches all plug-ins for the first found with type TPluginType.
TPluginType | The type of the plugin to search. |
null
if not available. TPluginType | : | ConfigurationPlugin |
|
inline |
Same as GetPlugin, but converts the plug-in found to the template type. A type-check is performed. If the plugin has a different type, null
is returned.
priority | The priority of the plug-in to return. |
TPluginType | The type of the plugin to search. |
null
if not available. TPluginType | : | ConfigurationPlugin |
|
inline |
Checks if any plug-in is attached. This is useful if optional configuration objects are used. In case no plug-in was attached (by a third party), the effort to declare and search a variable can be omitted.
true
if this object has any plugin set, false
otherwise.
|
inline |
Adds the given plug-in to the list of plug-ins used to query configuration values. Higher numbers for parameter priority, prioritize this plug-in against those with lower values. Default values are provided with static constant fields PrioCLIArgs, PrioEnvironment and PrioStandard.
plugin | The plug-in to insert. |
priority | The priority of the plug-in. |
|
inline |
Utility method that checks if a given value represents boolean true. Uses field TrueValues. Comparisons are made case insensitive.
value | The value to check |
true
, if the value represents 'true', false
if not.
|
inline |
Receives and optionally creates configuration variable.
If the variable was not found and Variable.DefaultValue in variable is set, the method adds the value value to a plug-in of priority PrioDefaultValues, (respectively to the plug-in found at or below PrioDefaultValues). For the conversion of the value, field ConfigurationPlugin.StringConverter of field a plug-in of priority PrioDefaultValues, is used. (Which has to be present.)
variable | The variable to receive. |
0
if not found.
|
inline |
Convenience method to set values according to the provided string. For the conversion of the "externalized" string, method XTernalizer.LoadFromString of field ConfigurationPlugin.StringConverter of a plug-in of priority PrioDefaultValues, is used.
variable | The variable object. |
externalizedValue | The new value to write. |
|
inline |
Implementation of get method. No locking is performed (has to be done before invoking this method)
variable | The variable to get. |
substitute | If false , automatic variable substitutions are suppressed. |
|
inline |
Convenience method that stores the variable with priority Configuration.PrioProtectedValues.
The variable value is determined as follows:
variable | The variable object. |
externalizedValue | Optional externalized value string. If given, the variable is set prior to writing. |
|
inline |
Remove the given plug-in from the list of plug-ins. Responsibility for deletion of removed plug-ins moves to the remover. Active (inserted) plug-ins get deleted by class' destructor.
plugin | The plug-in to be deleted. |
|
inline |
Sets the command line arguments for default plug-in CLIArgs.
args | Parameters taken from standard C# method main() (the list of command line arguments). Defaults to null. |
|
inline |
Writes the variable into one of the plug-ins registered with this configuration object. The plug-ins are looped in descending order of priority, until a first plug-in confirms the variable to be written.
The first (most highly prioritized) plug-in to start the loop with, is defined by field Variable::Priority of the given variable.
If this is unset (0
), which is the default value of the field for freshly declared variable objects, the starting priority value of the loop is detected.
Detection is made by searching the variable in the plug-ins prior to storing it. The search order is likewise by priority, starting with the highest. If the variable was not found, PrioDefaultValues is chosen. Usually the writing loop then will also already end there, because standard configuration sets have a write-enabled in-memory plug-in at that priority.
If the variable was found in the plug-in of priority PrioProtectedValues, the method stops without storing anything.
This approach of storing variables, supports various use cases very nicely. Some of the frequent ones shall be named here:
0
), allows to store the variable in the plug-in that it was originally read from, or - if not stored already - in default values.- 1
, allows to store the variable just in the plug-in with highest possible priority.The method returns the priority of the configuration plug-in that the value was written to. This value is as well stored in field Variable::Priority of the given object.
A result of 0
(no priority) indicates that the variable was not written. This might only happen if
0
),Optional parameter externalizedValue allows to provide a string that is parsed by the storing plug-in to reset the variables' values prior to writing.
variable | The variable object. |
externalizedValue | Optional externalized value string. If given, the variable is set prior to writing. |
|
inline |
Convenience method that stores the variable with priority Configuration.PrioDefaultValues.
The variable value is determined as follows:
variable | The variable object. |
externalizedValue | Optional externalized value string. If given, the variable is set prior to writing. |
NumberFormat NumberFormat = new NumberFormat() |
Number format definition used to read and write int and float values.
|
protected |
The plug-ins we have in our portfolio
|
static |
Constant providing a priority which is even lower than default. This priority is not used by any standard plug-ins and currently not even anywhere in core ALib. The rational to provide it is for third party libraries that may use this value in cases where values are estimated or detected - instead of defaulted. A user of the library may then overwrite such auto-detection by setting a default value in the configuration.
|
static |
Constant providing default priority for IniFile (or similar) external plug-ins.
|
static |
Constant providing a low priority. This is used with PrioDefaultValues
|
static |
Constant providing default priority for environment variables plug-in.
|
static |
Constant providing the highest possible priority. This is used with PrioProtectedValues
|
static |
Constant providing default priority for command-line parameters plug-in.
char [] SubstitutionVariableDelimiters |
The delimiters used to identify the end of a substitutable variable. If field SubstitutionVariableEnd is not empty, this field is ignored. Otherwise, all characters defined in this string are used to identify the end of a substitution variable.
The end of a substitution variables. If this field is set, then field SubstitutionVariableDelimiters is ignored. If this field is null (the default) or empty, it is ignored and characters in field SubstitutionVariableDelimiters are used to identify the end of the variable.
The start string to identify substitution variables. Defaults to single character '$'
. If a string is set, i.e. "${"
, then field SubstitutionVariableEnd may be set accordingly, i.e. "}"
A temporary object used with variable substitution
A temporary object used with variable substitution
A temporary object used with variable substitution
List<AString> TrueValues |
Values considered to indicate "true". Defaults to { "1", "true", "t", "yes", "y", "on", "ok" }. See methods IsTrue.
Application specific values (e.g. localization languages) might be added by publicly accessing this field and adding new values (or removing existing).