Friday, 20 May 2016

Java: Owner: property file loading strategies

In preceding examples, we are keeping the property file "ProjectConfig.properties", in the same package, where the interface ProjectConfig.java exists. Don't you see this as a restriction? Why should I keep my property file in my source packages? I want to keep my property files in some location. By using Sources annotation, you can tell to Owner API, where to search for property files.


ProjectConfig.properties
#Project configurations
images.directory = /Users/harikrishna_gurram/images
images.backup.directory = /Users/harikrishna_gurram/backup/images
videos.directory = /Users/harikrishna_gurram/videos
maxIcons = 9


ProjectConfig.java
package owner_api_tutorial;

import org.aeonbits.owner.Config.Sources;
import org.aeonbits.owner.Config;


@Sources({ "file:~/.myapp.config", 
    "file:/etc/myapp.config", 
    "classpath:ProjectConfig.properties" })
public interface ProjectConfig extends Config {

  @Key("images.directory")
  String imagesDirectory();

  @Key("images.backup.directory")
  String imagesBackupDirectory();

  @Key("videos.directory")
  String videosDirectory();

  @DefaultValue("16")
  int maxIcons();

  @DefaultValue("admin@admin.com")
  String adminMail();
}


@Sources({ "file:~/.myapp.config",
    "file:/etc/myapp.config",
    "classpath:ProjectConfig.properties" })

As you see, I passed different paths to property files like "file:~/.myapp.config", "file:/etc/myapp.config", "classpath:ProjectConfig.properties". By default Owner API follows first load policy (@LoadPolicy(LoadType.FIRST)). First it tries to load properties from the file file:~/.myapp.config, if this is found, this file alone will be used. If the file not found, it tries to load the second one "file:/etc/myapp.config", and if this is found, this one will be used. If this too not found, finally it search for the file "ProjectConfig.properties" in project class path ad load it. If the last attempt also failed to load the property file, then the Java interface will not be associated to any file, and only @DefaultValue will be used where specified.


PropertyUtil.java
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.imagesBackupDirectory());
    System.out.println(cfg.maxIcons());
    System.out.println(cfg.videosDirectory());

  }
}


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


To run this example, I placed "ProjectConfig.properties" file in classpath. It is not required to follow same naming convention to property file and interface. For example, I renamed ProjectConfig.properties file to Con.properties.

Con.properties

#Project configurations
images.directory = /Users/harikrishna_gurram/images
images.backup.directory = /Users/harikrishna_gurram/backup/images
videos.directory = /Users/harikrishna_gurram/videos
maxIcons = 9

ProjectConfig.java    
package owner_api_tutorial;

import org.aeonbits.owner.Config;
import org.aeonbits.owner.Config.Sources;

@Sources({ "file:~/.myapp.config", 
    "file:/etc/myapp.config", 
    "classpath:Con.properties" })
public interface ProjectConfig extends Config {

  @Key("images.directory")
  String imagesDirectory();

  @Key("images.backup.directory")
  String imagesBackupDirectory();

  @Key("videos.directory")
  String videosDirectory();

  @DefaultValue("16")
  int maxIcons();

  @DefaultValue("admin@admin.com")
  String adminMail();
}


Re run PropertyUtil.java class, you will get the same output.

Note
~ points to the Home directory in your machine.

$ echo ~
/Users/harikrishna_gurram




Previous                                                 Next                                                 Home

No comments:

Post a Comment