-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Is cache in ResourceBundleMessageInterpolator thread-safe?
PostPosted: Wed Apr 20, 2011 9:00 pm 
Newbie

Joined: Wed Apr 20, 2011 8:45 pm
Posts: 6
Hi!
While trying to implement my own mod of MessageInterpolator, stumbled on ResourceBundleMessageInterpolator cache. As is written in Java6SE API, WeakHashMap, which is used for cache, is not thread-safe. And I haven't seen any synchronization routines in the code. Version 4.1.0.Final:
Code:
/**
* Resource bundle backed message interpolator.
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
* @author Gunnar Morling
*/
public class ResourceBundleMessageInterpolator implements MessageInterpolator {
   // ...

   /**
    * Step 1-3 of message interpolation can be cached. We do this in this map.
    */
   private final Map<LocalisedMessage, String> resolvedMessages = new WeakHashMap<LocalisedMessage, String>();

   /**
    * Flag indicating whether this interpolator should chance some of the interpolation steps.
    */
   private final boolean cacheMessages;

   public ResourceBundleMessageInterpolator() {
      this( null );
   }

   public ResourceBundleMessageInterpolator(ResourceBundleLocator userResourceBundleLocator) {
      this( userResourceBundleLocator, true );
   }

   public ResourceBundleMessageInterpolator(ResourceBundleLocator userResourceBundleLocator, boolean cacheMessages) {

      defaultLocale = Locale.getDefault();

      if ( userResourceBundleLocator == null ) {
         this.userResourceBundleLocator = new PlatformResourceBundleLocator( USER_VALIDATION_MESSAGES );
      }
      else {
         this.userResourceBundleLocator = userResourceBundleLocator;
      }

      this.defaultResourceBundleLocator = new PlatformResourceBundleLocator( DEFAULT_VALIDATION_MESSAGES );
      this.cacheMessages = cacheMessages;
   }

   // ...

   private String interpolateMessage(String message, Map<String, Object> annotationParameters, Locale locale) {
      LocalisedMessage localisedMessage = new LocalisedMessage( message, locale );
      String resolvedMessage = null;

      if ( cacheMessages ) {
         resolvedMessage = resolvedMessages.get( localisedMessage );
      }

                  // ...

      if ( cacheMessages ) {
         resolvedMessages.put( localisedMessage, resolvedMessage );
      }

                  // ...
   }

   // ...
}

So I'm guessing:
Is there some thread-safety magic in WeakHashMap (sorry, I'm a newbie in Java)?
Or is it (cache) is a short living one (since it's not static), and each MessageInterpolator is for single-threaded use only?
Or is it MessageInterpolator synchronized somewhere else (which would be kinda inefficient)?
Or is this cache never used?


Top
 Profile  
 
 Post subject: Re: Is cache in ResourceBundleMessageInterpolator thread-safe?
PostPosted: Wed Apr 20, 2011 10:43 pm 
Newbie

Joined: Wed Apr 20, 2011 8:45 pm
Posts: 6
Oh, sorry, posted in the wrong area.
I reposted this in appropriate forum branch, but now can't find way to delete this topic from here...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.