ReloadableResourceBundleMessageSource (Grails 6.0.0)

Package: org.grails.spring.context.support

## [Java] Class ReloadableResourceBundleMessageSource

- org.grails.spring.context.support.ReloadableResourceBundleMessageSource

- All Implemented Interfaces and Traits:
    org.springframework.context.ResourceLoaderAware

      public class ReloadableResourceBundleMessageSource
      extends org.springframework.context.support.AbstractMessageSource
      implements org.springframework.context.ResourceLoaderAware

  Spring-specific org.springframework.context.MessageSource implementation that accesses resource bundles using specified basenames, participating in the Spring org.springframework.context.ApplicationContext's resource loading.

  In contrast to the JDK-based org.springframework.context.support.ResourceBundleMessageSource, this class uses java.util.Properties instances as its custom data structure for messages, loading them via a org.springframework.util.PropertiesPersister strategy from Spring org.springframework.core.io.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.

  In contrast to org.springframework.context.support.ResourceBundleMessageSource, this class supports reloading of properties files through the "cacheSeconds" setting, and also through programmatically clearing the properties cache. Since application servers 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 will not be reflected in the application.

  Note that the base names set as "basenames" property are treated in a slightly different fashion than the "basenames" property of org.springframework.context.support.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 org.springframework.context.support.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 do not make a significant difference.

  This MessageSource can easily be used outside of an org.springframework.context.ApplicationContext: It will use a org.springframework.core.io.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!

  Authors:
    Juergen Hoeller
    Lari Hotari
  See Also:
    setCacheSeconds
    setBasenames
    setDefaultEncoding
    setFileEncodings
    setPropertiesPersister
    setResourceLoader
    org.springframework.util.DefaultPropertiesPersister
    org.springframework.core.io.DefaultResourceLoader
    org.springframework.context.support.ResourceBundleMessageSource
    java.util.ResourceBundle

- - ### Nested Class Summary

    Nested classes
    Modifiers | Name | Description
    --- | --- | ---
    `protected class` | `ReloadableResourceBundleMessageSource.PropertiesHolder` | PropertiesHolder for caching.
    `protected static class` | `ReloadableResourceBundleMessageSource.PropertiesHolderCacheEntry` |

  - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `private static java.lang.String` | `PROPERTIES_SUFFIX` |
    `private static java.lang.String` | `XML_SUFFIX` |
    `private java.lang.String[]` | `basenames` |
    `protected long` | `cacheMillis` |
    `private java.util.concurrent.ConcurrentMap<Pair<java.lang.String, java.util.Locale>, CacheEntry<java.util.List<Pair<java.lang.String, org.springframework.core.io.Resource>>>>` | `cachedFilenames` | Cache to hold filename lists per Locale
    `private java.util.concurrent.ConcurrentMap<java.util.Locale, CacheEntry<PropertiesHolder>>` | `cachedMergedProperties` | Cache to hold merged loaded properties per locale
    `private java.util.concurrent.ConcurrentMap<java.lang.String, CacheEntry<PropertiesHolder>>` | `cachedProperties` | Cache to hold already loaded properties per filename
    `private java.util.concurrent.ConcurrentMap<java.lang.String, CacheEntry<org.springframework.core.io.Resource>>` | `cachedResources` |
    `private java.lang.String` | `defaultEncoding` |
    `private boolean` | `fallbackToSystemLocale` |
    `protected long` | `fileCacheMillis` |
    `private java.util.Properties` | `fileEncodings` |
    `private org.springframework.util.PropertiesPersister` | `propertiesPersister` |
    `private org.springframework.core.io.ResourceLoader` | `resourceLoader` |

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `protected java.util.List<Pair<java.lang.String, org.springframework.core.io.Resource>>` | `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.
    `` | `public PropertiesHolder` | `call()`
    `` | `public java.util.List<Pair<java.lang.String, org.springframework.core.io.Resource>>` | `call()`
    `` | `public PropertiesHolder` | `call()`
    `` | `public CacheEntry` | `call()`
    `` | `public org.springframework.core.io.Resource` | `call()`
    `` | `public void` | `clearCache()` Clear the resource bundle cache.
    `` | `public void` | `clearCacheIncludingAncestors()` Clear the resource bundle caches of this MessageSource and all its ancestors.
    `` | `public java.util.Set<java.lang.String>` | `getBundleCodes(java.util.Locale locale, java.lang.String basenames)` Retrieves all codes from one or multiple basenames
    `` | `protected 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 PropertiesHolder` | `getProperties(java.lang.String filename, org.springframework.core.io.Resource resource)` Get a PropertiesHolder for the given filename, either from the cache or freshly loaded.
    `` | `protected java.util.List<java.lang.String>` | `getValidBasenames(java.lang.String[] basenames)`
    `` | `protected java.util.Properties` | `loadProperties(org.springframework.core.io.Resource resource, java.lang.String filename)` Load the properties from the given resource.
    `` | `protected org.springframework.core.io.Resource` | `locateResource(java.lang.String filename)`
    `` | `protected org.springframework.core.io.Resource` | `locateResourceWithoutCache(java.lang.String 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).
    `` | `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 org.springframework.context.support.ResourceBundleMessageSource referring to a Spring resource location: e.g.
    `` | `public void` | `setBasenames(java.lang.String basenames)` Set an array of basenames, each following the basic ResourceBundle convention of not specifying file extension or language codes, but in contrast to org.springframework.context.support.ResourceBundleMessageSource referring to a Spring resource location: e.g.
    `` | `public void` | `setCacheSeconds(int cacheSeconds)` Set the number of seconds to cache the list of matching properties files.
    `` | `public void` | `setDefaultEncoding(java.lang.String defaultEncoding)` Set the default charset to use for parsing properties files.
    `` | `public void` | `setFallbackToSystemLocale(boolean fallbackToSystemLocale)` Set whether to fall back to the system Locale if no files for a specific Locale have been found.
    `` | `public void` | `setFileCacheSeconds(int fileCacheSeconds)` Set the number of seconds to cache loaded properties files.
    `` | `public void` | `setFileEncodings(java.util.Properties fileEncodings)` Set per-file charsets to use for parsing properties files.
    `` | `public void` | `setPropertiesPersister(org.springframework.util.PropertiesPersister propertiesPersister)` Set the PropertiesPersister to use for parsing properties files.
    `` | `public void` | `setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader)` Set the ResourceLoader to use for loading bundle properties files.
    `` | `public java.lang.String` | `toString()`

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class org.springframework.context.support.AbstractMessageSource` | `org.springframework.context.support.AbstractMessageSource#setUseCodeAsDefaultMessage(boolean), org.springframework.context.support.AbstractMessageSource#getParentMessageSource(), org.springframework.context.support.AbstractMessageSource#setParentMessageSource(org.springframework.context.MessageSource), org.springframework.context.support.AbstractMessageSource#setCommonMessages(java.util.Properties), org.springframework.context.support.AbstractMessageSource#getMessage(java.lang.String, [Ljava.lang.Object;, java.util.Locale), org.springframework.context.support.AbstractMessageSource#getMessage(java.lang.String, [Ljava.lang.Object;, java.lang.String, java.util.Locale), org.springframework.context.support.AbstractMessageSource#getMessage(org.springframework.context.MessageSourceResolvable, java.util.Locale), org.springframework.context.support.AbstractMessageSource#setAlwaysUseMessageFormat(boolean), org.springframework.context.support.AbstractMessageSource#wait(long), org.springframework.context.support.AbstractMessageSource#wait(long, int), org.springframework.context.support.AbstractMessageSource#wait(), org.springframework.context.support.AbstractMessageSource#equals(java.lang.Object), org.springframework.context.support.AbstractMessageSource#toString(), org.springframework.context.support.AbstractMessageSource#hashCode(), org.springframework.context.support.AbstractMessageSource#getClass(), org.springframework.context.support.AbstractMessageSource#notify(), org.springframework.context.support.AbstractMessageSource#notifyAll()`

- - ### Field Detail

    - #### private static final java.lang.String PROPERTIES_SUFFIX

    - #### private static final java.lang.String XML_SUFFIX

    - #### private java.lang.String[] basenames

    - #### protected long cacheMillis

    - #### private final java.util.concurrent.ConcurrentMap<Pair<java.lang.String, java.util.Locale>, CacheEntry<java.util.List<Pair<java.lang.String, org.springframework.core.io.Resource>>>> cachedFilenames

      Cache to hold filename lists per Locale

    - #### private final java.util.concurrent.ConcurrentMap<java.util.Locale, CacheEntry<PropertiesHolder>> cachedMergedProperties

      Cache to hold merged loaded properties per locale

    - #### private final java.util.concurrent.ConcurrentMap<java.lang.String, CacheEntry<PropertiesHolder>> cachedProperties

      Cache to hold already loaded properties per filename

    - #### private final java.util.concurrent.ConcurrentMap<java.lang.String, CacheEntry<org.springframework.core.io.Resource>> cachedResources

    - #### private java.lang.String defaultEncoding

    - #### private boolean fallbackToSystemLocale

    - #### protected long fileCacheMillis

    - #### private java.util.Properties fileEncodings

    - #### private org.springframework.util.PropertiesPersister propertiesPersister

    - #### private org.springframework.core.io.ResourceLoader resourceLoader

  - ### Method Detail

    - #### protected java.util.List<Pair<java.lang.String, org.springframework.core.io.Resource>> 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
        calculateFilenamesForLocale

    - #### 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. E.g.: basename "messages", Locale "de_AT_oo" -> "messages_de_AT_OO", "messages_de_AT", "messages_de".

      Follows the rules defined by java.util.Locale#toString().

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

    - #### @java.lang.Override public PropertiesHolder call()

    - #### @java.lang.Override public java.util.List<Pair<java.lang.String, org.springframework.core.io.Resource>> call()

    - #### @java.lang.Override public PropertiesHolder call()

    - #### @java.lang.Override public CacheEntry call()

    - #### @java.lang.Override public org.springframework.core.io.Resource call()

    - #### public void clearCache()

      Clear the resource bundle cache. Subsequent resolve calls will lead to reloading of the properties files.

    - #### public void clearCacheIncludingAncestors()

      Clear the resource bundle caches of this MessageSource and all its ancestors.

      See Also:
        clearCache

    - #### public java.util.Set<java.lang.String> getBundleCodes(java.util.Locale locale, java.lang.String basenames)

      Retrieves all codes from one or multiple basenames

      Parameters:
        `locale` - the locale
        `basenames` - the basenames of the bundle
      Returns:
        a list with all codes from valid registered bundles

    - #### protected PropertiesHolder getMergedProperties(java.util.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.

    - #### @java.lang.SuppressWarnings("rawtypes") protected PropertiesHolder getProperties(java.lang.String filename, org.springframework.core.io.Resource resource)

      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

    - #### protected java.util.List<java.lang.String> getValidBasenames(java.lang.String[] basenames)

    - #### protected java.util.Properties loadProperties(org.springframework.core.io.Resource resource, java.lang.String filename)

      Load the properties from the given resource.

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

    - #### protected org.springframework.core.io.Resource locateResource(java.lang.String filename)

    - #### protected org.springframework.core.io.Resource locateResourceWithoutCache(java.lang.String filename)

    - #### @java.lang.Override 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.

    - #### @java.lang.Override 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).

    - #### 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 org.springframework.context.support.ResourceBundleMessageSource referring to a Spring resource location: e.g. "WEB-INF/messages" for "WEB-INF/messages.properties", "WEB-INF/messages_en.properties", etc.

      XML properties files are also supported: .g. "WEB-INF/messages" will find and load "WEB-INF/messages.xml", "WEB-INF/messages_en.xml", etc as well.

      Parameters:
        `basename` - the single basename
      See Also:
        setBasenames
        org.springframework.core.io.ResourceEditor
        java.util.ResourceBundle

    - #### public void setBasenames(java.lang.String basenames)

      Set an array of basenames, each following the basic ResourceBundle convention of not specifying file extension or language codes, but in contrast to org.springframework.context.support.ResourceBundleMessageSource referring to a Spring resource location: e.g. "WEB-INF/messages" for "WEB-INF/messages.properties", "WEB-INF/messages_en.properties", etc.

      XML properties files are also supported: .g. "WEB-INF/messages" will find and load "WEB-INF/messages.xml", "WEB-INF/messages_en.xml", etc as well.

      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.util.ResourceBundle

    - #### public void setCacheSeconds(int cacheSeconds)

      Set the number of seconds to cache the list of matching properties files.

      - Default is "-1", indicating to cache forever (just like `java.util.ResourceBundle`).
      - A positive number will cache the list of matching properties files for the given number of seconds. This is essentially the interval between refresh checks.
      - A value of "0" will attemp to list the matching properties files on every message access. Do not use this in a production environment!

    - #### 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 the `java.util.Properties` default encoding: ISO-8859-1.

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

      Parameters:
        `defaultEncoding` - the default charset
      See Also:
        setFileEncodings
        org.springframework.util.PropertiesPersister#load

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

    - #### public void setFileCacheSeconds(int fileCacheSeconds)

      Set the number of seconds to cache loaded properties files.

      - Default value is the same value as cacheSeconds
      - 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!

    - #### public void setFileEncodings(java.util.Properties fileEncodings)

      Set per-file charsets to use for parsing properties files.

      Only applies to classic properties files, not to XML 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
        org.springframework.util.PropertiesPersister#load

    - #### public void setPropertiesPersister(org.springframework.util.PropertiesPersister propertiesPersister)

      Set the PropertiesPersister to use for parsing properties files.

      The default is a DefaultPropertiesPersister.

      See Also:
        org.springframework.util.DefaultPropertiesPersister

    - #### public void setResourceLoader(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 of an ApplicationContext.

      See Also:
        org.springframework.core.io.DefaultResourceLoader
        org.springframework.context.ResourceLoaderAware

    - #### @java.lang.Override public java.lang.String toString()