--- a/jdk/src/java.base/share/classes/java/util/ResourceBundle.java Mon Jan 30 11:00:18 2017 -0800
+++ b/jdk/src/java.base/share/classes/java/util/ResourceBundle.java Mon Jan 30 14:38:08 2017 -0800
@@ -61,7 +61,10 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.jar.JarEntry;
+import java.util.spi.ResourceBundleControlProvider;
import java.util.spi.ResourceBundleProvider;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
import jdk.internal.loader.BootLoader;
import jdk.internal.misc.JavaUtilResourceBundleAccess;
@@ -232,6 +235,8 @@
* <li>{@code ResourceBundle.Control} is <em>not</em> supported in named modules.
* If the {@code getBundle} method with a {@code ResourceBundle.Control} is called
* in a named module, the method will throw an {@code UnsupportedOperationException}.
+ * Any service providers of {@link ResourceBundleControlProvider} are ignored in
+ * named modules.
* </li>
* </ul>
*
@@ -262,6 +267,18 @@
* {@link #getBundle(String, Locale, ClassLoader, Control) getBundle}
* factory method for details.
*
+ * <p><a name="modify_default_behavior">For the {@code getBundle} factory</a>
+ * methods that take no {@link Control} instance, their <a
+ * href="#default_behavior"> default behavior</a> of resource bundle loading
+ * can be modified with custom {@link
+ * ResourceBundleControlProvider} implementations.
+ * If any of the
+ * providers provides a {@link Control} for the given base name, that {@link
+ * Control} will be used instead of the default {@link Control}. If there is
+ * more than one service provider for supporting the same base name,
+ * the first one returned from {@link ServiceLoader} will be used.
+ * A custom {@link Control} implementation is ignored by named modules.
+ *
* <h3>Cache Management</h3>
*
* Resource bundle instances created by the <code>getBundle</code> factory
@@ -367,7 +384,8 @@
public ResourceBundle getBundle(String baseName, Locale locale, Module module) {
// use the given module as the caller to bypass the access check
return getBundleImpl(module, module,
- baseName, locale, Control.INSTANCE);
+ baseName, locale,
+ getDefaultControl(module, baseName));
}
@Override
@@ -815,7 +833,7 @@
{
Class<?> caller = Reflection.getCallerClass();
return getBundleImpl(baseName, Locale.getDefault(),
- caller, Control.INSTANCE);
+ caller, getDefaultControl(caller, baseName));
}
/**
@@ -889,7 +907,7 @@
{
Class<?> caller = Reflection.getCallerClass();
return getBundleImpl(baseName, locale,
- caller, Control.INSTANCE);
+ caller, getDefaultControl(caller, baseName));
}
/**
@@ -925,7 +943,8 @@
@CallerSensitive
public static ResourceBundle getBundle(String baseName, Module module) {
return getBundleFromModule(Reflection.getCallerClass(), module, baseName,
- Locale.getDefault(), Control.INSTANCE);
+ Locale.getDefault(),
+ getDefaultControl(module, baseName));
}
/**
@@ -953,7 +972,9 @@
* equivalent to calling {@link #getBundle(String, Locale, ClassLoader)
* getBundle(baseName, targetLocale, module.getClassLoader()} to load
* resource bundles that are visible to the class loader of the given
- * unnamed module.
+ * unnamed module. Custom {@link java.util.spi.ResourceBundleControlProvider}
+ * implementations, if present, will only be invoked if the specified
+ * module is an unnamed module.
*
* @param baseName the base name of the resource bundle,
* a fully qualified class name
@@ -974,7 +995,7 @@
@CallerSensitive
public static ResourceBundle getBundle(String baseName, Locale targetLocale, Module module) {
return getBundleFromModule(Reflection.getCallerClass(), module, baseName, targetLocale,
- Control.INSTANCE);
+ getDefaultControl(module, baseName));
}
/**
@@ -1030,7 +1051,10 @@
*
* <p>This method behaves the same as calling
* {@link #getBundle(String, Locale, ClassLoader, Control)} passing a
- * default instance of {@link Control}.
+ * default instance of {@link Control} unless another {@link Control} is
+ * provided with the {@link ResourceBundleControlProvider} SPI. Refer to the
+ * description of <a href="#modify_default_behavior">modifying the default
+ * behavior</a>.
*
* <p><a name="default_behavior">The following describes the default
* behavior</a>.
@@ -1228,7 +1252,7 @@
throw new NullPointerException();
}
Class<?> caller = Reflection.getCallerClass();
- return getBundleImpl(baseName, locale, caller, loader, Control.INSTANCE);
+ return getBundleImpl(baseName, locale, caller, loader, getDefaultControl(caller, baseName));
}
/**
@@ -1453,6 +1477,39 @@
return getBundleImpl(baseName, targetLocale, caller, loader, control);
}
+ private static Control getDefaultControl(Class<?> caller, String baseName) {
+ return getDefaultControl(caller.getModule(), baseName);
+ }
+
+ private static Control getDefaultControl(Module targetModule, String baseName) {
+ return targetModule.isNamed() ?
+ Control.INSTANCE :
+ ResourceBundleControlProviderHolder.getControl(baseName);
+ }
+
+ private static class ResourceBundleControlProviderHolder {
+ private static final PrivilegedAction<List<ResourceBundleControlProvider>> pa =
+ () -> {
+ return Collections.unmodifiableList(
+ ServiceLoader.load(ResourceBundleControlProvider.class,
+ ClassLoader.getSystemClassLoader()).stream()
+ .map(ServiceLoader.Provider::get)
+ .collect(Collectors.toList()));
+ };
+
+ private static final List<ResourceBundleControlProvider> CONTROL_PROVIDERS =
+ AccessController.doPrivileged(pa);
+
+ private static Control getControl(String baseName) {
+ return CONTROL_PROVIDERS.isEmpty() ?
+ Control.INSTANCE :
+ CONTROL_PROVIDERS.stream()
+ .flatMap(provider -> Stream.ofNullable(provider.getControl(baseName)))
+ .findFirst()
+ .orElse(Control.INSTANCE);
+ }
+ }
+
private static void checkNamedModule(Class<?> caller) {
if (caller.getModule().isNamed()) {
throw new UnsupportedOperationException(
@@ -2414,7 +2471,8 @@
* @apiNote <a name="note">{@code ResourceBundle.Control} is not supported
* in named modules.</a> If the {@code ResourceBundle.getBundle} method with
* a {@code ResourceBundle.Control} is called in a named module, the method
- * will throw an {@link UnsupportedOperationException}.
+ * will throw an {@link UnsupportedOperationException}. Any service providers
+ * of {@link ResourceBundleControlProvider} are ignored in named modules.
*
* @since 1.6
* @see java.util.spi.ResourceBundleProvider