public class ReloadableResourceBundleMessageSource extends AbstractResourceBasedMessageSource implements ResourceLoaderAware
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
:
e.g. 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 of 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!
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)
,
DefaultPropertiesPersister
,
DefaultResourceLoader
,
ResourceBundleMessageSource
,
ResourceBundle
Modifier and Type | Class and Description |
---|---|
protected class |
ReloadableResourceBundleMessageSource.PropertiesHolder
PropertiesHolder for caching.
|
Modifier and Type | Field and Description |
---|---|
private java.util.concurrent.ConcurrentMap<java.lang.String,java.util.Map<java.util.Locale,java.util.List<java.lang.String>>> |
cachedFilenames
Cache to hold filename lists per Locale
|
private java.util.concurrent.ConcurrentMap<java.util.Locale,ReloadableResourceBundleMessageSource.PropertiesHolder> |
cachedMergedProperties
Cache to hold merged loaded properties per locale
|
private java.util.concurrent.ConcurrentMap<java.lang.String,ReloadableResourceBundleMessageSource.PropertiesHolder> |
cachedProperties
Cache to hold already loaded properties per filename
|
private boolean |
concurrentRefresh |
private java.util.Properties |
fileEncodings |
private static java.lang.String |
PROPERTIES_SUFFIX |
private PropertiesPersister |
propertiesPersister |
private ResourceLoader |
resourceLoader |
private static java.lang.String |
XML_SUFFIX |
logger
Constructor and Description |
---|
ReloadableResourceBundleMessageSource() |
Modifier and Type | Method and Description |
---|---|
protected java.util.List<java.lang.String> |
calculateAllFilenames(java.lang.String basename,
java.util.Locale locale)
Calculate all filenames for the given bundle basename and Locale.
|
protected java.util.List<java.lang.String> |
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 |
getMergedProperties(java.util.Locale locale)
Get a PropertiesHolder that contains the actually visible properties
for a Locale, after merging all specified resource bundles.
|
protected ReloadableResourceBundleMessageSource.PropertiesHolder |
getProperties(java.lang.String filename)
Get a PropertiesHolder for the given filename, either from the
cache or freshly loaded.
|
protected java.util.Properties |
loadProperties(Resource resource,
java.lang.String filename)
Load the properties from the given resource.
|
protected java.util.Properties |
newProperties()
Template method for creating a plain new
Properties instance. |
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)
Resolves the given message code as key in the retrieved bundle files,
using a cached MessageFormat instance per message code.
|
protected java.lang.String |
resolveCodeWithoutArguments(java.lang.String code,
java.util.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 |
setConcurrentRefresh(boolean concurrentRefresh)
Specify whether to allow for concurrent refresh behavior, i.e.
|
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() |
addBasenames, getBasenameSet, getCacheMillis, getDefaultEncoding, isFallbackToSystemLocale, setBasename, setBasenames, setCacheMillis, setCacheSeconds, setDefaultEncoding, setFallbackToSystemLocale
getCommonMessages, getDefaultMessage, getDefaultMessage, getMessage, getMessage, getMessage, getMessageFromParent, getMessageInternal, getParentMessageSource, isUseCodeAsDefaultMessage, resolveArguments, setCommonMessages, setParentMessageSource, setUseCodeAsDefaultMessage
createMessageFormat, formatMessage, isAlwaysUseMessageFormat, renderDefaultMessage, setAlwaysUseMessageFormat
private static final java.lang.String PROPERTIES_SUFFIX
private static final java.lang.String XML_SUFFIX
private java.util.Properties fileEncodings
private boolean concurrentRefresh
private PropertiesPersister propertiesPersister
private ResourceLoader resourceLoader
private final java.util.concurrent.ConcurrentMap<java.lang.String,java.util.Map<java.util.Locale,java.util.List<java.lang.String>>> cachedFilenames
private final java.util.concurrent.ConcurrentMap<java.lang.String,ReloadableResourceBundleMessageSource.PropertiesHolder> cachedProperties
private final java.util.concurrent.ConcurrentMap<java.util.Locale,ReloadableResourceBundleMessageSource.PropertiesHolder> cachedMergedProperties
public ReloadableResourceBundleMessageSource()
public void setFileEncodings(java.util.Properties fileEncodings)
Only applies to classic properties files, not to XML files.
fileEncodings
- Properties with filenames as keys and charset
names as values. Filenames have to match the basename syntax,
with optional locale-specific components: e.g. "WEB-INF/messages"
or "WEB-INF/messages_en".AbstractResourceBasedMessageSource.setBasenames(java.lang.String...)
,
PropertiesPersister.load(java.util.Properties, java.io.InputStream)
public void setConcurrentRefresh(boolean concurrentRefresh)
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".
AbstractResourceBasedMessageSource.setCacheSeconds(int)
public void setPropertiesPersister(PropertiesPersister propertiesPersister)
The default is a DefaultPropertiesPersister.
DefaultPropertiesPersister
public void setResourceLoader(ResourceLoader resourceLoader)
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.
setResourceLoader
in interface ResourceLoaderAware
resourceLoader
- ResourceLoader object to be used by this objectDefaultResourceLoader
,
ResourceLoaderAware
protected java.lang.String resolveCodeWithoutArguments(java.lang.String code, java.util.Locale locale)
resolveCodeWithoutArguments
in class AbstractMessageSource
code
- the code of the message to resolvelocale
- the locale to resolve the code for
(subclasses are encouraged to support internationalization)null
if not foundAbstractMessageSource.resolveCode(java.lang.String, java.util.Locale)
,
MessageFormat
protected java.text.MessageFormat resolveCode(java.lang.String code, java.util.Locale locale)
resolveCode
in class AbstractMessageSource
code
- the code of the message to resolvelocale
- the locale to resolve the code for
(subclasses are encouraged to support internationalization)null
if not foundAbstractMessageSource.resolveCodeWithoutArguments(String, java.util.Locale)
protected ReloadableResourceBundleMessageSource.PropertiesHolder getMergedProperties(java.util.Locale locale)
Only used when caching resource bundle contents forever, i.e. with cacheSeconds < 0. Therefore, merged properties are always cached forever.
protected java.util.List<java.lang.String> calculateAllFilenames(java.lang.String basename, java.util.Locale locale)
basename
- the basename of the bundlelocale
- the localeAbstractResourceBasedMessageSource.setFallbackToSystemLocale(boolean)
,
calculateFilenamesForLocale(java.lang.String, java.util.Locale)
protected java.util.List<java.lang.String> calculateFilenamesForLocale(java.lang.String basename, java.util.Locale locale)
Follows the rules defined by Locale.toString()
.
basename
- the basename of the bundlelocale
- the localeprotected ReloadableResourceBundleMessageSource.PropertiesHolder getProperties(java.lang.String filename)
filename
- the bundle filename (basename + Locale)protected ReloadableResourceBundleMessageSource.PropertiesHolder refreshProperties(java.lang.String filename, ReloadableResourceBundleMessageSource.PropertiesHolder propHolder)
null
if not cached before, or a timed-out cache entry
(potentially getting re-validated against the current last-modified timestamp).filename
- the bundle filename (basename + Locale)propHolder
- the current PropertiesHolder for the bundleprotected java.util.Properties loadProperties(Resource resource, java.lang.String filename) throws java.io.IOException
resource
- the resource to load fromfilename
- the original bundle filename (basename + Locale)java.io.IOException
- if properties loading failedprotected java.util.Properties newProperties()
Properties
instance.
The default implementation simply calls Properties.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.
public void clearCache()
public void clearCacheIncludingAncestors()
clearCache()
public java.lang.String toString()
toString
in class java.lang.Object