开发者

JSF2.0 ResourceBundle needs to be reloaded without server restart

开发者 https://www.devze.com 2023-02-20 16:07 出处:网络
We are using JSF2.0 with JDK1.6 and Tomcat6.1 We have a requirement to update the property file values (loaded by JSF resource bundle) without restarting the server so that the live web sessions won\

We are using JSF2.0 with JDK1.6 and Tomcat6.1

We have a requirement to update the property file values (loaded by JSF resource bundle) without restarting the server so that the live web sessions won't be stopped.

Is it possible with JDK1.6 , i tried the below clearCache code but it didn't work.

ResourceBundle bundle = ResourceBundle.getBundle("Label");
String s = bundle.getString("profile.firstName");
out.println("Value before: %"+ s);
ResourceBundle.clearCache(Thread.currentThread().getContextClassLoader());
bundle = ResourceBundle.getBundle("Label");
s = bundle.getString("profile.firstName");
out.println("Value after: {}"+s);

Has anyone tried the same before.

Update

The below doesn't seems to resolve the problem of reloading the resource bundle

ResourceBundle.clearCache(Thread.currentThread().getContextClassLoader());
ApplicationResourceBundle applicationBundle = ApplicationAssociate.getCurrentInstance().getResourceBundles().get("Label");
Field field = applicationBundle.getClass().getDeclaredField("resources");
field.setAccessible(true);
Map<Locale, ResourceBundle> resources = (Map<Locale, ResourceBundle>) field开发者_开发问答.get(applicationBundle);
resources.clear();

Am I missing anything?


This used to work on some JSF implementations/versions. However, on more recent Mojarra versions the caching mechanism got an extra layer in the implementation itself. Assuming that you're indeed using Mojarra, in addition of the line

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

you also need to do this, starting with com.sun.faces.application.ApplicationAssociate

ApplicationResourceBundle applicationBundle = ApplicationAssociate.getCurrentInstance().getResourceBundles().get("Label");
Field field = applicationBundle.getClass().getDeclaredField("resources");
field.setAccessible(true);
Map<Locale, ResourceBundle> resources = (Map<Locale, ResourceBundle>) field.get(applicationBundle);
resources.clear();

Yes, that's a hack, but as far JSF doesn't provide any clean API methods to achieve the same.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号