Class StoredBundleControl

java.lang.Object
java.util.ResourceBundle.Control
org.tentackle.i18n.StoredBundleControl
All Implemented Interfaces:
DomainContextProvider

public class StoredBundleControl extends ResourceBundle.Control implements DomainContextProvider
Bundle control that loads resource bundles from the database.

newBundle(String, Locale, String, ClassLoader, boolean) loads the StoredBundle for the requested base name and locale and wraps it in a StoredResourceBundle. If nothing is stored, it falls back to the property file, unless fallbackToProperties is turned off. A load that fails, because the backend is unreachable, for example, falls back to the property file as well, but the result is not cached (see getTimeToLive(String, Locale)).
As long as there is no current Session or loading from the backend is disabled, the control behaves like the standard ResourceBundle.Control.

The control is handed out by the StoredBundleControlProvider in non-modular applications. StoredBundleFactory does not use it as a control, but reuses createUDK(String, Locale) and loadStoredBundle(StoredBundle.StoredBundleUDK) to load the bundles of named modules.

Author:
harald
  • Constructor Details

    • StoredBundleControl

      public StoredBundleControl()
      Creates a stored bundle control object.
  • Method Details

    • isFallbackToProperties

      public static boolean isFallbackToProperties()
      Returns whether the property files are used if a bundle or key is not stored in the database.

      Applies to the bundles loaded by this control as well as to those loaded by StoredBundleFactory.

      Returns:
      true if falling back to the property files (default)
    • setFallbackToProperties

      public static void setFallbackToProperties(boolean fallbackToProperties)
      Sets whether the property files are used if a bundle or key is not stored in the database.
      Parameters:
      fallbackToProperties - true to fall back to the property files, false to throw a MissingResourceException instead
    • getDomainContext

      public DomainContext getDomainContext()
      Description copied from interface: DomainContextProvider
      Gets the domain context.
      Specified by:
      getDomainContext in interface DomainContextProvider
      Returns:
      the domain context
    • getFormats

      public List<String> getFormats(String baseName)
      Overrides:
      getFormats in class ResourceBundle.Control
    • getTimeToLive

      public long getTimeToLive(String baseName, Locale locale)
      Overrides:
      getTimeToLive in class ResourceBundle.Control
    • newBundle

      public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException
      Overrides:
      newBundle in class ResourceBundle.Control
      Throws:
      IllegalAccessException
      InstantiationException
      IOException
    • isBackendLoadable

      protected boolean isBackendLoadable()
      Returns whether bundles can be loaded from the backend.

      Requires a current session and StoredBundleFactory.isEnabled(). The latter must be checked here as well, since in non-modular applications, bundles are loaded through this control and never reach the factory's own check.

      Returns:
      true if loading from the backend is possible
    • createUDK

      protected StoredBundle.StoredBundleUDK createUDK(String baseName, Locale locale)
      Creates the unique domain key to load a stored bundle.

      The locale suffix is derived from the locale, not by searching the bundle name for an underscore: base names may contain underscores themselves, and org.myapp.Texts_v2 must not be read as the bundle org.myapp.Texts for the locale v2.

      Parameters:
      baseName - the base bundle name of the resource bundle
      locale - the locale
      Returns:
      the unique domain key
    • loadStoredBundle

      protected StoredResourceBundle loadStoredBundle(StoredBundle.StoredBundleUDK key)
      Loads the bundle from storage.

      A failure the backend reports as temporary (the modification tracker is not running yet, for example) is treated like a missing bundle, any other exception is thrown.

      Parameters:
      key - the unique domain key
      Returns:
      the stored resource bundle or null if no such bundle in the database