Skip to content

Commit 5a98516

Browse files
committed
Lenient fallback to plain getBundle call without Control handle
Issue: SPR-16776
1 parent 30363c8 commit 5a98516

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

spring-context/src/main/java/org/springframework/context/support/ResourceBundleMessageSource.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ public class ResourceBundleMessageSource extends AbstractResourceBasedMessageSou
8080
* This allows for very efficient hash lookups, significantly faster
8181
* than the ResourceBundle class's own cache.
8282
*/
83-
private final Map<String, Map<Locale, ResourceBundle>> cachedResourceBundles = new ConcurrentHashMap<>();
83+
private final Map<String, Map<Locale, ResourceBundle>> cachedResourceBundles =
84+
new ConcurrentHashMap<>();
8485

8586
/**
8687
* Cache to hold already generated MessageFormats.
@@ -90,7 +91,11 @@ public class ResourceBundleMessageSource extends AbstractResourceBasedMessageSou
9091
* very efficient hash lookups without concatenated keys.
9192
* @see #getMessageFormat
9293
*/
93-
private final Map<ResourceBundle, Map<String, Map<Locale, MessageFormat>>> cachedBundleMessageFormats = new ConcurrentHashMap<>();
94+
private final Map<ResourceBundle, Map<String, Map<Locale, MessageFormat>>> cachedBundleMessageFormats =
95+
new ConcurrentHashMap<>();
96+
97+
@Nullable
98+
private volatile MessageSourceControl control = new MessageSourceControl();
9499

95100

96101
/**
@@ -220,7 +225,24 @@ protected ResourceBundle getResourceBundle(String basename, Locale locale) {
220225
protected ResourceBundle doGetBundle(String basename, Locale locale) throws MissingResourceException {
221226
ClassLoader classLoader = getBundleClassLoader();
222227
Assert.state(classLoader != null, "No bundle ClassLoader set");
223-
return ResourceBundle.getBundle(basename, locale, classLoader, new MessageSourceControl());
228+
229+
MessageSourceControl control = this.control;
230+
if (control != null) {
231+
try {
232+
return ResourceBundle.getBundle(basename, locale, classLoader, control);
233+
}
234+
catch (UnsupportedOperationException ex) {
235+
// Probably in a Jigsaw environment on JDK 9+
236+
this.control = null;
237+
if (logger.isInfoEnabled()) {
238+
logger.info("ResourceBundle.Control not supported in current system environment: " +
239+
ex.getMessage() + " - falling back to plain ResourceBundle.getBundle retrieval.");
240+
}
241+
}
242+
}
243+
244+
// Fallback: plain getBundle lookup without Control handle
245+
return ResourceBundle.getBundle(basename, locale, classLoader);
224246
}
225247

226248
/**
@@ -266,7 +288,8 @@ protected MessageFormat getMessageFormat(ResourceBundle bundle, String code, Loc
266288
if (msg != null) {
267289
if (codeMap == null) {
268290
codeMap = new ConcurrentHashMap<>();
269-
Map<String, Map<Locale, MessageFormat>> existing = this.cachedBundleMessageFormats.putIfAbsent(bundle, codeMap);
291+
Map<String, Map<Locale, MessageFormat>> existing =
292+
this.cachedBundleMessageFormats.putIfAbsent(bundle, codeMap);
270293
if (existing != null) {
271294
codeMap = existing;
272295
}
@@ -341,9 +364,9 @@ public ResourceBundle newBundle(String baseName, Locale locale, String format, C
341364
final String resourceName = toResourceName(bundleName, "properties");
342365
final ClassLoader classLoader = loader;
343366
final boolean reloadFlag = reload;
344-
InputStream stream;
367+
InputStream inputStream;
345368
try {
346-
stream = AccessController.doPrivileged((PrivilegedExceptionAction<InputStream>) () -> {
369+
inputStream = AccessController.doPrivileged((PrivilegedExceptionAction<InputStream>) () -> {
347370
InputStream is = null;
348371
if (reloadFlag) {
349372
URL url = classLoader.getResource(resourceName);
@@ -364,12 +387,12 @@ public ResourceBundle newBundle(String baseName, Locale locale, String format, C
364387
catch (PrivilegedActionException ex) {
365388
throw (IOException) ex.getException();
366389
}
367-
if (stream != null) {
390+
if (inputStream != null) {
368391
String encoding = getDefaultEncoding();
369392
if (encoding == null) {
370393
encoding = "ISO-8859-1";
371394
}
372-
try (InputStreamReader bundleReader = new InputStreamReader(stream, encoding)) {
395+
try (InputStreamReader bundleReader = new InputStreamReader(inputStream, encoding)) {
373396
return loadBundle(bundleReader);
374397
}
375398
}

0 commit comments

Comments
 (0)