Wednesday, January 22, 2020

Get property from a properties file updated at runtime

Sometime using java I use a properties file stored on file sistem to manage static data out of my deliverable.
following a way to get the data from the data after an update at runtime.


import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;


public abstract class Props {
 
    private final static Logger logger = Logger.getLogger(Props.class.getCanonicalName());
    private static Properties properties = new Properties();
    private static File configurationFile;
    private static long lastModified;
    
    
    public File getPefPropsCustomDir(){
      return new File("");    
    }
    
    public static Properties getProperties() throws Exception{
        try {
            if (lastModified != configurationFile.lastModified())
                loadDefaultProperties();
  
            return Props.properties;
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Error in getProperties() ", e);
            throw e;
        }
    }

    public static String getProperty(String key) throws Exception {
      if(properties != null){
        try {
            if (lastModified != configurationFile.lastModified())
              loadDefaultProperties();
            
            String returnString = properties.getProperty(key);
            if(returnString != null)
              return returnString.trim();
      
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Error in getProperty() on load " + key, e);
            throw e;
        }
        
      }
      return null;
    }

    private static void loadDefaultProperties() {
        loadProperties(configurationFile);
    }
    
    public static void loadProperties(final File configFile) {
      if(configFile.exists()){
        try {
            logger.config("file updated --> property reload");
            lastModified = configFile.lastModified();
            configurationFile = configFile;
            FileInputStream input = new FileInputStream(configFile);
            propemethodrties = new Properties();
            properties.load(input);
            input.close();
        } catch (Exception e) {
            logger.log(Level.SEVERE, "ERROR PROPERTIES FILE: {" + configFile.getAbsolutePath() + "} ",e);
        }
      } else {
        logger.log(Level.SEVERE, "ERROR PROPERTIES FILE: {" + configFile.getAbsolutePath() + "} NOT EXIST");
      }
  }
    
    
 public static void init(String filePath){
  configurationFile = new File(filePath);
  loadDefaultProperties();
 }
   

   static {
      String path = System.getProperty("property.location");
      init(path);
    }
}

The above class use a static constructor for an init process reading the the file path from a system property. Alternatively you can remove the static costructor an initialize all as you want, for example from a ServletListener that get the value from the web.xml file.

Everytime a property is get out using getProperty method, there is a control on the last modified date of the file checking if is necessary a reload using the loadDefaultPropertys method. The same is checking out te Properties Object.

Finally you can also load a property file using the loadProperties method that is the real initialization methd.

Enjoy!!