java.lang.Objectjava.util.ResourceBundle$Control
Direct Known Subclasses:
SingleFormatControl, NoFallbackControl
ResourceBundle.Control defines a set of callback methods
that are invoked by the ResourceBundle.getBundle factory
methods during the bundle loading process. In other words, a
ResourceBundle.Control collaborates with the factory
methods for loading resource bundles. The default implementation of
the callback methods provides the information necessary for the
factory methods to perform the default behavior.
In addition to the callback methods, the toBundleName and toResourceName methods are defined
primarily for convenience in implementing the callback
methods. However, the toBundleName method could be
overridden to provide different conventions in the organization and
packaging of localized resources. The toResourceName
method is final to avoid use of wrong resource and class
name separators.
Two factory methods, #getControl(List) and #getNoFallbackControl(List) , provide
ResourceBundle.Control instances that implement common
variations of the default bundle loading process.
The formats returned by the getFormats method and candidate locales returned by the Locale)
getCandidateLocales method must be consistent in all
ResourceBundle.getBundle invocations for the same base
bundle. Otherwise, the ResourceBundle.getBundle methods
may return unintended bundles. For example, if only
"java.class" is returned by the getFormats
method for the first call to ResourceBundle.getBundle
and only "java.properties" for the second call, then the
second call will return the class-based one that has been cached
during the first call.
A ResourceBundle.Control instance must be thread-safe
if it's simultaneously used by multiple threads.
ResourceBundle.getBundle does not synchronize to call
the ResourceBundle.Control methods. The default
implementations of the methods are thread-safe.
Applications can specify ResourceBundle.Control
instances returned by the getControl factory methods or
created from a subclass of ResourceBundle.Control to
customize the bundle loading process. The following are examples of
changing the default bundle loading process.
Example 1
The following code lets ResourceBundle.getBundle look
up only properties-based resources.
import java.util.*;
import static java.util.ResourceBundle.Control.*;
...
ResourceBundle bundle =
ResourceBundle.getBundle("MyResources", new Locale("fr", "CH"),
ResourceBundle.Control.getControl(FORMAT_PROPERTIES));
Given the resource bundles in the example in
the ResourceBundle.getBundle description, this
ResourceBundle.getBundle call loads
MyResources_fr_CH.properties whose parent is
MyResources_fr.properties whose parent is
MyResources.properties. (MyResources_fr_CH.properties
is not hidden, but MyResources_fr_CH.class is.)
Example 2
The following is an example of loading XML-based bundles using Properties.loadFromXML .
ResourceBundle rb = ResourceBundle.getBundle("Messages",
new ResourceBundle.Control() {
public List<String> getFormats(String baseName) {
if (baseName == null)
throw new NullPointerException();
return Arrays.asList("xml");
}
public ResourceBundle newBundle(String baseName,
Locale locale,
String format,
ClassLoader loader,
boolean reload)
throws IllegalAccessException,
InstantiationException,
IOException {
if (baseName == null || locale == null
|| format == null || loader == null)
throw new NullPointerException();
ResourceBundle bundle = null;
if (format.equals("xml")) {
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, format);
InputStream stream = null;
if (reload) {
URL url = loader.getResource(resourceName);
if (url != null) {
URLConnection connection = url.openConnection();
if (connection != null) {
// Disable caches to get fresh data for
// reloading.
connection.setUseCaches(false);
stream = connection.getInputStream();
}
}
} else {
stream = loader.getResourceAsStream(resourceName);
}
if (stream != null) {
BufferedInputStream bis = new BufferedInputStream(stream);
bundle = new XMLResourceBundle(bis);
bis.close();
}
}
return bundle;
}
});
...
private static class XMLResourceBundle extends ResourceBundle {
private Properties props;
XMLResourceBundle(InputStream stream) throws IOException {
props = new Properties();
props.loadFromXML(stream);
}
protected Object handleGetObject(String key) {
return props.getProperty(key);
}
public Enumeration<String> getKeys() {
...
}
}
1.6 - | Field Summary | ||
|---|---|---|
| public static final List | FORMAT_DEFAULT | The default format List, which contains the strings
"java.class" and "java.properties", in
this order. This List is {@linkplain
Collections#unmodifiableList(List) unmodifiable}.
|
| public static final List | FORMAT_CLASS | The class-only format List containing
"java.class". This List is {@linkplain
Collections#unmodifiableList(List) unmodifiable}.
|
| public static final List | FORMAT_PROPERTIES | The properties-only format List containing
"java.properties". This List is
{@linkplain Collections#unmodifiableList(List) unmodifiable}.
|
| public static final long | TTL_DONT_CACHE | The time-to-live constant for not caching loaded resource bundle
instances.
|
| public static final long | TTL_NO_EXPIRATION_CONTROL | The time-to-live constant for disabling the expiration control
for loaded resource bundle instances in the cache.
|
| Constructor: |
|---|
|
| Method from java.util.ResourceBundle$Control Summary: |
|---|
| getCandidateLocales, getControl, getFallbackLocale, getFormats, getNoFallbackControl, getTimeToLive, needsReload, newBundle, toBundleName, toResourceName |
| Methods from java.lang.Object: |
|---|
| clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from java.util.ResourceBundle$Control Detail: |
|---|
List of Locales as candidate
locales for baseName and locale. This
method is called by the ResourceBundle.getBundle
factory method each time the factory method tries finding a
resource bundle for a target Locale.
The sequence of the candidate locales also corresponds to the runtime resource lookup path (also known as the parent chain), if the corresponding resource bundles for the candidate locales exist and their parents are not defined by loaded resource bundles themselves. The last element of the list must be a {@linkplain Locale#ROOT root locale} if it is desired to have the base bundle as the terminal of the parent chain. If the given locale is equal to It is not a requirement to return an immutable
(unmodifiable) The default implementation returns a Locale(language, country, variant) Locale(language, country) Locale(language) Locale.ROOTwhere language, country and
variant are the language, country and variant values
of the given locale, respectively. Locales where the
final component values are empty strings are omitted.
The default implementation uses an ArrayList that
overriding implementations may modify before returning it to the
caller. However, a subclass must not modify it after it has
been returned by For example, if the given
Locale("ja", "", "XX")
Locale("ja")
Locale.ROOT
is returned. And if the resource bundles for the "ja" and
"" Locales are found, then the runtime resource
lookup path (parent chain) is:
Messages_ja -> Messages |
ResourceBundle.Control in which the getFormats method returns the specified
formats. The formats must be equal to
one of Control#FORMAT_PROPERTIES , Control#FORMAT_CLASS or Control#FORMAT_DEFAULT . ResourceBundle.Control
instances returned by this method are singletons and thread-safe.
Specifying Control#FORMAT_DEFAULT is equivalent to
instantiating the |
Locale to be used as a fallback locale for
further resource bundle searches by the
ResourceBundle.getBundle factory method. This method
is called from the factory method every time when no resulting
resource bundle has been found for baseName and
locale, where locale is either the parameter for
ResourceBundle.getBundle or the previous fallback
locale returned by this method.
The method returns The default implementation returns the {@linkplain
Locale#getDefault() default |
List of Strings containing
formats to be used to load resource bundles for the given
baseName. The ResourceBundle.getBundle
factory method tries to load resource bundles with formats in the
order specified by the list. The list returned by this method
must have at least one String. The predefined
formats are "java.class" for class-based resource
bundles and "java.properties" for {@linkplain
PropertyResourceBundle properties-based} ones. Strings starting
with "java." are reserved for future extensions and
must not be used by application-defined formats.
It is not a requirement to return an immutable (unmodifiable)
The default implementation returns #FORMAT_DEFAULT so
that the |
ResourceBundle.Control in which the getFormats method returns the specified
formats and the getFallbackLocale
method returns null. The formats must
be equal to one of Control#FORMAT_PROPERTIES , Control#FORMAT_CLASS or Control#FORMAT_DEFAULT .
ResourceBundle.Control instances returned by this
method are singletons and thread-safe. |
ResourceBundle.Control. Positive time-to-live values
specify the number of milliseconds a bundle can remain in the
cache without being validated against the source data from which
it was constructed. The value 0 indicates that a bundle must be
validated each time it is retrieved from the cache. #TTL_DONT_CACHE specifies that loaded resource bundles are not
put in the cache. #TTL_NO_EXPIRATION_CONTROL specifies
that loaded resource bundles are put in the cache with no
expiration control.
The expiration affects only the bundle loading process by the
All cached resource bundles are subject to removal from the cache due to memory constraints of the runtime environment. Returning a large positive value doesn't mean to lock loaded resource bundles in the cache. The default implementation returns #TTL_NO_EXPIRATION_CONTROL . |
bundle in the cache needs
to be reloaded based on the loading time given by
loadTime or some other criteria. The method returns
true if reloading is required; false
otherwise. loadTime is a millisecond offset since
the Calendar
Epoch.
The calling ResourceBundle.getBundle factory method
calls this method on the ResourceBundle.Control
instance used for its current invocation, not on the instance
used in the invocation that originally loaded the resource
bundle.
The default implementation compares |
null if there is no
resource bundle available for the given parameters. If a resource
bundle can't be instantiated due to an unexpected error, the
error must be reported by throwing an Error or
Exception rather than simply returning
null.
If the The default implementation instantiates a
|
baseName and locale
to the bundle name. This method is called from the default
implementation of the newBundle and needsReload
methods.
This implementation returns the following value: baseName + "_" + language + "_" + country + "_" + variantwhere language, country and
variant are the language, country and variant values
of locale, respectively. Final component values that
are empty Strings are omitted along with the preceding '_'. If
all of the values are empty strings, then baseName
is returned.
For example, if Overriding this method allows applications to use different conventions in the organization and packaging of localized resources. |
bundleName to the form required
by the ClassLoader.getResource
method by replacing all occurrences of '.' in
bundleName with '/' and appending a
'.' and the given file suffix. For
example, if bundleName is
"foo.bar.MyResources_ja_JP" and suffix
is "properties", then
"foo/bar/MyResources_ja_JP.properties" is returned. |