Friday, 20 May 2016

Owner API: Intelligent way of dealing Java properties file

While working in real world applications, you may observed that, lot of configurations are maintained by many files, there is much boiler plate code to read these properties, type cast these properties from String to some other data type like int, float, date etc., Owner api is provides a way to minimize the code required to handle java properties (configuration) files. Let me try to explain this by using simple example.

Setting up
I am going to use following maven dependency for owner API.

<dependency>
         <groupId>org.aeonbits.owner</groupId>
         <artifactId>owner</artifactId>
         <version>1.0.9</version>
</dependency>

If you want to use the features of Java 8, add following artifact.

<artifactId>owner-java8</artifactId>

<dependency>
         <groupId>org.aeonbits.owner</groupId>
         <artifactId>owner-java8</artifactId>
         <version>1.0.9</version>
</dependency>

First let me explain you the fundamental way to read properties file using Java.

How to read from properties file
Step 1: Instantiate Properties class.
Ex: Properties properties = new Properties();

Step 2: Create FileInputStream instance to read from properties file
Ex: URL resource = PropertyUtil.class.getClassLoader().getResource("ProjectConfig.properties");

File file = new File(resource.toURI());
FileInputStream fin = new FileInputStream(file);

I am assuming that my property file is in classpath. If you don’t know, how to add a property file to class path in Eclipse, go through following link.


Step 3: Load properties file
Ex: properties.load(fin);

Step 4: Read the properties.
Ex: properties.getProperty("imagesDirectory")

ProjectConfig.properties

#Project configurations
imagesDirectory = /Users/harikrishna_gurram/images
videosDirectory = /Users/harikrishna_gurram/videos
maxIcons = 9
adminMail = admin@admin.com


Following is the complete working application.

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.Properties;

public class PropertyUtil {
 public static void main(String args[]) throws Exception {
  Properties properties = new Properties();
  URL resource = PropertyUtil.class.getClassLoader().getResource(
    "ProjectConfig.properties");

  File file = new File(resource.toURI());

  try (FileInputStream fin = new FileInputStream(file);) {
   properties.load(fin);

   System.out.println(properties.getProperty("imagesDirectory"));
   System.out.println(properties.getProperty("videosDirectory"));
   System.out.println(properties.getProperty("maxIcons"));
   System.out.println(properties.getProperty("adminMail"));
  }

 }
}


Output
/Users/harikrishna_gurram/images
/Users/harikrishna_gurram/videos
9
admin@admin.com


Let me rewrite above application using Owner API. In Owner API we need to create an interface for every property file. Suppose my property file name is ProjectConfig.properties, I should create an interface ProjectConfig.java.

ProjectConfig.properties

#Project configurations
imagesDirectory = /Users/harikrishna_gurram/images
videosDirectory = /Users/harikrishna_gurram/videos
maxIcons = 9
adminMail = admin@admin.com


ProjectConfig.java file correspondent to ProjectConfig.properties file looks like below.

Make sure your interface extends Config interface.

package owner_api_tutorial;

import org.aeonbits.owner.Config;

public interface ProjectConfig extends Config {
 String imagesDirectory();
 String videosDirectory();
 int maxIcons();
 String adminMail();
}


package owner_api_tutorial;

import org.aeonbits.owner.ConfigFactory;

public class PropertyUtil {
 public static void main(String args[]) throws Exception {
  ProjectConfig cfg = ConfigFactory.create(ProjectConfig.class);

  System.out.println(cfg.adminMail());
  System.out.println(cfg.imagesDirectory());
  System.out.println(cfg.maxIcons());
  System.out.println(cfg.videosDirectory());

 }
}


Output

admin@admin.com
/Users/harikrishna_gurram/images
9
/Users/harikrishna_gurram/videos


Note:
To run this application successfully, keep the properties file ProjectConfig.properties in the same package where the interface ProjectConfig exists. In my case I keep ProjectConfig.properties file in the package owner_api_tutorial.

In this tutorial series you are going to learn.



Previous                                                 Next                                                 Home

No comments:

Post a Comment