Over a million developers have joined DZone.

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

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

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.



 

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

Published at DZone with permission of Luigi Viggiano, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}