- 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, 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'sServiceFactory
andModuleSorter
, the BundleFactory knows to which module a resource belongs to and loads it from the enclosing module. - for non-modular applications:
because the use cases ofResourceBundleControlProvider
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 interfaceResourceBundleControlProvider
and semantics to lookup and install providers as the standard JRE, but scans the application classpath instead of the system classpath.
Seeorg.tentackle.locale.StoredBundleControlProvider
in tentackle-i18n for an example. -
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
Modifier and TypeMethodDescriptionvoid
Clears the bundle cache.findBundle
(String baseName, Locale locale) Finds a resource bundle using the specified base name and locale, and the caller's class loader.findBundle
(String baseName, Locale locale, ClassLoader loader) Finds a resource bundle using the specified base name, locale, and class loader.static ResourceBundle
Gets a resource bundle using the specified base name, the default locale, and the caller's class loader.
Replacement forResourceBundle.getBundle(java.lang.String)
.static ResourceBundle
Gets a resource bundle using the specified base name and locale, and the caller's class loader.
Replacement forResourceBundle.getBundle(java.lang.String, java.util.Locale)
.static ResourceBundle
getBundle
(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.static BundleFactory
The singleton.Gets the control providers.
Method can be used to add a provider explicitly.void
setClassLoader
(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
The singleton.- Returns:
- the singleton
-
getBundle
Gets a resource bundle using the specified base name, the default locale, and the caller's class loader.
Replacement forResourceBundle.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
Gets a resource bundle using the specified base name and locale, and the caller's class loader.
Replacement forResourceBundle.getBundle(java.lang.String, java.util.Locale)
.- 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.
Method can be used to add a provider explicitly.- Returns:
- the non-empty list of providers or null, if no providers found
-
getControl
Gets the control to load the bundle.- 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, and the caller's class loader.- 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.- 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.
-