Tuesday, July 14, 2015

Externalized Resource Bundle for ADF Application

Often we have to use resource bundle for our project which is a part of the project war.
Issue with this approach is when the war is specific to environment, for eg, if you need to provide different url for dev,prod,st and qa in your application, you need to create environment specific artifact and the same artifact can not be implemented across all the environment.

Solution to this approach is to externalize the resource bundle. The resource bundle would be the part of environment and the application would point to the location of the resource bundle instead of resource bundle being a part of the Application.

steps to implement the same:

1. create the property file with content(content is specific to this demo):

button_value=this text comes from property file
butoon_message=this message will be displayed on button click which comes from property file

and save it as TestMessage.properties in the local folder for eg:

C:\test\TestMessage.properties

while deploying the application in weblogic the path can be:
/web/TestMessage.properties

2. Mention the path's name in weblogic's start parameter.
I would define a system variable as TESTPROP_PATH and assign it the location as C:\test\TestMessage.properties for the example

To achieve this i have made an entry in
setDomainEnv file present in <UserFolder>\AppData\Roaming\JDeveloper\system11.1.1.7.40.64.93\DefaultDomain\bin

as:
set EXTRA_JAVA_PROPERTIES=-Djps.app.credential.overwrite.allowed=true -DTESTPROP_PATH=/test/TestMessage.properties %EXTRA_JAVA_PROPERTIES%

once set, this variable can be accessed from application as:

System.getProperty("AIMSPROP_PATH")

***please check with Environment team how to perform the step 2 for the environment


3. Now in your application you need to create a java class which would extend ListResourceBundle and fetch all the entries from the file as:

4. Make an entry for this class as <resource-bundle> in faces-config.xml

5. To access the resource bundle in jspx or jsff and entry has to be made in for f:loadBundle and can be used as:
6. Similarly, to use the resource bundle in java class can be used as:

the link to the demo application can be found here

steps 1 & 2 has to be performed individually to run the downloaded application


Please put in your comments in case of any doubts or suggestion to improvise this entry

2 comments:

  1. How about bundles used by EOs and VOs?

    ReplyDelete
  2. you can use the bundle in Model Project as well. You need to redo the step 3 for model project as well.

    Suppose you need to use a DB sequence in your project which is specific for dev and prod env. in that case all you need to do is make an entry for the sequence name in property file as
    sequnece_name=your_sequence name

    use this in EntityImpl as

    @Override
    protected void initDefaults() {
    ResourceBundle rs = new TestResBundle(); //to get the bundle class
    String sequnece_name = rs.getString("sequnece_name"); //to fetch the specific entry

    super.initDefaults();
    SequenceImpl seq =
    new SequenceImpl(sequnece_name, getDBTransaction());
    populateAttributeAsChanged(ID, seq.getSequenceNumber());
    }

    however optimization is always an option for the example mentioned :)

    ReplyDelete