Alite Config Reader¶
Alite Config Reader is a implementation of Alite
ConfigurationInterface backed by Groovy Config Slurper. It provides type-safe easy way of integrating configurations into java code.
The Config Reader is designed to be simple and easy to use. If you want more advanced behaviour, try Template_Groovy_Configurator, which provide these advanced behaviour with cost of greater complexity. The advantage is that both, Config Reader and Template Groovy Configurator, implement the same
ConfigutarionInterface. This allows to switch between quite seamlessly. You just need to change the code for loading the configuration. The rest will remain the same. One possible way is to start your project with Config Reader and if the need for more advanced configuration arises you can just switch to Template_Groovy_Configurator.
The groovy configuration can be loaded by
loadAndMergeScriptText methods. The first one is designed to read from a file given by filename or instance of the
File class or from by file given by
URI. The latter handles given
String as groovy configuration text.
As the name of methods suggests it loads the content and merges it into existing configuration. If the method is called for the first time, the configuration is just simply loaded. However after that every next loaded configuration (incoming) is merged into the loaded one (existing). The merge works as follows: For each incoming configuration key it looks into existing configuration for the key with the same name. If this key is found, its value is overwritten by incoming one. Otherwise incoming key and value are simply added to existing configuration.
For example let's have two configuration files:
fruit.apple.color = "red" fruit.banana.color = "green"
fruit.banana.color = "yellow" fruit.kiwi.type = "It's not a fruit, it's a bird!"
If we now load
config1.groovy file, the contend stored in the
ConfigReader object will be identical with the file. Then if we load
config2.groovy into the same
ConfigReader instance, configuration stored will be as follows:
fruit.apple.color = "red" fruit.banana.color = "yellow" fruit.kiwi.type = "It's not a fruit, it's a bird!"
The value with key
fruit.apple.color has been kept the same, value with key
fruit.banana.color was overwritten into
"yellow" and value with its key
fruit.kiwi.type was added. This feature allows you to enhance configuration by exception paradigm: The default configuration is stored in main configuration file. Program takes as argument another groovy configuration (file name or groovy text) which contains keys and values that user wants to be different from their default version.
Because the Config Reader is backed by the Config Slurper class, it is strongly advised to not use
environment keyword as first word for configuration key. This keyword in some cases might be interpreted by Config Slurper as "environment" keyword and whole program might misbehave. For more details about configuration environmen see Config Slurper website.
The Config Reader is an external part of the Alite toolkit. It can be obtained as maven dependency either in its stable version 3.0.1 or the possibly unstable but sometimes the newest snapshot version.
Maven dependecy decleration:
<dependency> <groupId>cz.agents.alite</groupId> <artifactId>configreader</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>cz.agents.alite</groupId> <artifactId>configreader</artifactId> <version>3.0-SNAPSHOT</version> </dependency>