org.springframework.context.support
Class ReloadableResourceBundleMessageSource

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

public class ReloadableResourceBundleMessageSource
extends AbstractMessageSource
implements ResourceLoaderAware

MessageSource that accesses the ResourceBundles with the specified basenames. This class uses java.util.Properties instances as its internal data structure for messages, loading them via a PropertiesPersister strategy: The default strategy can load properties files with a specific charset.

In contrast to ResourceBundleMessageSource, this class supports reloading of properties files through the "cacheSeconds" setting, and also through programmatically clearing the properties cache. Since application servers do typically cache all files loaded from the classpath, it is necessary to store resources somewhere else (for example, in the "WEB-INF" directory of a web app). Otherwise changes of files in the classpath are not reflected in the application.

Note that the "basename" respectively "basenames" property has a different convention here: It follows the basic ResourceBundle rule of not specifying file extension or language codes, but can refer to any Spring resource location (instead of being restricted to classpath resources). With a "classpath:" prefix, resources can still be loaded from the classpath, but "cacheSeconds" values other than "-1" (caching forever) will not work in this case.

This MessageSource can easily be used outside an ApplicationContext: It uses a DefaultResourceLoader as default, getting overridden with the ApplicationContext if running in a context. It does not have any other specific dependencies.

Author:
Thomas Achleitner, Juergen Hoeller
See Also:
setCacheSeconds(int), setBasenames(java.lang.String[]), setDefaultEncoding(java.lang.String), setFileEncodings(java.util.Properties), setPropertiesPersister(org.springframework.util.PropertiesPersister), setResourceLoader(org.springframework.core.io.ResourceLoader), DefaultPropertiesPersister, DefaultResourceLoader, ResourceBundleMessageSource, ResourceBundle

Nested Class Summary
protected static class ReloadableResourceBundleMessageSource.PropertiesHolder
          PropertiesHolder for caching.
 
Field Summary
static java.lang.String PROPERTIES_SUFFIX
           
 
Fields inherited from class org.springframework.context.support.AbstractMessageSource
logger
 
Constructor Summary
ReloadableResourceBundleMessageSource()
           
 
Method Summary
protected  java.util.List calculateAllFilenames(java.lang.String basename, java.util.Locale locale)
          Calculate all filenames for the given bundle basename and Locale.
protected  java.util.List calculateFilenamesForLocale(java.lang.String basename, java.util.Locale locale)
          Calculate the filenames for the given bundle basename and Locale, appending language code, country code, and variant code.
 void clearCache()
          Clear the resource bundle cache.
 void clearCacheIncludingAncestors()
          Clear the resource bundle caches of this MessageSource and all its ancestors.
protected  ReloadableResourceBundleMessageSource.PropertiesHolder getProperties(java.lang.String filename)
          Get PropertiesHolder for the given filename, either from the cache or freshly loaded.
protected  ReloadableResourceBundleMessageSource.PropertiesHolder refreshProperties(java.lang.String filename, ReloadableResourceBundleMessageSource.PropertiesHolder propHolder)
          Refresh the PropertiesHolder for the given bundle filename.
protected  java.text.MessageFormat resolveCode(java.lang.String code, java.util.Locale locale)
          Subclasses must implement this method to resolve a message.
 void setBasename(java.lang.String basename)
          Set a single basename, following the basic ResourceBundle convention of not specifying file extension or language codes, but in contrast to ResourceBundleMessageSource referring to a Spring resource location: e.g.
 void setBasenames(java.lang.String[] basenames)
          Set an array of basenames, each following the above-mentioned special convention.
 void setCacheSeconds(int cacheSeconds)
          Set the number of seconds to cache loaded properties files.
 void setDefaultEncoding(java.lang.String defaultEncoding)
          Set the default charset to use for parsing properties files.
 void setFallbackToSystemLocale(boolean fallbackToSystemLocale)
          Set whether to fall back to the system Locale if no files for a specific Locale have been found.
 void setFileEncodings(java.util.Properties fileEncodings)
          Set per-file charsets to use for parsing properties files.
 void setPropertiesPersister(PropertiesPersister propertiesPersister)
          Set the PropertiesPersister to use for parsing properties files.
 void setResourceLoader(ResourceLoader resourceLoader)
          Set the ResourceLoader to use for loading bundle properties files.
 java.lang.String toString()
           
 
Methods inherited from class org.springframework.context.support.AbstractMessageSource
getMessage, getMessage, getMessage, getMessageInternal, getParentMessageSource, setParentMessageSource, setUseCodeAsDefaultMessage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PROPERTIES_SUFFIX

public static final java.lang.String PROPERTIES_SUFFIX
See Also:
Constant Field Values
Constructor Detail

ReloadableResourceBundleMessageSource

public ReloadableResourceBundleMessageSource()
Method Detail

setBasename

public void setBasename(java.lang.String basename)
Set a single basename, following the basic ResourceBundle convention of not specifying file extension or language codes, but in contrast to ResourceBundleMessageSource referring to a Spring resource location: e.g. "WEB-INF/messages" for "WEB-INF/messages.properties", "WEB-INF/messages_en.properties", etc.

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

setBasenames

public void setBasenames(java.lang.String[] basenames)
Set an array of basenames, each following the above-mentioned special convention. 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

setDefaultEncoding

public void setDefaultEncoding(java.lang.String defaultEncoding)
Set the default charset to use for parsing properties files. Used if no file-specific charset is specified for a file.

Default is none, using java.util.Properties' default charset.

See Also:
setFileEncodings(java.util.Properties), PropertiesPersister.load(java.util.Properties, java.io.InputStream)

setFileEncodings

public void setFileEncodings(java.util.Properties fileEncodings)
Set per-file charsets to use for parsing properties files.

Parameters:
fileEncodings - Properties with filenames as keys and charset names as values. Filenames have to match the basename syntax, with optional locale-specific appendices: e.g. "WEB-INF/messages" or "WEB-INF/messages_en".
See Also:
setBasenames(java.lang.String[]), PropertiesPersister.load(java.util.Properties, java.io.InputStream)

setFallbackToSystemLocale

public void setFallbackToSystemLocale(boolean fallbackToSystemLocale)
Set whether to fall back to the system Locale if no files for a specific Locale have been found. Default is true; if this is turned off, the only fallback will be the default file (e.g. "messages.properties" for basename "messages").

Falling back to the system Locale is the default behavior of java.util.ResourceBundle. However, this is often not desirable in an application server environment, where the system Locale is not relevant to the application at all: Set this flag to "false" in such a scenario.


setCacheSeconds

public void setCacheSeconds(int cacheSeconds)
Set the number of seconds to cache loaded properties files.


setPropertiesPersister

public void setPropertiesPersister(PropertiesPersister propertiesPersister)
Set the PropertiesPersister to use for parsing properties files. The default is DefaultPropertiesPersister.

See Also:
DefaultPropertiesPersister

setResourceLoader

public void setResourceLoader(ResourceLoader resourceLoader)
Set the ResourceLoader to use for loading bundle properties files. The default is DefaultResourceLoader. Will get overridden by the ApplicationContext if running in a context.

Specified by:
setResourceLoader in interface ResourceLoaderAware
Parameters:
resourceLoader - ResourceLoader object to be used by this object
See Also:
DefaultResourceLoader

resolveCode

protected java.text.MessageFormat resolveCode(java.lang.String code,
                                              java.util.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.

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

calculateAllFilenames

protected java.util.List calculateAllFilenames(java.lang.String basename,
                                               java.util.Locale locale)
Calculate all filenames for the given bundle basename and Locale. Will calculate filenames for the given Locale, the system Locale (if applicable), and the default file.

Parameters:
basename - the basename of the bundle
locale - the locale
Returns:
the List of filenames to check
See Also:
setFallbackToSystemLocale(boolean), calculateFilenamesForLocale(java.lang.String, java.util.Locale)

calculateFilenamesForLocale

protected java.util.List calculateFilenamesForLocale(java.lang.String basename,
                                                     java.util.Locale locale)
Calculate the filenames for the given bundle basename and Locale, appending language code, country code, and variant code. E.g.: basename "messages", Locale "de_AT_oo" -> "messages_de_AT_OO", "messages_de_AT", "messages_de".

Parameters:
basename - the basename of the bundle
locale - the locale
Returns:
the List of filenames to check

getProperties

protected ReloadableResourceBundleMessageSource.PropertiesHolder getProperties(java.lang.String filename)
Get PropertiesHolder for the given filename, either from the cache or freshly loaded.


refreshProperties

protected ReloadableResourceBundleMessageSource.PropertiesHolder refreshProperties(java.lang.String filename,
                                                                                   ReloadableResourceBundleMessageSource.PropertiesHolder propHolder)
Refresh the PropertiesHolder for the given bundle filename. The holder can be null if not cached before, or a timed-out cache entry (potentially getting re-validated against the current last-modified timestamp).


clearCache

public void clearCache()
Clear the resource bundle cache. Following resolve calls will lead to reloading of the properties files.


clearCacheIncludingAncestors

public void clearCacheIncludingAncestors()
Clear the resource bundle caches of this MessageSource and all its ancestors.

See Also:
clearCache()

toString

public java.lang.String toString()


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