0 Replies Latest reply on Aug 27, 2009 8:43 AM by Dieter Rehbein

    clear resource bundle cache, a solution (requires Java 6)

    Dieter Rehbein Newbie

      Hi all,


      during development I don't want to restart the applicaction server just because a message has been added or modified. Since the resource bundles are cached, a method to clear that cache is required.


      I've implemented a simple seam component, which can be used to clear the cache. Maybe this helps some other people using SEAM:



      public class ClearResourcesAction extends SkilineAction
      {
         private static final long serialVersionUID = 6353316361885036069L;
      
         public static final String COMPONENT_NAME = "clearResourcesAction";
         
         @In(SeamComponents.RESOURCE_BUNDLE)
         private ResourceBundle resourceBundle;
         
         @SuppressWarnings("unchecked")
         public void clearResources()
         {
            try
            {
               SeamResourceBundle seamResourceBundle = (SeamResourceBundle)resourceBundle;
               Field field = seamResourceBundle.getClass().getDeclaredField("bundleCache");
               field.setAccessible(true);
               Map bundleCache = (Map)field.get(resourceBundle);
               bundleCache.clear();
               SeamResourceBundle.clearCache(Thread.currentThread().getContextClassLoader());
               statusMessages.add("Resourcebundle cache cleared");
            }
            catch(Exception e)
            {
               statusMessages.add(Severity.ERROR, "Could not clear resource bundle cache: {0}", e);
            }
         }
      }
      



      The class SeamComponents defines constants for some built in seam components. SeamComponents.RESOURCE_BUNDLE is defined as org.jboss.seam.core.resourceBundle.


      The static method java.util.ResourceBundle.clearCache was introduced in Java 6, so this solution doesn't work with Java 5!


      regards
      Dieter