Although configuration is important and essential in any application, there is no real standardization available today.
There were several attempts to create a standard around it so that each application could read his parameters in a consistent way. But as of today, there is no final standardization, nor within Java SE, not within Java EE.
Since it is so important, The Eclipse MicroProfile project created a specification for reading parameters in their Micro-services architecture. And it was almost the first thing they did (which highlights the importance they gave it). That work is now continued under the wings of the JCP (as JSR-382) so that it can become a real standard within Java SE and Java EE.
Instead of using other frameworks and libraries which have support for reading configuration, I wanted to switch to the MicroProfile Config project. Because I wanted to be prepared for the feature but encountered a few issues.
Adapted the code of MicroProfile Config API and Apache Geronimo config to Java 7 and added my own extensions which compose Atbash Config.
My issues with MicroProfile Config project
Issue is a heavy term, my requirements are not (yet) available within the Config project but the good thing is that they are planned within the JCP specification work.
I want to read configuration parameters
- With Java 7
- For any Java EE project, not limited to Eclipse MicroProfile applications.
- Use single artefact which contains the configuration parameters for different environments embedded in it (within different files)
- Use YAML structured configuration files.
And that is not possible for the moment because
- The MicroProfile Config project is build using Java 8 code structures. So it is not possible to run it in a Java 7 environment.
- The Apache Geronimo Configuration project implements the API and is a very good candidate to use in my Java EE programs. Because it is created as a standalone project (not as part of a server like other implementation such as Payara, KumuluzEE, WebSphere Liberty or others) and thus you can add this dependency to any Java EE application. But configuration file name is still related to MicroProfile (/META-INF/microprofile-config.properties).
- MicroProfile Config project has support for overruling properties by specifying them in the environment or as JVM system parameters. But there is no feature to define multiple files nor the ability to define multiple files within the artefact for each environment.
- Only properties files are supported. And when you have larger projects, it can be handy to use a YAML structure so that it is more clear which properties are closely related.
Adapt MicroProfile Config API and Apache Geronimo Configuration.
Except for the first issue, support for Java 7, my own configuration extension could be a solution for my requirements. So I had no other choice than to copy the code and adapt it. And although the code is licensed under the Apache OpenSource license (which allows me to do this) it doesn’t feel right. So I want to thank explicitly all those committers that created the code for their wonderful work and I also made explicitly a reference within the notice file and documentation.
Atbash configuration extension
The extension consist of a custom implementation of the ConfigSource interface. That way I’m able to register another source for reading configuration parameters and fulfill my other 3 requirements.
– Implement the BaseConfigurationName interface and define the class also for the ServiceLoader mechanism. Now I’m able to use any filename like demo.properties or demo.yaml when the value demo is specified as the base name.
– By specifying the environment with the JVM system parameter -S, I can add a prefix to the filename and supply additional or override parameters for a certain environment
– YAML file support is implemented with the use of Snakeyaml where the file is converted to key-value pairs.
For more information, you can read the manual.
There are 3 ways how you can use the Maven artefacts
1. Use plain MicroProfile config with Java 7.
Add the adapted Geronimo config code to your project
<dependency> <groupId>be.atbash.config</groupId> <artifactId>geronimo-config</artifactId> <version>0.9</version> </dependency>
2. Use the Atbash configuration extension with Java 7
You need the Geronimo config and atbash config project
<dependency> <groupId>be.atbash.config</groupId> <artifactId>geronimo-config</artifactId> <version>0.9</version> </dependency> <dependency> <groupId>be.atbash.config</groupId> <artifactId>atbash-config</artifactId> <version>0.9</version> </dependency>
3. Use the Atbash configuration extension with Java 8
In this scenario, you don’t need the adapted code and can use the regular implementations
<dependency> <groupId>org.apache.geronimo.config</groupId> <artifactId>geronimo-config-impl</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>be.atbash.config</groupId> <artifactId>atbash-config</artifactId> <version>0.9</version> <exclusions> <exclusion> <groupId>be.atbash.config</groupId> <artifactId>microprofile-config-api</artifactId> </exclusion> </exclusions> </dependency>
MicroProfile Config is the first real standardization around reading parameters for configuration. They have built their implementations on top of Java 8.
So when you need a general usable library with Java EE 7 (which is using Java 7 as a base version), you can use the Atbash config code (adapted from the MicroProfile Config API and Apache Geronimo config as implementation) which has a few handy extensions.
This makes you future proof at the time JSR-382 will be adopted by the different application servers.
Thank you for reading until the bottom of this blog entry and have ave fun.