org.springframework.context.support
Class ReloadableResourceBundleMessageSource

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 encoding.

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 base names set as the "basename" and "basenames" properties are treated in a slightly different fashion than the "basename" property of ResourceBundleMessageSource. 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 implementation is usually slightly faster than ResourceBundleMessageSource, which builds on java.util.ResourceBundle - in the default mode, i.e. when caching forever. With "cacheSeconds" set to 1, message lookup takes about twice as long - with the benefit that changes in individual properties files are detected with a maximum delay of 1 second. Higher "cacheSeconds" values usually don't make a significant difference.

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.

Thanks to Thomas Achleitner for providing the initial implementation of this message source!

SinceNot specified.
VersionNot specified.
AuthorJuergen Hoeller
Wiki javadoc Use textile entry format.
Add your comments here.
Fields inherited from org.springframework.context.supportAbstractMessageSource
Method Summary
protected List calculateAllFilenames( String basename, Locale locale )
Calculate all filenames for the given bundle basename and Locale.
protected List calculateFilenamesForLocale( String basename, 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 getMergedProperties( Locale locale )
Get a PropertiesHolder that contains the actually visible properties for a Locale, after merging all specified resource bundles.
protected ReloadableResourceBundleMessageSource.PropertiesHolder getProperties( String filename )
Get a PropertiesHolder for the given filename, either from the cache or freshly loaded.
protected Properties loadProperties( Resource resource, String filename )
Load the properties from the given resource.
protected ReloadableResourceBundleMessageSource.PropertiesHolder refreshProperties( String filename, ReloadableResourceBundleMessageSource.PropertiesHolder propHolder )
Refresh the PropertiesHolder for the given bundle filename.
protected MessageFormat resolveCode( String code, Locale locale )
Resolves the given message code as key in the retrieved bundle files, using a cached MessageFormat instance per message code.
protected String resolveCodeWithoutArguments( String code, Locale locale )
Resolves the given message code as key in the retrieved bundle files, returning the value found in the bundle as-is (without MessageFormat parsing).
void setBasename( 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( 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( 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( 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.
String toString()
No description provided.
ReloadableResourceBundleMessageSource
public ReloadableResourceBundleMessageSource ( )
No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
calculateAllFilenames
protected List calculateAllFilenames ( String basename, 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
TypeNameDescription
String basename the basename of the bundle
Locale locale the locale
Wiki javadoc Use textile entry format.
Add your comments here.
calculateFilenamesForLocale
protected List calculateFilenamesForLocale ( String basename, 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
TypeNameDescription
String basename the basename of the bundle
Locale locale the locale
Wiki javadoc Use textile entry format.
Add your comments here.
clearCache
public void clearCache ( )
Clear the resource bundle cache. Subsequent resolve calls will lead to reloading of the properties files.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
clearCacheIncludingAncestors
public void clearCacheIncludingAncestors ( )
Clear the resource bundle caches of this MessageSource and all its ancestors.
Returns void No description provided.
See also
Wiki javadoc Use textile entry format.
Add your comments here.
getMergedProperties
protected ReloadableResourceBundleMessageSource.PropertiesHolder getMergedProperties ( Locale locale )
Get a PropertiesHolder that contains the actually visible properties for a Locale, after merging all specified resource bundles. Either fetches the holder from the cache or freshly loads it.

Only used when caching resource bundle contents forever, i.e. with cacheSeconds < 0. Therefore, merged properties are always cached forever.

Parameters
TypeNameDescription
Locale locale No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
getProperties
protected ReloadableResourceBundleMessageSource.PropertiesHolder getProperties ( String filename )
Get a PropertiesHolder for the given filename, either from the cache or freshly loaded.
Parameters
TypeNameDescription
String filename the bundle filename (basename + Locale)
Wiki javadoc Use textile entry format.
Add your comments here.
loadProperties
protected Properties loadProperties ( Resource resource, String filename )
throws
Load the properties from the given resource.
Parameters
TypeNameDescription
Resource resource the resource to load from
String filename the original bundle filename (basename + Locale)
Exceptions
IOException if properties loading failed
Wiki javadoc Use textile entry format.
Add your comments here.
refreshProperties
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).
Parameters
TypeNameDescription
String filename the bundle filename (basename + Locale)
ReloadableResourceBundleMessageSource.PropertiesHolder propHolder the current PropertiesHolder for the bundle
Wiki javadoc Use textile entry format.
Add your comments here.
resolveCode
protected MessageFormat resolveCode ( String code, Locale locale )
Resolves the given message code as key in the retrieved bundle files, using a cached MessageFormat instance per message code.
Overrides method in AbstractMessageSource
Parameters
TypeNameDescription
String code No description provided.
Locale locale No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
resolveCodeWithoutArguments
protected String resolveCodeWithoutArguments ( String code, Locale locale )
Resolves the given message code as key in the retrieved bundle files, returning the value found in the bundle as-is (without MessageFormat parsing).
Overrides method in AbstractMessageSource
Parameters
TypeNameDescription
String code No description provided.
Locale locale No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setBasename
public void setBasename ( 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.

As of Spring 1.2.2, XML properties files are also supported: e.g. "WEB-INF/messages" will find and load "WEB-INF/messages.xml", "WEB-INF/messages_en.xml", etc as well. Note that this will only work on JDK 1.5+.

Parameters
TypeNameDescription
String basename the single basename
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setBasenames
public void setBasenames ( 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
TypeNameDescription
String [] basenames an array of basenames
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setCacheSeconds
public void setCacheSeconds ( int cacheSeconds )
Set the number of seconds to cache loaded properties files.
  • Default is "-1", indicating to cache forever (just like java.util.ResourceBundle).
  • A positive number will cache loaded properties files for the given number of seconds. This is essentially the interval between refresh checks. Note that a refresh attempt will first check the last-modified timestamp of the file before actually reloading it; so if files don't change, this interval can be set rather low, as refresh attempts will not actually reload.
  • A value of "0" will check the last-modified timestamp of the file on every message access. Do not use this in a production environment!
Parameters
TypeNameDescription
int cacheSeconds No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setDefaultEncoding
public void setDefaultEncoding ( 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 the java.util.Properties default encoding.

Only applies to classic properties files, not to XML files.

Parameters
TypeNameDescription
String defaultEncoding No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
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.

Parameters
TypeNameDescription
boolean fallbackToSystemLocale No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setFileEncodings
public void setFileEncodings ( Properties fileEncodings )
Set per-file charsets to use for parsing properties files.

Only applies to classic properties files, not to XML files.

Parameters
TypeNameDescription
Properties 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".
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setPropertiesPersister
public void setPropertiesPersister ( PropertiesPersister propertiesPersister )
Set the PropertiesPersister to use for parsing properties files.

The default is a DefaultPropertiesPersister.

Parameters
TypeNameDescription
PropertiesPersister propertiesPersister No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setResourceLoader
public void setResourceLoader ( ResourceLoader resourceLoader )
Set the ResourceLoader to use for loading bundle properties files.

The default is a DefaultResourceLoader. Will get overridden by the ApplicationContext if running in a context, as it implements the ResourceLoaderAware interface. Can be manually overridden when running outside of an ApplicationContext.

Implements method in ResourceLoaderAware
Parameters
TypeNameDescription
ResourceLoader resourceLoader No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
toString
public String toString ( )
No description provided.
Overrides method in Object
Wiki javadoc Use textile entry format.
Add your comments here.