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, ResourceBundleControlProvider is unavailable, i.e. disabled and throws an exception if used. Loading a bundle from another module, as it is the case for a factory located in a framework module, would not work at all. Since Java 9, a workaround is provided 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 to which module a resource belongs to and loads it from the enclosing module.
  2. for non-modular applications:
    because the use cases of ResourceBundleControlProvider are still very limited in Java 8. The major restriction is that the providers are loaded from installed extensions only. This requires either a copy the tentackle-jars in the extensions directory of the JRE installation (which is a bad habit in general, because it applies to all applications) or to set the runtime option -Djava.ext.dirs=... to a directory path containing the jars. The latter may be a workaround for applications started from the shell, but is not feasible for webstart applications or applications running within a container.
    Tentackle's bundle factory uses the same interface ResourceBundleControlProvider and semantics to lookup and install providers as the standard JRE, but scans the application classpath instead of the system classpath.
    See org.tentackle.locale.StoredBundleControlProvider in tentackle-i18n for an example.
  3. The classes providing bundles can be annotated with @Bundle and will automatically become registered resources via META-INF by means of the tentackle:analyze plugin. Same applies to some framework-related annotations auch as @FxControllerService or @GuiProviderService. The tentackle-i18n plugin can then check during the test phase whether the keys used in the java source are really defined in a resource 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, the default locale, and the caller's class loader.
      Replacement for ResourceBundle.getBundle(java.lang.String).
      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, and the caller's class loader.
      Replacement for ResourceBundle.getBundle(java.lang.String, java.util.Locale).
      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.
      Method can be used to add a provider explicitly.
      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.
      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, and the caller's class loader.
      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.
      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.