The PropertiesTree class allows you to use a properties file in an object-oriented way. It is inspired by Groovy/Grail's config objects but doesn't require Groovy.
For instance, this makes it easy to read a set of properties in a loop. Imagine you like configure a number of data sources and your config file contains this:
foo=bar dataSources.database1.url=localhost:3306/main dataSources.database1.username=admin dataSources.database1.password=4711 dataSources.database2.url=remote:3306/secondary dataSources.database2.username=remoteuser dataSources.database2.password=12345 dataSources.database2.settings.autoCommit=false dataSources.database2.settings.poolSize=5You can use your config like this:
PropertiesTree config = ...; for (PropertiesTree dataSourceConfig : dataSources.getChildNodes()) { String url = dataSourceConfig.getLeafValue("url"); ... } PropertiesTree database2settings = config.getNode("dataSources.database2.settings"); System.out.println( database2settings.getLeaves().toString() );
The method getChildNodes() returns a list of child nodes and getLeafValue() or getLeaves() gives you the data of a node.
You can download the class here:
- PropertiesTree.java
- PropertiesTreeTest.java