Package org.springframework.context.support

# Class ReloadableResourceBundleMessageSource

java.lang.Object
org.springframework.context.support.MessageSourceSupport
org.springframework.context.support.AbstractMessageSource
org.springframework.context.support.AbstractResourceBasedMessageSource
org.springframework.context.support.ReloadableResourceBundleMessageSource

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `HierarchicalMessageSource`, `MessageSource`, `ResourceLoaderAware`

public class ReloadableResourceBundleMessageSource extends AbstractResourceBasedMessageSource implements ResourceLoaderAware
Spring-specific `MessageSource` implementation that accesses resource bundles using specified basenames, participating in the Spring `ApplicationContext`'s resource loading.

In contrast to the JDK-based `ResourceBundleMessageSource`, this class uses `Properties` instances as its custom data structure for messages, loading them via a `PropertiesPersister` strategy from Spring `Resource` handles. This strategy is not only capable of reloading files based on timestamp changes, but also of loading properties files with a specific character encoding. It will detect XML property files as well.

Note that the basenames set as `"basenames"` property are treated in a slightly different fashion than the "basenames" 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) might not work reliably in this case.

For a typical web application, message files could be placed in `WEB-INF`: for example, a "WEB-INF/messages" basename would find a "WEB-INF/messages.properties", "WEB-INF/messages_en.properties" etc arrangement as well as "WEB-INF/messages.xml", "WEB-INF/messages_en.xml" etc. Note that message definitions in a previous resource bundle will override ones in a later bundle, due to sequential lookup.

This MessageSource can easily be used outside an `ApplicationContext`: it will use a `DefaultResourceLoader` as default, simply getting overridden with the ApplicationContext's resource loader 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!

Author:
  Juergen Hoeller, Sebastien Deleuze
See Also:
  - `AbstractResourceBasedMessageSource.setCacheSeconds(int)`
  - `AbstractResourceBasedMessageSource.setBasenames(java.lang.String...)`
  - `AbstractResourceBasedMessageSource.setDefaultEncoding(java.lang.String)`
  - `setFileEncodings(java.util.Properties)`
  - `setPropertiesPersister(org.springframework.util.PropertiesPersister)`
  - `setResourceLoader(org.springframework.core.io.ResourceLoader)`
  - `DefaultResourceLoader`
  - `ResourceBundleMessageSource`
  - `ResourceBundle`

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`protected class`
`ReloadableResourceBundleMessageSource.PropertiesHolder`
PropertiesHolder for caching.

## Field Summary

### Fields inherited from class org.springframework.context.support.MessageSourceSupport

`logger`

## Constructor Summary

Constructors
Constructor
Description
`ReloadableResourceBundleMessageSource()`

## Method Summary

Modifier and Type
Method
Description
`protected List<String>`
`calculateAllFilenames(String basename, Locale locale)`
Calculate all filenames for the given bundle basename and Locale.
`protected List<String>`
`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 List<ReloadableResourceBundleMessageSource.PropertiesHolder>`
`collectPropertiesToMerge(Locale locale)`
Determine the properties to merge based on the specified basenames.
`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(org.springframework.core.io.Resource resource, String filename)`
Load the properties from the given resource.
`protected ReloadableResourceBundleMessageSource.PropertiesHolder`
`mergeProperties(List<ReloadableResourceBundleMessageSource.PropertiesHolder> holders)`
Merge the given properties holders into a single holder.
`protected Properties`
`newProperties()`
Template method for creating a plain new `Properties` instance.
`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).
`protected org.springframework.core.io.Resource`
`resolveResource(String filename)`
Resolve the specified bundle `filename` into a concrete `Resource`, potentially checking multiple sources or file extensions.
`void`
`setConcurrentRefresh(boolean concurrentRefresh)`
Specify whether to allow for concurrent refresh behavior, i.e.
`void`
`setFileEncodings(Properties fileEncodings)`
Set per-file charsets to use for parsing properties files.
`void`
`setFileExtensions(List<String> fileExtensions)`
Set the list of supported file extensions.
`void`
`setPropertiesPersister(org.springframework.util.PropertiesPersister propertiesPersister)`
Set the PropertiesPersister to use for parsing properties files.
`void`
`setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader)`
Set the ResourceLoader to use for loading bundle properties files.
`String`
`toString()`

### Methods inherited from class org.springframework.context.support.AbstractResourceBasedMessageSource

`addBasenames, getBasenameSet, getCacheMillis, getDefaultEncoding, getDefaultLocale, isFallbackToSystemLocale, setBasename, setBasenames, setCacheMillis, setCacheSeconds, setDefaultEncoding, setDefaultLocale, setFallbackToSystemLocale`

### Methods inherited from class org.springframework.context.support.AbstractMessageSource

`getCommonMessages, getDefaultMessage, getDefaultMessage, getMessage, getMessage, getMessage, getMessageFromParent, getMessageInternal, getParentMessageSource, isUseCodeAsDefaultMessage, resolveArguments, setCommonMessages, setParentMessageSource, setUseCodeAsDefaultMessage`

### Methods inherited from class org.springframework.context.support.MessageSourceSupport

`createMessageFormat, formatMessage, isAlwaysUseMessageFormat, renderDefaultMessage, setAlwaysUseMessageFormat`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

## Constructor Details

### ReloadableResourceBundleMessageSource

public ReloadableResourceBundleMessageSource()

## Method Details

### setFileExtensions

public void setFileExtensions(List<String> fileExtensions)
Set the list of supported file extensions.

The default is a list containing `.properties` and `.xml`.

Parameters:
  `fileExtensions` - the file extensions (starts with a dot)
Since:
  6.1

### 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:
  `fileEncodings` - a Properties with filenames as keys and charset names as values. Filenames have to match the basename syntax, with optional locale-specific components: for example, "WEB-INF/messages" or "WEB-INF/messages_en".
See Also:
  - `AbstractResourceBasedMessageSource.setBasenames(java.lang.String...)`
  - `PropertiesPersister.load(java.util.Properties, java.io.InputStream)`

### setConcurrentRefresh

public void setConcurrentRefresh(boolean concurrentRefresh)
Specify whether to allow for concurrent refresh behavior, i.e. one thread locked in a refresh attempt for a specific cached properties file whereas other threads keep returning the old properties for the time being, until the refresh attempt has completed.

Default is "true": this behavior is new as of Spring Framework 4.1, minimizing contention between threads. If you prefer the old behavior, i.e. to fully block on refresh, switch this flag to "false".

Since:
  4.1
See Also:
  - `AbstractResourceBasedMessageSource.setCacheSeconds(int)`

### setPropertiesPersister

public void setPropertiesPersister(@Nullable org.springframework.util.PropertiesPersister propertiesPersister)
Set the PropertiesPersister to use for parsing properties files.

The default is `DefaultPropertiesPersister`.

See Also:
  - `DefaultPropertiesPersister.INSTANCE`

### setResourceLoader

public void setResourceLoader(@Nullable org.springframework.core.io.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 an ApplicationContext.

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

### resolveCodeWithoutArguments

@Nullable 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:
  `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

@Nullable 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.

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(String, java.util.Locale)`

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

See Also:
  - `collectPropertiesToMerge(java.util.Locale)`
  - `mergeProperties(java.util.List<org.springframework.context.support.ReloadableResourceBundleMessageSource.PropertiesHolder>)`

### collectPropertiesToMerge

protected List<ReloadableResourceBundleMessageSource.PropertiesHolder> collectPropertiesToMerge(Locale locale)
Determine the properties to merge based on the specified basenames.

Parameters:
  `locale` - the locale
Returns:
  the list of properties holders
Since:
  6.1.4
See Also:
  - `AbstractResourceBasedMessageSource.getBasenameSet()`
  - `calculateAllFilenames(java.lang.String, java.util.Locale)`
  - `mergeProperties(java.util.List<org.springframework.context.support.ReloadableResourceBundleMessageSource.PropertiesHolder>)`

### mergeProperties

protected ReloadableResourceBundleMessageSource.PropertiesHolder mergeProperties(List<ReloadableResourceBundleMessageSource.PropertiesHolder> holders)
Merge the given properties holders into a single holder.

Parameters:
  `holders` - the list of properties holders
Returns:
  a single merged properties holder
Since:
  6.1.4
See Also:
  - `newProperties()`
  - `getMergedProperties(java.util.Locale)`
  - `collectPropertiesToMerge(java.util.Locale)`

### calculateAllFilenames

protected List<String> 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:
  `basename` - the basename of the bundle
  `locale` - the locale
Returns:
  the List of filenames to check
See Also:
  - `AbstractResourceBasedMessageSource.setFallbackToSystemLocale(boolean)`
  - `calculateFilenamesForLocale(java.lang.String, java.util.Locale)`

### calculateFilenamesForLocale

protected List<String> calculateFilenamesForLocale(String basename, Locale locale)
Calculate the filenames for the given bundle basename and Locale, appending language code, country code, and variant code.

For example, basename "messages", Locale "de_AT_oo" → "messages_de_AT_OO", "messages_de_AT", "messages_de".

Follows the rules defined by `Locale.toString()`.

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

### getProperties

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

Parameters:
  `filename` - the bundle filename (basename + Locale)
Returns:
  the current PropertiesHolder for the bundle

### refreshProperties

protected ReloadableResourceBundleMessageSource.PropertiesHolder refreshProperties(String filename, @Nullable 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).

Parameters:
  `filename` - the bundle filename (basename + Locale)
  `propHolder` - the current PropertiesHolder for the bundle
See Also:
  - `resolveResource(String)`

### resolveResource

@Nullable protected org.springframework.core.io.Resource resolveResource(String filename)
Resolve the specified bundle `filename` into a concrete `Resource`, potentially checking multiple sources or file extensions.

If no suitable concrete `Resource` can be resolved, this method returns a `Resource` for which `Resource.exists()` returns `false`, which gets subsequently ignored.

This can be leveraged to check the last modification timestamp or to load properties from alternative sources — for example, from an XML BLOB in a database, or from properties serialized using a custom format such as JSON.

The default implementation delegates to the configured `ResourceLoader` to resolve resources, checking in order for existing `Resource` with extensions defined by `setFileExtensions(List)` (`.properties` and `.xml` by default).

When overriding this method, `loadProperties(Resource, String)` must be capable of loading properties from any type of `Resource` returned by this method. As a consequence, implementors are strongly encouraged to also override `loadProperties()`.

As an alternative to overriding this method, you can configure a `PropertiesPersister` that is capable of dealing with all resources returned by this method. Please note, however, that the default `loadProperties()` implementation uses `loadFromXml` for XML resources and otherwise uses the two `load` methods for other types of resources.

Parameters:
  `filename` - the bundle filename (basename + Locale)
Returns:
  the `Resource` to use, or `null` if none found
Since:
  6.1

### loadProperties

protected Properties loadProperties(org.springframework.core.io.Resource resource, String filename) throws IOException
Load the properties from the given resource.

Parameters:
  `resource` - the resource to load from
  `filename` - the original bundle filename (basename + Locale)
Returns:
  the populated Properties instance
Throws:
  `IOException` - if properties loading failed

### newProperties

protected Properties newProperties()
Template method for creating a plain new `Properties` instance. The default implementation simply calls `Properties()`.

Allows for returning a custom `Properties` extension in subclasses. Overriding methods should just instantiate a custom `Properties` subclass, with no further initialization or population to be performed at that point.

Returns:
  a plain Properties instance
Since:
  4.2

### clearCache

public void clearCache()
Clear the resource bundle cache. Subsequent 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 String toString()

Overrides:
  `toString` in class `Object`