Friday 24 January 2014

How to load external resource bundle using java in ADF


Resource bundles are used to achieve Internalization in the Web based applications. They contain Key / Value pairs. Key identifies local-specific object in resource bundle.

Generally, when we create a “ViewController” project in ADF, It creates a default property file inside the project of “Resource Bundle Type” as Properties Bundle.

When an application is deployed, location of resource bundle will be inside EAR file as a result any update made in the component label requires application re-deployment. The issue can be resolved easily just placing resource bundle property file outside the EAR.

Below example will explain the scenario and configurations required:

Configure ListResourceBundle in project:

Goto-> project properties -> Resource Bundle
 
 

Register resource bundle in your application

Open faces-config.xml file and define resource bundle.

<resource-bundle>
      <base-name>view.MyResourceBundle</base-name>
      <var>viewcontrollerBundle</var>
    </resource-bundle>

Code snippet of java Class:

package view;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.util.Enumeration;
import java.util.ListResourceBundle;
import java.util.Properties;


public class MyResourceBundle extends ListResourceBundle {

    static final Object[][] contents = readPropertyFile();

    public Object[][] getContents() {
        return contents;
    }

    public static Object[][] readPropertyFile() {
        Object[][] data = null;
        try {
//Property file path in system
                       String nodeProFile =
                "C:\\Users\\Gopal\\Desktop\\resource bundle\\MyResourceBundle.properties";
            System.out.println("nodeProFile : " + nodeProFile);
            Properties prop = new Properties();
            //load a properties file
            prop.load(new FileInputStream(nodeProFile));
            Enumeration propKeys = prop.keys();
            System.out.println("Property size : " + prop.size());
            data = new Object[prop.size()][2];
            int i = 0;
            while (propKeys.hasMoreElements()) {
                String key = (String)propKeys.nextElement();
                String value = prop.getProperty(key);
                System.out.println("Key  " + key + " -> : " + value);
                data[i][0] = key;
                data[i][1] = value;
                i++;
            }
        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return data;
    }
}

*Comments and feedback are most welcomed, Thanks.