Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is not exactly trivial.</p> <p>For one just clearing the <code>ResourceBundle</code> via <code>clearCache()</code> doesn't always yield the desired results. Often you need at least also try to clear using the context class loader:</p> <p><code>ResourceBundle.clearCache(Thread.currentThread().getContextClassLoader());</code></p> <p>This however will still not reload the resource bundle defined in a faces-config.xml file. At least the Mojarra JSF 1.2 implementation privately caches the resource bundle internally. This happens in:</p> <pre><code>FacesContext -&gt; Application -&gt; associate (ApplicationAssociate) -&gt; resourceBundles (Map&lt;String, ApplicationResourceBundle&gt;()) -&gt; resources (Map&lt;Locale, ResourceBundle&gt;) </code></pre> <p>It's possible to clear this cache via reflection (at the end of the day, it's just an entry in a Map), or you might wanna replace the Application. Both are not things you normally do lightheartedly.</p> <p>Purely for development you could use JRebel, which probably already has knowledge of Mojarra and most likely does the reflection trick mentioned above.</p> <p>After some experimenting, I came to the following code which does the trick on JBoss AS 5/JSF 1.2. It does tie your code to Mojarra (imports sun packages) and can break with any upgrade because of reflective tricks being used. But anyway, this is the code:</p> <pre><code>public static void reloadBundle() { ResourceBundle.clearCache(Thread.currentThread().getContextClassLoader()); ApplicationResourceBundle appBundle = ApplicationAssociate.getCurrentInstance().getResourceBundles().get("your_bundle_name"); Map&lt;Locale, ResourceBundle&gt; resources = getFieldValue(appBundle, "resources"); resources.clear(); } @SuppressWarnings("unchecked") private static &lt;T&gt; T getFieldValue(Object object, String fieldName) { try { Field field = object.getClass().getDeclaredField(fieldName); field.setAccessible(true); return (T) field.get(object); } catch (Exception e) { return null; } } </code></pre> <p><em>(replace the getFieldValue helper method with your own favorite reflective util if necessary and sprinkle with exception and null handlers where appropriate)</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload