tools4j-config part 1, introduction
Tools4j-config 0.0.1 was released in Maven Central Repository about a week ago, a framework that aims to support creating configurable applications in a productive and consistent way.
This is a quick introduction on the possibilities for defining configuration and constraints, and how to administrate it for applications.
Prerequisites
Tools4j-config is fully functional in any Java SE 6+ compatible environment and is distributed as a set of Maven 3+ projects.
Quickstart
First we’ll define configuration used by our application. Create a maven project with the following dependencies.
| 1 2 3 4 5 6 7 8 9 10 11 12 |
|
Let’s assume we need access to a database, so we create a class that will represent the configuration needed to connect to it.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Now register this class with the framework and read its configuration.
| 1 2 3 |
|
Done! Our application is now configurable. Pretty quick and simple.
But what did we read? Nothing actually. The list is empty since there is no configuration available yet. We need the administrator to provision configuration first. So let’s take the administrator perspective for a second.
Create a new maven project with the following dependencies.
| 1 2 3 4 5 6 7 8 9 10 11 12 |
|
Configuration is managed programmatically and this is how our imaginary administrator creates a database named user and sets values for it.
| 1 2 3 4 5 6 7 |
|
All good? Not quite. The code above fails twice.
Bean Database@user have a property java.net.URL@url with value /dev/null not matching its type.
Bean Database@user have a property java.lang.Integer@poolSize with value nonsense not matching its type.
These failures should be obvious. The administrator provisioned values that did not conform to the types of the configurable class, which brings up an important point: type safety. Administrators should not be able to (accidentally nor intentionally) break constraints of applications.
Assuming values are corrected, we can switch back to the application’s perspective and read the user instance.
| 1 |
|
The user instance is now initialized with values provisioned by the administrator and the application can read it without having to redeploy itself or restart the JVM.
At this very basic level it is important to notice a couple of things. Neither developer nor administrator made any assumptions on the runtime environment, nor did they know/care from where or how configuration was read/written.
Our quickstart use-case is complete but let’s dive a little deeper in order to understand the modelling capabilities of tools4j-config.
Built-in types
Configurable fields can have any of the following types.
java.lang.Stringjava.lang.Numberand derived typesjava.lang.Booleanjava.lang.Enumand derived types, including user-defined onesjava.util.Datejava.util.Currencyjava.util.Localejava.io.Filejava.net.InetAddressjava.net.URLjava.net.URIjavax.xml.datatype.Duration
Fields can also be declared as a java.util.Collection implementation, generified with any of the above types. Fields initialized at declaration are considered default values (provisioned values take precedence). It is possible to declare user-defined types, but it requires some extra effort and will be covered in a future post.
All of these declarations are valid (annotations omitted for brevity).
| 1 2 3 4 5 6 7 8 9 10 11 12 |
|
References
Configurable classes can have references to other configurable classes, including themselves (recursive relationships). Circular references are allowed as well (person ‘a’ is person ‘b’:s best friend and usually vice versa).
Declaring references is identical to regular Java types.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Referential integrity is enforced in order to keep relationships consistent. It is not possible create references to instances that does not exist; or remove instances that other instances already have references to.
Administrating references is almost to identical to provisioning regular Java type values.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Inheritance
Configurable classes support inheritance and configurable fields will be inherited from their parent class, enabling reuse of configurable fields and methods.
Validation
Tools4j-config integrates with JSR-303 Bean Validation to help developers to further constrain the premises under which configuration may be provisioned.
To enable Bean Validation we need to add the following dependencies to both maven projects mentioned earlier.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Next follows a hypothetical example where Bean Validation constraints are used to make sure that three properties of binary search trees are satisfied.
- The left subtree of a node contains only nodes with keys less than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- Both the left and right subtrees must also be binary search trees.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
Tools4j-config is fully compatible with JSR-303 and support any combination of constraints on behalf on Bean Validation.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
We will not explore validation possibilities further but please do so on your own. JSR303 provide a great way for reusing constraint-related efforts.
What’s next?
Tools4j-config encourage developers to be very precise about defining configurable parts, strengthening guarantees that applications will operate on correct and meaningful data. Tools4-config also enable productivity, leaving room to focus on domain concerns and not forcing a lot of boiler-plate code around administration, persistence and many other unrelated concerns.
Tools4j-config has a lot more functionality than this post could hold – so this is the end. But the following topics will be explored in the future.
- Persistence and data distribution
How to choose between different ways of storing configuration such as XML files, SQL, NoSQL and more.
- Compile-time checks
How to catch schema faults at compile-time.
- Administration interfaces
How to provisioning configuration using interface such as JAX-WS, JAX-RS, auto-generated GUI, CLI and more.
- Administration users and roles
How to define users and enforce roles for administration.
- Sessions and atomic commits
How to make several changes in the scope of a session and commit changes atomically.
- Schema discovery
How to explore the configuration schema at runtime.
- Change notifications
How applications can subscribe for configuration changes.
- Automatic documentation generation
How to generate operational documentation from code.
- Performance, scalability and consistency
How to support low-latency high-performance centralized management in environments of scale with hundreds of thousands configuration instances.
- Java EE and OSGi integration and portability
How to integrate tools4j-config in Java EE, OSGi, Spring, CDI and other programming models and frameworks.
- Extendability
How to provide tailor-made implementations using the SPI mechanism and verify them against the TCK.
Please visit the website if you think tools4j-config seems interesting.


It looks good! I hope to see more tools4j-config topics soon.
Regards,
Martin
Cool! Thought about doing something simular once or twice myself. Looking forward to more tools4j.
Cheers,
Eltjo.