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 feasable 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 Summary

      All Methods Static Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void clearCache()
      Clears the bundle cache.
      java.util.ResourceBundle findBundle​(java.lang.String baseName, java.util.Locale locale)
      Finds a resource bundle using the specified base name and locale, and the caller's class loader.
      java.util.ResourceBundle findBundle​(java.lang.String baseName, java.util.Locale locale, java.lang.ClassLoader loader)
      Finds a resource bundle using the specified base name, locale, and class loader.
      static java.util.ResourceBundle getBundle​(java.lang.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).
      static java.util.ResourceBundle getBundle​(java.lang.String baseName, java.util.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).
      static java.util.ResourceBundle getBundle​(java.lang.String baseName, java.util.Locale locale, java.lang.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).
      java.lang.ClassLoader getClassLoader()
      Gets the classloader to load the bundles.
      java.util.ResourceBundle.Control getControl​(java.lang.String baseName)
      Gets the control to load the bundle.
      static BundleFactory getInstance()
      The singleton.
      java.util.List<java.util.spi.ResourceBundleControlProvider> getProviders()
      Gets the control providers.
      Method can be used to add a provider explicitly.
      void setClassLoader​(java.lang.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).
    • Method Detail

      • getInstance

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

        static java.util.ResourceBundle getBundle​(java.lang.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 java.util.ResourceBundle getBundle​(java.lang.String baseName,
                                                  java.util.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 java.util.ResourceBundle getBundle​(java.lang.String baseName,
                                                  java.util.Locale locale,
                                                  java.lang.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

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

        java.util.List<java.util.spi.ResourceBundleControlProvider> 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

        java.util.ResourceBundle.Control getControl​(java.lang.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

        java.util.ResourceBundle findBundle​(java.lang.String baseName,
                                            java.util.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

        java.util.ResourceBundle findBundle​(java.lang.String baseName,
                                            java.util.Locale locale,
                                            java.lang.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.