tags:

views:

301

answers:

1

I want to reload the ResourceBundles used by JSF programmatically on a button click. The ResourceBundles in the classpath (i.e. WEB-INF/classes) are modified by an external application and the modification event is known to me.

+2  A: 

Try

ResourceBundle.clearCache(Thread.currentThread().getContextClassLoader());

(you can use the same method without arguments)

From the javadoc

Resource bundle instances created by the getBundle factory methods are cached by default, and the factory methods return the same resource bundle instance multiple times if it has been cached. getBundle clients may clear the cache, manage the lifetime of cached resource bundle instances using time-to-live values, or specify not to cache resource bundle instances. Refer to the descriptions of the getBundle factory method, clearCache, ResourceBundle.Control.getTimeToLive, and ResourceBundle.Control.needsReload for details.

Bozho
The solution is good, but the classloader not really. I would rather use the context classloader from the thread. You're now dependent on the classloader which actually loaded the bean/action class in question, which may not per se be the same classloader which loaded the resourcebundles.
BalusC
would that be FacesContext.getClass().getClassLoader() ?
Bozho
`Thread.currentThread().getContextClassLoader()`
BalusC
Sorry, I forgot to say that I am running on Java 1.4 and the clearCache() methods have been added since Java 1.6. In this case, what to do?Anyway, thanks for your response.
raatprohory
I don't have the 1.4 classes, but you can try using reflection to clear the internal cache of the ResourceBundle class.
Bozho