Luigi is a passionate software developer working in Java since 2000. In 2001 he co-founded the Java User Group Torino, the first local JUG in Italy. Quickly after that, many other JUGs have born in Italy following JUG Torino's example. Since some years now, he is working as free lance software consultant between Italy, Germany, UK and Switzerland, on several industries. His personal interests include software, science fiction books and movies, traveling, photography and good food. Luigi is a DZone MVB and is not an employee of DZone and has posted 19 posts at DZone. You can read more from them at their website. View Full User Profile

Owner 1.0.3 What's New? Part 1: Variable Expansion

02.14.2013
| 2338 views |
  • submit to reddit

In the last blog post I introduced what is the OWNER API and how to use it.
Basically it is tiny library that helps reducing the Java code to read the configuration for your application from Java Properties files. You can find full documentation here. Version 1.0.0 was having the very basic features, and now I’m adding new things that may return useful without compromising its simplicity and compactness.

Yesterday I have released version 1.0.3 which includes some new features that I want to introduce in some short blog posts.
On this weekend I set up the continuous integration on CloudBees and I noticed that compiling with JDK 6 there was a compilation error, since I was using JDK 7, so I decided that the fix was worthing another release, so that if somebody is using JDK 6 won’t have any problem.

So, what’s new then?

First of all, the OWNER library is now available on Maven Central Repository, and this will ease the job of people willing to try it, since now it only requires few lines to configure your project to use it, and I tried to improve the documentation. Unfortunately, publishing the library on Maven Central Repository required me to buy an internet domain name and use it for the groupId of the library. This may have broken the backward compatibility if someone started to use this library. I apologize for that.

The first thing I want to point out, is something I always missed in Java Properties files. A “variable expansion” mechanism.
For example, imagine your application having a configuration file like this:

server.host=myhost.com
service.url=http://${server.host}/some/endpoint
application.path=${HOME}/myapp
application.data=${application.path}/data
application.logs=${application.path}/logs

With plain Java properties file, the job to replace the variables is left to you. With Owner API, you can create a Java interface to map the above configuration and have all the variables resolved:

public interface MyAppConfig extends Config {
    @Key("server.host")
    String host();

    @Key("service.url")
    URL serviceURL();

    @Key("application.data")
    File appData();

    @Key("application.logs")
    File appLogs();
}

// then use it with:
MyAppConfig cfg = ConfigFactory.create(MyAppConfig.class, System.getenv());

The System.getenv() is used to resolve the ${HOME} system environment variable. In the same way you can pass other properties files too, like System properties or any other class implementing java.util.Map, see ConfigFactory.create(). I called this technique “importing properties”, since you specify some Properties or Maps during the creation of the Config object.

More details can be found on the README file on github: variables expansion and importing properties.

I’ll write more about what’s new in the next blog post.



 

Published at DZone with permission of Luigi Viggiano, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)