APIdia Launch Partner
Top Description Fields Constructors Methods
java.util.spi

public abstract Class AbstractResourceBundleProvider

extends Object
implements ResourceBundleProvider
Class Inheritance
All Implemented Interfaces
java.util.spi.ResourceBundleProvider
Imports
jdk.internal.access.JavaUtilResourceBundleAccess, .SharedSecrets, sun.util.resources.Bundles, java.io.IOException, .InputStream, .UncheckedIOException, java.util.Locale, .PropertyResourceBundle, .ResourceBundle

AbstractResourceBundleProvider is an abstract class that provides the basic support for a provider implementation class for ResourceBundleProvider.

Resource bundles can be packaged in one or more named modules, service provider 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.spi.MyResourcesProvider" must be defined and a service provider module will provide an implementation class of "com.example.app.spi.MyResourcesProvider" as follows:

import com.example.app.spi.MyResourcesProvider;
class MyResourcesProviderImpl extends AbstractResourceBundleProvider
    implements MyResourcesProvider
{
    public MyResourcesProviderImpl() {
        super("java.properties");
    }
    // this provider maps the resource bundle to per-language package
    protected String toBundleName(String baseName, Locale locale) {
        return "p." + locale.getLanguage() + "." + baseName;
    }

    public ResourceBundle getBundle(String baseName, Locale locale) {
        // this module only provides bundles in French
        if (locale.equals(Locale.FRENCH)) {
             return super.getBundle(baseName, locale);
        }
        // otherwise return null
        return null;
    }
}

Refer to ResourceBundleProvider for details.

Since
9
See Also
Resource Bundles and Named Modules

Field Summary

Modifier and TypeField and Description
private static final String
private static final String
private final String[]
private static final JavaUtilResourceBundleAccess

Constructor Summary

AccessConstructor and Description
protected
AbstractResourceBundleProvider()

Constructs an AbstractResourceBundleProvider with the "java.properties" format.

protected
AbstractResourceBundleProvider(String...
the formats to be used for loading resource bundles
formats
)

Constructs an AbstractResourceBundleProvider with the specified formats.

Method Summary

Modifier and TypeMethod and Description
public ResourceBundle

Returns:

ResourceBundle of the given baseName and locale, or null if no resource bundle is found
getBundle
(String
the base bundle name of the resource bundle, a fully qualified class name.
baseName
,
Locale
the locale for which the resource bundle should be instantiated
locale
)

Implements java.util.spi.ResourceBundleProvider.getBundle.

Returns a ResourceBundle for the given baseName and locale.

private ResourceBundle
getBundle0(Module module, String bundleName)

private static ResourceBundle
private static ResourceBundle
loadResourceBundle(Module module, String bundleName)

protected String

Returns:

the bundle name for the resource bundle
toBundleName
(String
the base name of the resource bundle, a fully qualified class name
baseName
,
Locale
the locale for which a resource bundle should be loaded
locale
)

Returns the bundle name for the given baseName and locale that this provider provides.

private static String
toResourceName(String bundleName, String suffix)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

FORMAT_CLASSback to summary
private static final String FORMAT_CLASS
FORMAT_PROPERTIESback to summary
private static final String FORMAT_PROPERTIES
formatsback to summary
private final String[] formats
RB_ACCESSback to summary
private static final JavaUtilResourceBundleAccess RB_ACCESS

Constructor Detail

AbstractResourceBundleProviderback to summary
protected AbstractResourceBundleProvider()

Constructs an AbstractResourceBundleProvider with the "java.properties" format. This constructor is equivalent to AbstractResourceBundleProvider("java.properties").

AbstractResourceBundleProviderback to summary
protected AbstractResourceBundleProvider(String... formats)

Constructs an AbstractResourceBundleProvider with the specified formats. The getBundle(String, Locale) method looks up resource bundles for the given formats. formats must be "java.class" or "java.properties".

Parameters
formats:String[]

the formats to be used for loading resource bundles

Exceptions
NullPointerException:
if the given formats is null
IllegalArgumentException:
if the given formats is not "java.class" or "java.properties".

Method Detail

getBundleback to summary
public ResourceBundle getBundle(String baseName, Locale locale)

Implements java.util.spi.ResourceBundleProvider.getBundle.

Returns a ResourceBundle for the given baseName and locale.

Implementation Note

The default implementation of this method calls the toBundleName method to get the bundle name for the baseName and locale 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:String

the base bundle name of the resource bundle, a fully qualified class name.

locale:Locale

the locale for which the resource bundle should be instantiated

Returns:ResourceBundle

ResourceBundle of the given baseName and locale, or null if no resource bundle is found

Annotations
@Override
Exceptions
NullPointerException:
if baseName or locale is null
UncheckedIOException:
if any IO exception occurred during resource bundle loading
getBundle0back to summary
private ResourceBundle getBundle0(Module module, String bundleName)
loadPropertyResourceBundleback to summary
private static ResourceBundle loadPropertyResourceBundle(Module module, String bundleName) throws IOException
loadResourceBundleback to summary
private static ResourceBundle loadResourceBundle(Module module, String bundleName)
toBundleNameback to summary
protected String toBundleName(String baseName, Locale locale)

Returns the bundle name for the given baseName and locale 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" of Locale("ja", "", "XX") and Locale("en") could be "p.resources.ja.Bundle_ja_ _XX" and "p.resources.Bundle_en" respectively.

This 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 java.util.ResourceBundle.Control#toBundleName(String, Locale).

Parameters
baseName:String

the base name of the resource bundle, a fully qualified class name

locale:Locale

the locale for which a resource bundle should be loaded

Returns:String

the bundle name for the resource bundle

toResourceNameback to summary
private static String toResourceName(String bundleName, String suffix)