Dynamic Property Loader using Java Dynamic Proxy pattern

While reading through Stackoverflow, I came up to this interesting question: Java Properties File binding to Java Interface. The idea is simple but quite helpful.

Basically we create an interface like:

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

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

And whenever we want to use constant in our application, we simple use loginConstant.appDescription(). Our application framework creates a dynamic proxy implementation for this interface. All we have to do is just to call the method and we get the value either from property file or default one.

So below is the sample code:

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

Disclaimer: This by no means should be used in Production. The code is just for reference. A lot more optimizations can be done in the below code.

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

So now the magic code. Well nothing is magical here. Just plain Java Dyna proxy using Reflection API

DefaultStringValue.java

    package net.viralpatel.annotations;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface DefaultStringValue {
    	public String value();
    }

Key.java

    package net.viralpatel.annotations;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Key {
    	public String value();
    }

DynamicProperty.java

    package net.viralpatel;
    
    import java.lang.reflect.Proxy;
    
    public class DynamicProperty {
    	
    	public static <T extends Constants> T  create(Class<T> clazz) {
    		
    		T object = (T) Proxy.newProxyInstance(clazz
    				.getClassLoader(), new Class[] { clazz },
    				new Handler(clazz));  
    		
    		return object;
    		
    	}
    }

Handler.java

    package net.viralpatel;
    
    import java.lang.annotation.Annotation;
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    
    import net.viralpatel.annotations.DefaultStringValue;
    import net.viralpatel.annotations.Key;
    
    public class Handler implements InvocationHandler {
    	
    	Object obj;
    
    	public Handler(Object obj) {
    		this.obj = obj;
    	}
    	
    	public Object invoke(Object proxy, Method m, Object[] args)
    			throws Throwable {
    		String key = null;
    		String defaultValue = null;
    		try {
    
    			Annotation[] annotations = m.getAnnotations();
    			for (Annotation annotation : annotations) {
    				if (annotation instanceof Key) {
    					key = ((Key)annotation).value();
    				} else if (annotation instanceof DefaultStringValue) {
    					defaultValue = ((DefaultStringValue)annotation).value();
    				}
    			}
    
    			String ret = PropertyLoader.get(key);
    			return (null == ret) ? defaultValue : ret;
    			
    			// result = m.invoke(obj, args);
    
    		} catch (Exception e) {
    			// We could also put some code here if we want to do
    			// anything special in case an Exception is thrown from
    			// the inside of invoked method.
    			throw e;
    		}
    	}
    }

Constants.java

    package net.viralpatel;
    
    public interface Constants {
    	//marker
    }

PropertyLoader.java

    package net.viralpatel;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class PropertyLoader {
    	private static Properties prop = null;
    
    	public static String get(String key) {
    		if (null == prop) {
    			init();
    		}
    		return (String) prop.get(key);
    	}
    
    	private static void init() {
    		prop = new Properties();
    		InputStream in = Properties.class
    				.getResourceAsStream("/net/viralpatel/config.properties");
    		try {
    			prop.load(in);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
    	}
    }

Download Source Code

Dynamic_Property_Java_Proxy.zip (12 KB)
Update Moved the code to Github for further development: github.com/viralpatel/dynaproperty



5 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]

Current day month ye@r *