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 another 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 as you may see in the JUnit tests in the linked small project.

Please also have a look at the javadoc of MultiplePropertiesResourceBundle.java – you find the sources as a small maven project on https://github.com/dueni/faces-ext/tree/master/resourcebundle. Hope you like it.

By dueni.

Advertisements

8 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

  2. Estimados. tengo un proyecto que manjeo con capas, Model y viewcontroller. En el model creo todoas las clases y cree mi archivo .properties con algunos mensajes. y en la capa viewcontroller los quiero llamar y nose como configurar he probado todo y no agarra el valor y cuando pongo el archivo propeties en la capa de view controller ahi se agarra el valor pero como las capas los manejo en distintos proyectos con la misma Solucion, la forma de llmar como seria hay q importar algo. ppor favor si alguien sabe podria ayudarme

    Comment by Gianpiere — 2012/11/15 @ 16:56

    • Sorry, I don’t understand spanish – I think it’s spanish what you wrote …

      Comment by Hanspeter — 2012/11/16 @ 08:23

  3. Hi,

    I recently found your code. Helped me a lot. I’ve added support for jboss vfs, maybe you want to add this to your code? Leave me a note by mail, if you are interested.

    Comment by Ingolf Kamen — 2014/04/09 @ 10:15

    • Hello Ingolf.
      Sure, would be nice to support jboss vfs as well. Will send you an email.

      Comment by Hanspeter — 2014/04/09 @ 10:52

  4. Hi, i have a jar with messagesCore.properties and a web client with same properties messagesCore.properties, this last file only have keys to everride.
    Problem is cl.getResources(resourceName) find 2 properties (jar and web client) but addMatchingNameOnce only add once.
    the in the line for (String bundleName : bundleNames) .. how could combine if ther for contains only one element?

    Regards

    Comment by rolo — 2014/06/20 @ 14:01

    • Hi.
      Might be a little late, but never the less.
      The solution described in this post works with unique file names in the different locations (jar, web-client) based on a matching part of the files. If you have a messageCore.properties files in jar and web-client you should rename e.g. the file in web-client to messageCore-web.properties. using messageCore as the base-name.
      Regards

      Comment by Hanspeter — 2014/10/03 @ 22:19


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 )

Google+ photo

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

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: