Skip to content

Commit 71cdf85

Browse files
committed
(Reloadable)ResourceBundleMessageSource allows for custom PropertyResourceBundle/Properties subclasses
Issue: SPR-12666
1 parent 450072e commit 71cdf85

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -345,7 +345,7 @@ protected PropertiesHolder getMergedProperties(Locale locale) {
345345
if (mergedHolder != null) {
346346
return mergedHolder;
347347
}
348-
Properties mergedProps = new Properties();
348+
Properties mergedProps = newProperties();
349349
mergedHolder = new PropertiesHolder(mergedProps, -1);
350350
for (int i = this.basenames.length - 1; i >= 0; i--) {
351351
List<String> filenames = calculateAllFilenames(this.basenames[i], locale);
@@ -565,7 +565,7 @@ protected PropertiesHolder refreshProperties(String filename, PropertiesHolder p
565565
*/
566566
protected Properties loadProperties(Resource resource, String filename) throws IOException {
567567
InputStream is = resource.getInputStream();
568-
Properties props = new Properties();
568+
Properties props = newProperties();
569569
try {
570570
if (resource.getFilename().endsWith(XML_SUFFIX)) {
571571
if (logger.isDebugEnabled()) {
@@ -601,6 +601,19 @@ protected Properties loadProperties(Resource resource, String filename) throws I
601601
}
602602
}
603603

604+
/**
605+
* Template method for creating a plain new {@link Properties} instance.
606+
* The default implementation simply calls {@link Properties#Properties()}.
607+
* <p>Allows for returning a custom {@link Properties} extension in subclasses.
608+
* Overriding methods should just instantiate a custom {@link Properties} subclass,
609+
* with no further initialization or population to be performed at that point.
610+
* @return a plain Properties instance
611+
* @since 4.2
612+
*/
613+
protected Properties newProperties() {
614+
return new Properties();
615+
}
616+
604617

605618
/**
606619
* Clear the resource bundle cache.

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.io.InputStreamReader;
22+
import java.io.Reader;
2223
import java.net.URL;
2324
import java.net.URLConnection;
2425
import java.security.AccessController;
@@ -67,7 +68,7 @@ public class ResourceBundleMessageSource extends AbstractMessageSource implement
6768

6869
private String[] basenames = new String[0];
6970

70-
private String defaultEncoding;
71+
private String defaultEncoding = "ISO-8859-1";
7172

7273
private boolean fallbackToSystemLocale = true;
7374

@@ -150,9 +151,9 @@ public void setBasenames(String... basenames) {
150151

151152
/**
152153
* Set the default charset to use for parsing resource bundle files.
153-
* <p>Default is none, using the {@code java.util.ResourceBundle}
154-
* default encoding: ISO-8859-1.
155-
* and more flexibility in setting of an encoding per file.
154+
* <p>Default is the {@code java.util.ResourceBundle} default encoding:
155+
* ISO-8859-1.
156+
* @since 3.1.3
156157
*/
157158
public void setDefaultEncoding(String defaultEncoding) {
158159
this.defaultEncoding = defaultEncoding;
@@ -167,6 +168,7 @@ public void setDefaultEncoding(String defaultEncoding) {
167168
* {@code java.util.ResourceBundle}. However, this is often not desirable
168169
* in an application server environment, where the system Locale is not relevant
169170
* to the application at all: Set this flag to "false" in such a scenario.
171+
* @since 3.1.3
170172
*/
171173
public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) {
172174
this.fallbackToSystemLocale = fallbackToSystemLocale;
@@ -188,6 +190,7 @@ public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) {
188190
* Consider {@link ReloadableResourceBundleMessageSource} in combination
189191
* with resource bundle files in a non-classpath location.
190192
* </ul>
193+
* @since 3.1.3
191194
*/
192195
public void setCacheSeconds(int cacheSeconds) {
193196
this.cacheMillis = (cacheSeconds * 1000);
@@ -304,18 +307,24 @@ protected ResourceBundle getResourceBundle(String basename, Locale locale) {
304307
* @param locale the Locale to look for
305308
* @return the corresponding ResourceBundle
306309
* @throws MissingResourceException if no matching bundle could be found
307-
* @see java.util.ResourceBundle#getBundle(String, java.util.Locale, ClassLoader)
310+
* @see java.util.ResourceBundle#getBundle(String, Locale, ClassLoader)
308311
* @see #getBundleClassLoader()
309312
*/
310313
protected ResourceBundle doGetBundle(String basename, Locale locale) throws MissingResourceException {
311-
if ((this.defaultEncoding != null && !"ISO-8859-1".equals(this.defaultEncoding)) ||
312-
!this.fallbackToSystemLocale || this.cacheMillis >= 0) {
313-
return ResourceBundle.getBundle(basename, locale, getBundleClassLoader(), new MessageSourceControl());
314-
}
315-
else {
316-
// Good old standard call...
317-
return ResourceBundle.getBundle(basename, locale, getBundleClassLoader());
318-
}
314+
return ResourceBundle.getBundle(basename, locale, getBundleClassLoader(), new MessageSourceControl());
315+
}
316+
317+
/**
318+
* Load a property-based resource bundle from the given reader.
319+
* <p>The default implementation returns a {@link PropertyResourceBundle}.
320+
* @param reader the reader for the target resource
321+
* @return the fully loaded bundle
322+
* @throws IOException in case of I/O failure
323+
* @since 4.2
324+
* @see PropertyResourceBundle#PropertyResourceBundle(Reader)
325+
*/
326+
protected ResourceBundle loadBundle(Reader reader) throws IOException {
327+
return new PropertyResourceBundle(reader);
319328
}
320329

321330
/**
@@ -430,9 +439,7 @@ public InputStream run() throws IOException {
430439
}
431440
if (stream != null) {
432441
try {
433-
return (defaultEncoding != null ?
434-
new PropertyResourceBundle(new InputStreamReader(stream, defaultEncoding)) :
435-
new PropertyResourceBundle(stream));
442+
return loadBundle(new InputStreamReader(stream, defaultEncoding));
436443
}
437444
finally {
438445
stream.close();

0 commit comments

Comments
 (0)