Google

Nov 30, 2011

Java Interview Questions and Answers: working with proprties files

Managing properties for an enterprise application can be a bit trickier than one might expect.


Q: How would you go about managing properties files in your application?
A: One great advantage of property files is that they let you change your application's configuration without recompilation. However, you most likely need to restart your application for the new configuration to take effect. However, you most likely need to restart your application for the new configuration to take effect. There are different types of properies files like

1. Environment independent files archived within a war or ear. These can be loaded via the classpath.

classpath:test/myapp.properties

2. Environment independent files stored outside a war or ear. These need to be loaded via a file loader. Define a JVM argument or system property for the path of the file.
file:${PROPERTIES_HOME}/test/myapp.properties

3. Environment specific files stored outside a war or ear. These need to be loaded via a file loader. Define a JVM argument or system property for the path of the file.

file:${PROPERTIES_HOME}/test/myapp_${my_env}.properties

It is a best practice to store environment specific (e.g. test, dev, uat, staging, prod, etc)  files outside the war or ear archives as the same archive can be deployed to any environment without having to package environment specific archives.

4. Properties file with sensitive information. Store the property values encrypted (Triple DES) or encoded (base64) with a salt. A salt is a secret that can be stored in a database or a separate file with proper access control.

5. Dynamic information in properties file or loading a property file at runtime. For example, in your .properties file you can use
#messages
greeting=Welcome Mr. {0} {1} !!!

Then use the MessageFormat class in Java

MessageFormat.format((String) props.get("greeting"), firstName, lastName);

You can also dynamically load .properties file with the load and store methods in java.util.Properties. It is recommended to use an existing configuration library like Apache Commons Configuration to load properties file at runtime without having to bring the server or application down. The commons config does support variable interpolation.

application.title = ${application.name} ${application.version}

Finally you can use JMX (Java Management eXtension) to write your own managed bean to change proprty values at runtime. A typical real life example would be to change log4j.xml debug levels. Appropriate log levels are very handy in diagnosing problems in production, but log levels like trace or debug can adversely impact performance. Hence, it is very useful to be able to set log levels at runtime.


Here is a sample .properties file that can be used with Apache's commons configuration.

#database - encrypted
db.password=654fdgtr45#1232

#database - clear
db.host=localhost
db.user=john

#messages
greeting=Welcome Mr. {0} {1} !!!

#statuses
order.status.rejected=REJECTED
order.status.filled=FILLED
order.status.shipped=SHIPPED


# lists and arrays
colors.pie = #FF0000, #00FF00, #0000FF

#File includes -- include other .properties files
include = email.properties


Q. How would you load a properties file from a classpath or a file system?
A. Here is a very basic code snippet.


package test;

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

public class PropertiesUtil {

    public static Properties loadFromClassPath(String fileName) throws Exception {
        Properties props = new Properties();
        URL url = ClassLoader.getSystemResource(fileName);
        props.load(url.openStream());
        System.out.println(props);
        return props;
    }
    
    public static Properties loadFromFile(File propsFile) throws IOException {
        Properties props = new Properties();
        FileInputStream fis = new FileInputStream(propsFile);
        props.load(fis);    
        fis.close();
        System.out.println(props);
        return props;
    }
    
    public static void main(String[] args)  throws Exception {
        loadFromClassPath("test/myapp.properties");
        loadFromFile(new File("C:\\opt2\\myapp\\test\\myapp.properties"));
    }
}


Spring has the ability to load properties files as part of the application configuration, for use internally. The properties can be referenced within spring configuration files using ant-style placeholders, e.g. ${app.var}. Here is the link to step by step tutorial.

Spring Interview Questions and Answers: read from properties file

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home