19
19
import java .io .IOException ;
20
20
import java .io .InputStream ;
21
21
import java .io .InputStreamReader ;
22
+ import java .io .Reader ;
22
23
import java .net .URL ;
23
24
import java .net .URLConnection ;
24
25
import java .security .AccessController ;
@@ -67,7 +68,7 @@ public class ResourceBundleMessageSource extends AbstractMessageSource implement
67
68
68
69
private String [] basenames = new String [0 ];
69
70
70
- private String defaultEncoding ;
71
+ private String defaultEncoding = "ISO-8859-1" ;
71
72
72
73
private boolean fallbackToSystemLocale = true ;
73
74
@@ -150,9 +151,9 @@ public void setBasenames(String... basenames) {
150
151
151
152
/**
152
153
* 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
156
157
*/
157
158
public void setDefaultEncoding (String defaultEncoding ) {
158
159
this .defaultEncoding = defaultEncoding ;
@@ -167,6 +168,7 @@ public void setDefaultEncoding(String defaultEncoding) {
167
168
* {@code java.util.ResourceBundle}. However, this is often not desirable
168
169
* in an application server environment, where the system Locale is not relevant
169
170
* to the application at all: Set this flag to "false" in such a scenario.
171
+ * @since 3.1.3
170
172
*/
171
173
public void setFallbackToSystemLocale (boolean fallbackToSystemLocale ) {
172
174
this .fallbackToSystemLocale = fallbackToSystemLocale ;
@@ -188,6 +190,7 @@ public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) {
188
190
* Consider {@link ReloadableResourceBundleMessageSource} in combination
189
191
* with resource bundle files in a non-classpath location.
190
192
* </ul>
193
+ * @since 3.1.3
191
194
*/
192
195
public void setCacheSeconds (int cacheSeconds ) {
193
196
this .cacheMillis = (cacheSeconds * 1000 );
@@ -304,18 +307,24 @@ protected ResourceBundle getResourceBundle(String basename, Locale locale) {
304
307
* @param locale the Locale to look for
305
308
* @return the corresponding ResourceBundle
306
309
* @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)
308
311
* @see #getBundleClassLoader()
309
312
*/
310
313
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 );
319
328
}
320
329
321
330
/**
@@ -430,9 +439,7 @@ public InputStream run() throws IOException {
430
439
}
431
440
if (stream != null ) {
432
441
try {
433
- return (defaultEncoding != null ?
434
- new PropertyResourceBundle (new InputStreamReader (stream , defaultEncoding )) :
435
- new PropertyResourceBundle (stream ));
442
+ return loadBundle (new InputStreamReader (stream , defaultEncoding ));
436
443
}
437
444
finally {
438
445
stream .close ();
0 commit comments