- java.lang.Object
-
- java.util.spi.AbstractResourceBundleProvider
-
- All Implemented Interfaces:
ResourceBundleProvider
public abstract class AbstractResourceBundleProvider extends Object implements ResourceBundleProvider
AbstractResourceBundleProvider
is an abstract class that provides the basic support for a provider implementation class forResourceBundleProvider
.Resource bundles can be packaged in one or more named modules, bundle modules. The consumer of the resource bundle is the one calling
ResourceBundle.getBundle(String)
. In order for the consumer module to load a resource bundle "com.example.app.MyResources
" provided by another module, it will use the service loader mechanism. A service interface named "com.example.app.MyResourcesProvider
" must be defined and a bundle provider module will provide an implementation class of "com.example.app.MyResourcesProvider
" as follows:import com.example.app.MyResourcesProvider; class MyResourcesProviderImpl extends AbstractResourceBundleProvider implements MyResourcesProvider { protected String toBundleName(String baseName, Locale locale) { // return the bundle name per the naming of the resource bundle : } public ResourceBundle getBundle(String baseName, Locale locale) { // this module only provides bundles in french if (locale.equals(Locale.FRENCH)) { return super.getBundle(baseName, locale); } return null; } }
- Since:
- 9
- See Also:
- Resource Bundles in Named Modules, ResourceBundleProvider Service Providers
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractResourceBundleProvider()
Constructs anAbstractResourceBundleProvider
with the "java.properties" format.protected
AbstractResourceBundleProvider(String... formats)
Constructs anAbstractResourceBundleProvider
with the specifiedformats
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ResourceBundle
getBundle(String baseName, Locale locale)
Returns aResourceBundle
for the givenbaseName
andlocale
.protected String
toBundleName(String baseName, Locale locale)
Returns the bundle name for the givenbaseName
andlocale
that this provider provides.
-
-
-
Constructor Detail
-
AbstractResourceBundleProvider
protected AbstractResourceBundleProvider()
Constructs anAbstractResourceBundleProvider
with the "java.properties" format. This constructor is equivalent toAbstractResourceBundleProvider("java.properties")
.
-
AbstractResourceBundleProvider
protected AbstractResourceBundleProvider(String... formats)
Constructs anAbstractResourceBundleProvider
with the specifiedformats
. ThegetBundle(String, Locale)
method looks up resource bundles for the givenformats
.formats
must be "java.class" or "java.properties".- Parameters:
formats
- the formats to be used for loading resource bundles- Throws:
NullPointerException
- if the givenformats
is nullIllegalArgumentException
- if the givenformats
is not "java.class" or "java.properties".
-
-
Method Detail
-
toBundleName
protected String toBundleName(String baseName, Locale locale)
Returns the bundle name for the givenbaseName
andlocale
that this provider provides.- API Note:
- A resource bundle provider may package its resource bundles in the
same package as the base name of the resource bundle if the package
is not split among other named modules. If there are more than one
bundle providers providing the resource bundle of a given base name,
the resource bundles can be packaged with per-language grouping
or per-region grouping to eliminate the split packages.
For example, if
baseName
is"p.resources.Bundle"
then the resource bundle name of"p.resources.Bundle"
ofLocale("ja", "", "XX")
andLocale("en")
could be"p.resources.ja.Bundle_ja_ _XX"
andp.resources.Bundle_en"
respectivelyThis method is called from the default implementation of the
getBundle(String, Locale)
method. - Implementation Note:
- The default implementation of this method is the same as the
implementation of
ResourceBundle.Control.toBundleName(String, Locale)
. - Parameters:
baseName
- the base name of the resource bundle, a fully qualified class namelocale
- the locale for which a resource bundle should be loaded- Returns:
- the bundle name for the resource bundle
-
getBundle
public ResourceBundle getBundle(String baseName, Locale locale)
Returns aResourceBundle
for the givenbaseName
andlocale
.- Specified by:
getBundle
in interfaceResourceBundleProvider
- Implementation Note:
- The default implementation of this method calls the
toBundleName
method to get the bundle name for thebaseName
andlocale
and finds the resource bundle of the bundle name local in the module of this provider. It will only search the formats specified when this provider was constructed. - Parameters:
baseName
- the base bundle name of the resource bundle, a fully qualified class name.locale
- the locale for which the resource bundle should be instantiated- Returns:
ResourceBundle
of the givenbaseName
andlocale
, ornull
if no resource bundle is found- Throws:
NullPointerException
- ifbaseName
orlocale
isnull
UncheckedIOException
- if any IO exception occurred during resource bundle loading
-
-