-->
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.  [ 5 posts ] 
Author Message
 Post subject: Is cache in ResourceBundleMessageInterpolator thread-safe?
PostPosted: Wed Apr 20, 2011 10:41 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: Thu Apr 28, 2011 5:52 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Thread safety is not an issue here. If a thread does not find anything in the cache it (re-)creates the localized message.


Top
 Profile  
 
 Post subject: Re: Is cache in ResourceBundleMessageInterpolator thread-safe?
PostPosted: Thu Apr 28, 2011 8:50 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hmm, forget about what I just said. We need synchronization - http://lightbody.net/blog/2005/07/hashm ... nfini.html
I created HV-470


Top
 Profile  
 
 Post subject: Re: Is cache in ResourceBundleMessageInterpolator thread-safe?
PostPosted: Sat Apr 30, 2011 1:46 pm 
Newbie

Joined: Wed Apr 20, 2011 8:45 pm
Posts: 6
Glad to be of use. :)

Btw, MessageInterpolator I've mentioned about is ready, well tested and published. For more info: viewtopic.php?f=26&t=1010669


Top
 Profile  
 
 Post subject: Re: Is cache in ResourceBundleMessageInterpolator thread-safe?
PostPosted: Mon May 02, 2011 5:27 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Thanks for sharing :-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.