Interface BundleFactory

All Known Implementing Classes:
DefaultBundleFactory

public interface BundleFactory
The resource bundle factory.

Tentackle applications should use BundleFactory.getBundle(...) instead of ResourceBundle.getBundle(...) for 3 reasons:

  1. for modular applications:
    In Jigsaw, ResourceBundles are only visible from their own module by default. Furthermore, ResourceBundle.Controls and ResourceBundleControlProviders are not supported for named modules. Loading a bundle from another module, as is the case for a factory located in a framework module, would not work at all. The JDK provides a workaround via a service provider interface and corresponding uses/provides entries in module-info. Alternatively, the modules can be declared as "open". Either approach is rather cumbersome and may lead to runtime errors or loss of encapsulation.
    Thanks to Tentackle's ServiceFactory and ModuleSorter, the BundleFactory knows which module a resource belongs to and loads it via the ModuleHook of that module.
  2. for non-modular applications:
    The bundle factory uses the same interface ResourceBundleControlProvider and semantics to look up and install providers as the JDK, but locates the providers via the ServiceFinder. The controls are applied to all bundles that are not loaded via a ModuleHook.
    See org.tentackle.i18n.StoredBundleControlProvider in tentackle-i18n for an example.
  3. The classes providing bundles can be annotated with @Bundle and will automatically become registered resources in META-INF/bundles by means of the analyze goal of the tentackle-maven-plugin. The same applies to some framework-related annotations such as @FxControllerService or @GuiProviderService. The bundles goal of the tentackle-check-maven-plugin can then check whether the keys used in the Java source are really defined in a resource bundle.

Leading slashes are removed from the base names, i.e., "/org/example/MyBundle" and "org/example/MyBundle" denote the same bundle.

Author:
harald
  • Method Details

    • getInstance

      static BundleFactory getInstance()
      The singleton.
      Returns:
      the singleton
    • getBundle

      static ResourceBundle getBundle(String baseName)
      Gets a resource bundle using the specified base name and the default locale.
      Replacement for ResourceBundle.getBundle(java.lang.String). See findBundle(String, Locale) for how the bundle is located.
      Parameters:
      baseName - the base name of the resource bundle, a fully qualified class name
      Returns:
      a resource bundle for the given base name and the default locale
    • getBundle

      static ResourceBundle getBundle(String baseName, Locale locale)
      Gets a resource bundle using the specified base name and locale.
      Replacement for ResourceBundle.getBundle(java.lang.String, java.util.Locale). See findBundle(String, Locale) for how the bundle is located.
      Parameters:
      baseName - the base name of the resource bundle, a fully qualified class name
      locale - the locale for which a resource bundle is desired
      Returns:
      a resource bundle for the given base name and locale
    • getBundle

      static ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader)
      Gets a resource bundle using the specified base name, locale, and class loader.
      Replacement for ResourceBundle.getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader).
      Parameters:
      baseName - the base name of the resource bundle, a fully qualified class name
      locale - the locale for which a resource bundle is desired
      loader - the class loader from which to load the resource bundle
      Returns:
      a resource bundle for the given base name and locale
    • getClassLoader

      ClassLoader getClassLoader()
      Gets the classloader to load the bundles.
      Returns:
      the classloader, null if default
    • setClassLoader

      void setClassLoader(ClassLoader classLoader)
      Sets the classloader to load the bundles.
      If set, this classloader will be used for getBundle(java.lang.String) and getBundle(java.lang.String, java.util.Locale).
      Parameters:
      classLoader - the classloader, null if default
    • getProviders

      Gets the control providers.
      The returned list can be modified to add a provider explicitly, but only if at least one provider has been found.
      Returns:
      the non-empty list of providers or null, if no providers found
    • getControl

      ResourceBundle.Control getControl(String baseName)
      Gets the control to load the bundle.
      The control is determined by the first provider returning a non-null control for the given basename.
      Parameters:
      baseName - the bundle basename
      Returns:
      the control, null if delegate to ResourceBundle's default
    • findBundle

      ResourceBundle findBundle(String baseName, Locale locale)
      Finds a resource bundle using the specified base name and locale.

      If the bundle is registered for a named module (see Bundle), it is loaded via the ModuleHook of that module. Otherwise, it is loaded via ResourceBundle.getBundle(String) with the classloader set by setClassLoader(ClassLoader), if any, and the control of getControl(String), if any.

      Parameters:
      baseName - the base name of the resource bundle, a fully qualified class name
      locale - the locale for which a resource bundle is desired
      Returns:
      a resource bundle for the given base name and locale
    • findBundle

      ResourceBundle findBundle(String baseName, Locale locale, ClassLoader loader)
      Finds a resource bundle using the specified base name, locale, and class loader.
      Same as findBundle(String, Locale), but uses the given classloader for bundles not registered for a named module.
      Parameters:
      baseName - the base name of the resource bundle, a fully qualified class name
      locale - the locale for which a resource bundle is desired
      loader - the class loader from which to load the resource bundle
      Returns:
      a resource bundle for the given base name and locale
    • clearCache

      void clearCache()
      Clears the bundle cache.
      The default implementation clears the ResourceBundle cache for the classloader that loaded the factory.