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:
- for modular applications:
In Jigsaw, ResourceBundles are only visible from their own module by default. Furthermore,ResourceBundle.Controls andResourceBundleControlProviders 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'sServiceFactoryandModuleSorter, the BundleFactory knows which module a resource belongs to and loads it via theModuleHookof that module. - for non-modular applications:
The bundle factory uses the same interfaceResourceBundleControlProviderand semantics to look up and install providers as the JDK, but locates the providers via theServiceFinder. The controls are applied to all bundles that are not loaded via aModuleHook.
Seeorg.tentackle.i18n.StoredBundleControlProviderin tentackle-i18n for an example. -
The classes providing bundles can be annotated with @
Bundleand will automatically become registered resources inMETA-INF/bundlesby means of the analyze goal of the tentackle-maven-plugin. The same applies to some framework-related annotations such as @FxControllerService or @GuiProviderService. Thebundlesgoal 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 Summary
Modifier and TypeMethodDescriptionvoidClears the bundle cache.
The default implementation clears theResourceBundlecache for the classloader that loaded the factory.findBundle(String baseName, Locale locale) Finds a resource bundle using the specified base name and locale.findBundle(String baseName, Locale locale, ClassLoader loader) Finds a resource bundle using the specified base name, locale, and class loader.
Same asfindBundle(String, Locale), but uses the given classloader for bundles not registered for a named module.static ResourceBundleGets a resource bundle using the specified base name and the default locale.
Replacement forResourceBundle.getBundle(java.lang.String).static ResourceBundleGets a resource bundle using the specified base name and locale.
Replacement forResourceBundle.getBundle(java.lang.String, java.util.Locale).static ResourceBundlegetBundle(String baseName, Locale locale, ClassLoader loader) Gets a resource bundle using the specified base name, locale, and class loader.
Replacement forResourceBundle.getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader).Gets the classloader to load the bundles.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.static BundleFactoryThe singleton.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.voidsetClassLoader(ClassLoader classLoader) Sets the classloader to load the bundles.
If set, this classloader will be used forgetBundle(java.lang.String)andgetBundle(java.lang.String, java.util.Locale).
-
Method Details
-
getInstance
-
getBundle
Gets a resource bundle using the specified base name and the default locale.
Replacement forResourceBundle.getBundle(java.lang.String). SeefindBundle(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
Gets a resource bundle using the specified base name and locale.
Replacement forResourceBundle.getBundle(java.lang.String, java.util.Locale). SeefindBundle(String, Locale)for how the bundle is located.- Parameters:
baseName- the base name of the resource bundle, a fully qualified class namelocale- the locale for which a resource bundle is desired- Returns:
- a resource bundle for the given base name and locale
-
getBundle
Gets a resource bundle using the specified base name, locale, and class loader.
Replacement forResourceBundle.getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader).- Parameters:
baseName- the base name of the resource bundle, a fully qualified class namelocale- the locale for which a resource bundle is desiredloader- 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
Sets the classloader to load the bundles.
If set, this classloader will be used forgetBundle(java.lang.String)andgetBundle(java.lang.String, java.util.Locale).- Parameters:
classLoader- the classloader, null if default
-
getProviders
List<ResourceBundleControlProvider> 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
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
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 theModuleHookof that module. Otherwise, it is loaded viaResourceBundle.getBundle(String)with the classloader set bysetClassLoader(ClassLoader), if any, and the control ofgetControl(String), if any.- Parameters:
baseName- the base name of the resource bundle, a fully qualified class namelocale- the locale for which a resource bundle is desired- Returns:
- a resource bundle for the given base name and locale
-
findBundle
Finds a resource bundle using the specified base name, locale, and class loader.
Same asfindBundle(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 namelocale- the locale for which a resource bundle is desiredloader- 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 theResourceBundlecache for the classloader that loaded the factory.
-