org.springframework.context.support
Class ResourceBundleMessageSource

java.lang.Object
  extended byorg.springframework.context.support.AbstractMessageSource
      extended byorg.springframework.context.support.ResourceBundleMessageSource
All Implemented Interfaces:
HierarchicalMessageSource, MessageSource

public class ResourceBundleMessageSource
extends AbstractMessageSource

MessageSource that accesses the ResourceBundles with the specified basenames. This class relies on the underlying JDK's java.util.ResourceBundle implementation.

Unfortunately, java.util.ResourceBundle caches loaded bundles indefinitely. Reloading a bundle during VM execution is not possible by any means. As this MessageSource relies on ResourceBundle, it faces the same limitation. Consider ReloadableResourceBundleMessageSource for an alternative.

Author:
Rod Johnson, Juergen Hoeller
See Also:
setBasenames(java.lang.String[]), ReloadableResourceBundleMessageSource, ResourceBundle

Field Summary
 
Fields inherited from class org.springframework.context.support.AbstractMessageSource
logger
 
Constructor Summary
ResourceBundleMessageSource()
           
 
Method Summary
protected  MessageFormat getMessageFormat(ResourceBundle bundle, String code, Locale locale)
          Return a MessageFormat for the given bundle and code, fetching already generated MessageFormats from the cache.
protected  ResourceBundle getResourceBundle(String basename, Locale locale)
          Return a ResourceBundle for the given basename and code, fetching already generated MessageFormats from the cache.
protected  MessageFormat resolveCode(String code, Locale locale)
          Subclasses must implement this method to resolve a message.
protected  String resolveCodeWithoutArguments(String code, Locale locale)
          Subclasses can override this method to resolve a message without arguments in an optimized fashion, i.e. to resolve a message without involving a MessageFormat.
 void setBasename(String basename)
          Set a single basename, following ResourceBundle conventions: It is a fully-qualified classname.
 void setBasenames(String[] basenames)
          Set an array of basenames, each following ResourceBundle conventions.
 void setClassLoader(ClassLoader classLoader)
          Set the ClassLoader to load resource bundles with, or null for using the thread context class loader on actual access (applying to the thread that does the "getMessage" call).
 String toString()
          Show the configuration of this MessageSource.
 
Methods inherited from class org.springframework.context.support.AbstractMessageSource
createMessageFormat, getMessage, getMessage, getMessage, getMessageInternal, getParentMessageSource, resolveArguments, setParentMessageSource, setUseCodeAsDefaultMessage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ResourceBundleMessageSource

public ResourceBundleMessageSource()
Method Detail

setBasename

public void setBasename(String basename)
Set a single basename, following ResourceBundle conventions: It is a fully-qualified classname. If it doesn't contain a package qualifier (such as org.mypackage), it will be resolved from the classpath root.

Messages will normally be held in the /lib or /classes directory of a WAR. They can also be held in Jars on the class path. For example, a Jar in an application's manifest classpath could contain messages for the application.

Parameters:
basename - the single basename
See Also:
setBasenames(java.lang.String[]), ResourceBundle

setBasenames

public void setBasenames(String[] basenames)
Set an array of basenames, each following ResourceBundle conventions. The associated resource bundles will be checked sequentially when resolving a message code.

Note that message definitions in a previous resource bundle will override ones in a later bundle, due to the sequential lookup.

Parameters:
basenames - an array of basenames
See Also:
setBasename(java.lang.String), ResourceBundle

setClassLoader

public void setClassLoader(ClassLoader classLoader)
Set the ClassLoader to load resource bundles with, or null for using the thread context class loader on actual access (applying to the thread that does the "getMessage" call).


resolveCodeWithoutArguments

protected String resolveCodeWithoutArguments(String code,
                                             Locale locale)
Description copied from class: AbstractMessageSource
Subclasses can override this method to resolve a message without arguments in an optimized fashion, i.e. to resolve a message without involving a MessageFormat.

The default implementation does use MessageFormat, through delegating to the resolveCode method. Subclasses are encouraged to replace this with optimized resolution.

Unfortunately, java.text.MessageFormat is not implemented in an efficient fashion. In particular, it does not detect that a message pattern doesn't contain argument placeholders in the first place. Therefore, it's advisable to circumvent MessageFormat completely for messages without arguments.

Overrides:
resolveCodeWithoutArguments in class AbstractMessageSource
Parameters:
code - the code of the message to resolve
locale - the Locale to resolve the code for (subclasses are encouraged to support internationalization)
Returns:
the message String, or null if not found
See Also:
AbstractMessageSource.resolveCode(java.lang.String, java.util.Locale), MessageFormat

resolveCode

protected MessageFormat resolveCode(String code,
                                    Locale locale)
Description copied from class: AbstractMessageSource
Subclasses must implement this method to resolve a message.

Returns a MessageFormat instance rather than a message String, to allow for appropriate caching of MessageFormats in subclasses.

Subclasses are encouraged to provide optimized resolution for messages without arguments, not involving MessageFormat. See resolveCodeWithoutArguments javadoc for details.

Specified by:
resolveCode in class AbstractMessageSource
Parameters:
code - the code of the message to resolve
locale - the Locale to resolve the code for (subclasses are encouraged to support internationalization)
Returns:
the MessageFormat for the message, or null if not found
See Also:
AbstractMessageSource.resolveCodeWithoutArguments(java.lang.String, java.util.Locale)

getResourceBundle

protected ResourceBundle getResourceBundle(String basename,
                                           Locale locale)
Return a ResourceBundle for the given basename and code, fetching already generated MessageFormats from the cache.

Parameters:
basename - the basename of the ResourceBundle
locale - the Locale to find the ResourceBundle for
Returns:
the resulting ResourceBundle, or null if none found for the given basename and Locale

getMessageFormat

protected MessageFormat getMessageFormat(ResourceBundle bundle,
                                         String code,
                                         Locale locale)
                                  throws MissingResourceException
Return a MessageFormat for the given bundle and code, fetching already generated MessageFormats from the cache.

Parameters:
bundle - the ResourceBundle to work on
code - the message code to retrieve
locale - the Locale to use to build the MessageFormat
Returns:
the resulting MessageFormat, or null if no message defined for the given code
Throws:
MissingResourceException

toString

public String toString()
Show the configuration of this MessageSource.



Copyright (C) 2003-2004 The Spring Framework Project.