Skip to content

Commit c7fef87

Browse files
committed
ResourceBundleThemeSource exposes fallbackToSystemLocale and defaultEncoding
Issue: SPR-13209
1 parent d738ddd commit c7fef87

File tree

1 file changed

+61
-15
lines changed

1 file changed

+61
-15
lines changed

spring-context/src/main/java/org/springframework/ui/context/support/ResourceBundleThemeSource.java

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 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.
@@ -16,12 +16,13 @@
1616

1717
package org.springframework.ui.context.support;
1818

19-
import java.util.HashMap;
2019
import java.util.Map;
20+
import java.util.concurrent.ConcurrentHashMap;
2121

2222
import org.apache.commons.logging.Log;
2323
import org.apache.commons.logging.LogFactory;
2424

25+
import org.springframework.beans.factory.BeanClassLoaderAware;
2526
import org.springframework.context.HierarchicalMessageSource;
2627
import org.springframework.context.MessageSource;
2728
import org.springframework.context.support.ResourceBundleMessageSource;
@@ -41,16 +42,22 @@
4142
* @see java.util.ResourceBundle
4243
* @see org.springframework.context.support.ResourceBundleMessageSource
4344
*/
44-
public class ResourceBundleThemeSource implements HierarchicalThemeSource {
45+
public class ResourceBundleThemeSource implements HierarchicalThemeSource, BeanClassLoaderAware {
4546

4647
protected final Log logger = LogFactory.getLog(getClass());
4748

4849
private ThemeSource parentThemeSource;
4950

5051
private String basenamePrefix = "";
5152

53+
private String defaultEncoding;
54+
55+
private Boolean fallbackToSystemLocale;
56+
57+
private ClassLoader beanClassLoader;
58+
5259
/** Map from theme name to Theme instance */
53-
private final Map<String, Theme> themeCache = new HashMap<String, Theme>();
60+
private final Map<String, Theme> themeCache = new ConcurrentHashMap<String, Theme>();
5461

5562

5663
@Override
@@ -85,6 +92,33 @@ public void setBasenamePrefix(String basenamePrefix) {
8592
this.basenamePrefix = (basenamePrefix != null ? basenamePrefix : "");
8693
}
8794

95+
/**
96+
* Set the default charset to use for parsing resource bundle files.
97+
* <p>{@link ResourceBundleMessageSource}'s default is the
98+
* {@code java.util.ResourceBundle} default encoding: ISO-8859-1.
99+
* @since 4.2
100+
* @see ResourceBundleMessageSource#setDefaultEncoding
101+
*/
102+
public void setDefaultEncoding(String defaultEncoding) {
103+
this.defaultEncoding = defaultEncoding;
104+
}
105+
106+
/**
107+
* Set whether to fall back to the system Locale if no files for a
108+
* specific Locale have been found.
109+
* <p>{@link ResourceBundleMessageSource}'s default is "true".
110+
* @since 4.2
111+
* @see ResourceBundleMessageSource#setFallbackToSystemLocale
112+
*/
113+
public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) {
114+
this.fallbackToSystemLocale = fallbackToSystemLocale;
115+
}
116+
117+
@Override
118+
public void setBeanClassLoader(ClassLoader beanClassLoader) {
119+
this.beanClassLoader = beanClassLoader;
120+
}
121+
88122

89123
/**
90124
* This implementation returns a SimpleTheme instance, holding a
@@ -100,20 +134,23 @@ public Theme getTheme(String themeName) {
100134
if (themeName == null) {
101135
return null;
102136
}
103-
synchronized (this.themeCache) {
104-
Theme theme = this.themeCache.get(themeName);
105-
if (theme == null) {
106-
String basename = this.basenamePrefix + themeName;
107-
MessageSource messageSource = createMessageSource(basename);
108-
theme = new SimpleTheme(themeName, messageSource);
109-
initParent(theme);
110-
this.themeCache.put(themeName, theme);
111-
if (logger.isDebugEnabled()) {
112-
logger.debug("Theme created: name '" + themeName + "', basename [" + basename + "]");
137+
Theme theme = this.themeCache.get(themeName);
138+
if (theme == null) {
139+
synchronized (this.themeCache) {
140+
theme = this.themeCache.get(themeName);
141+
if (theme == null) {
142+
String basename = this.basenamePrefix + themeName;
143+
MessageSource messageSource = createMessageSource(basename);
144+
theme = new SimpleTheme(themeName, messageSource);
145+
initParent(theme);
146+
this.themeCache.put(themeName, theme);
147+
if (logger.isDebugEnabled()) {
148+
logger.debug("Theme created: name '" + themeName + "', basename [" + basename + "]");
149+
}
113150
}
114151
}
115-
return theme;
116152
}
153+
return theme;
117154
}
118155

119156
/**
@@ -130,6 +167,15 @@ public Theme getTheme(String themeName) {
130167
protected MessageSource createMessageSource(String basename) {
131168
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
132169
messageSource.setBasename(basename);
170+
if (this.defaultEncoding != null) {
171+
messageSource.setDefaultEncoding(this.defaultEncoding);
172+
}
173+
if (this.fallbackToSystemLocale != null) {
174+
messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);
175+
}
176+
if (this.beanClassLoader != null) {
177+
messageSource.setBeanClassLoader(this.beanClassLoader);
178+
}
133179
return messageSource;
134180
}
135181

0 commit comments

Comments
 (0)