Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Properties File binding to Java Interface

With GWT you have stuff like this:

public interface LoginConstants extends Constants {
   @DefaultStringValue("Wellcome to my super app")
   @Key("appDescription")
   String appDescription();

   @DefaultStringValue("Ok")
   @Key("okButtonLabel")
   String okButtonLabel();
}

Then you can use from your classes doing GWT.create(LoginConstant.class), in this way the interface is backed by dynamic implementation that, when I call loginConstants.appDescription() returns the value contained from a property file using the @Key annotation to reference the key in the property file. If the property file misses the property, then de @DefaultStringValue is returned. This is used for internationalization, but can possibly work also for configuration. But with GWT, this is meant to be used on the client side (ie. translated to JavaScript), and for i18n, not for configuration.

But, I find this idea very convenient also for configuration handling.

I wonder if somebody knows a framework to do a similar thing on the server side, without necessarily bind your code to GWT. ie. if there is any library that implements this kind of logic specifically designed for the configuration handling. I am not aware of anything like this.

Reference to the feature in GWT: https://developers.google.com/web-toolkit/doc/latest/DevGuideI18nConstants

like image 926
Luigi R. Viggiano Avatar asked Sep 11 '25 10:09

Luigi R. Viggiano


2 Answers

I implemented my own solution to the question:

BASIC USAGE

The approach used by OWNER APIs, is to define a Java interface associated to a properties file.

Suppose your properties file is defined as ServerConfig.properties:

port=80
hostname=foobar.com
maxThreads=100

To access this property you need to define a convenient Java interface in ServerConfig.java:

public interface ServerConfig extends Config {
    int port();
    String hostname();
    int maxThreads();
}

We'll call this interface the Properties Mapping Interface or just Mapping Interface since its goal is to map Properties into an easy to use a piece of code.

Then, you can use it from inside your code:

public class MyApp {
    public static void main(String[] args) {
        ServerConfig cfg = ConfigFactory.create(ServerConfig.class);
        System.out.println("Server " + cfg.hostname() + ":" + cfg.port() +
                           " will run " + cfg.maxThreads());
    }
}

But this is just the tip of the iceberg.

Continue reading here: Basic usage || Website || Github

I still have a couple of features in mind, but the current implementation goes a little forward than the basic functionalities described in the questions.

I need to add samples and documentation.

like image 55
Luigi R. Viggiano Avatar answered Sep 12 '25 23:09

Luigi R. Viggiano


I loved the idea so much that I quickly assembled some code using Java Dynamic proxies.

So basically you create an interface with relevant methods and annotate them with @Key, @DefaultStringValue annotations.

Below is the sample Java code:

Main.java

package net.viralpatel;

import net.viralpatel.annotations.DefaultStringValue;
import net.viralpatel.annotations.Key;

interface LoginConstants extends Constants {
       @DefaultStringValue("Wellcome to my super app")
       @Key("appDescription")
       String appDescription();

       @DefaultStringValue("Ok")
       @Key("okButtonLabel")
       String okButtonLabel();
}

public class Main {
    public static void main(String[] args) {
        LoginConstants constants = DynamicProperty.create(LoginConstants.class);
        System.out.println(constants.appDescription());
        System.out.println(constants.okButtonLabel());
    }
}

Also the property file in background that we load is

config.property

okButtonLabel=This is OK

Just execute the Main java class, following output will be displayed:

Output:

Wellcome to my super app 
This is OK

Here is the rest of code: http://viralpatel.net/blogs/dynamic-property-loader-using-java-dynamic-proxy-pattern/

like image 22
Viral Patel Avatar answered Sep 12 '25 23:09

Viral Patel