Insights into Java Server Faces

2010/01/04

Combine multiple properties files into one ResourceBundle

Filed under: Java, JSF — Hanspeter @ 20:54

Sometimes it would be nice to combine multiple properties files into one single ResourceBundle. For example, a part of the resources is provided by some library and anothe part of the resources should be provided by the application using that library.

Normally the only way to do that is to copy and overwrite the properties files coming with the library. With my MultiplePropertiesResourceBundle you don’t have to copy the original files, just provide additional properties files sticking to some naming convention and you can combine mutliple properties files and even overrule keys of other properties files of the set.

MultiPropertiesResourceBundle searches for multiple, similar named resources (the .properties files) on the class path and finds them in jars and classes folders. Let’s assume we want a MultiplePropertiesResourceBundle for “example.properties” file. For that we simply create an ExampleResourceBundle as follows:


 public class ExampleResourceBundle extends MultiplePropertiesResourceBundle {
        public ExampleResourceBundle() {
                super("example");
        }
 }

Now MultiplePropertiesResourceBundle is looking for all resource with name “example.properties” – therefore you must include a file with that exact name on every location where you want to place additional files named like “*example.properties”. For all these files, the resource names (like “example”, “another-example” etc. ) are collected and used to load them using PropertyResourceBundle with the asked Locale. All the key-value pairs are copied to MultiplePropertiesResourceBundle.

Support for different Locales is done by simply subclassing ExampleResourceBundle with all the Locale variants that are needed (like public class ExampleResourceBundle_en extends ExampleResourceBundle {}) and of course the Locale variants of the .properties files that contribute to MultiplePropertiesResourceBundle.

I developed this class, because we needed a way to build a single ResourceBundle in our JSF based application and allow different providers to provide entries for that single ResourceBundle. A ResourceBundle like the ExampleResourceBundle can easily be configured in faces-config.xml’s section as follows:


 <resource-bundle>
   <base-name>ch.dueni.util.ExampleResourceBundle</base-name>
   <var>example</var>
 </resource-bundle>

But it’s usage is not limited to JSF applications – since it uses/extends standard ResourceBundle handling it works in all Java applications.

Please also have a look at the Javadoc of MultiplePropertiesResourceBundle.

Hope you like it.

Advertisement

2 Comments »

  1. Hi,

    Can you please explain how we can configure our custom resource bundle(which is a java class – ExampleResourceBundle) in faces-config.xml?

    How can we access the properties that has been loaded in resource bundle (ExampleResourceBundle) in JSF xhtml files?

    Thanks in advance.

    Comment by Raj — 2011/10/18 @ 09:44

    • Hi, there used to be an example how this looks in faces-config.xml – however, there was some formatting issues. Fixed that – so now you see the faces-config.xml example again.

      And of course, once resource-bundle is configured in faces-config.xml it can be accessed in all pages with EL expressions like ${example.property_key}.

      Cheers
      Hanspeter

      Comment by Hanspeter — 2011/10/18 @ 12:27


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.