1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .ui .context .support ;
18
18
19
- import java .util .HashMap ;
20
19
import java .util .Map ;
20
+ import java .util .concurrent .ConcurrentHashMap ;
21
21
22
22
import org .apache .commons .logging .Log ;
23
23
import org .apache .commons .logging .LogFactory ;
24
24
25
+ import org .springframework .beans .factory .BeanClassLoaderAware ;
25
26
import org .springframework .context .HierarchicalMessageSource ;
26
27
import org .springframework .context .MessageSource ;
27
28
import org .springframework .context .support .ResourceBundleMessageSource ;
41
42
* @see java.util.ResourceBundle
42
43
* @see org.springframework.context.support.ResourceBundleMessageSource
43
44
*/
44
- public class ResourceBundleThemeSource implements HierarchicalThemeSource {
45
+ public class ResourceBundleThemeSource implements HierarchicalThemeSource , BeanClassLoaderAware {
45
46
46
47
protected final Log logger = LogFactory .getLog (getClass ());
47
48
48
49
private ThemeSource parentThemeSource ;
49
50
50
51
private String basenamePrefix = "" ;
51
52
53
+ private String defaultEncoding ;
54
+
55
+ private Boolean fallbackToSystemLocale ;
56
+
57
+ private ClassLoader beanClassLoader ;
58
+
52
59
/** 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 >();
54
61
55
62
56
63
@ Override
@@ -85,6 +92,33 @@ public void setBasenamePrefix(String basenamePrefix) {
85
92
this .basenamePrefix = (basenamePrefix != null ? basenamePrefix : "" );
86
93
}
87
94
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
+
88
122
89
123
/**
90
124
* This implementation returns a SimpleTheme instance, holding a
@@ -100,20 +134,23 @@ public Theme getTheme(String themeName) {
100
134
if (themeName == null ) {
101
135
return null ;
102
136
}
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
+ }
113
150
}
114
151
}
115
- return theme ;
116
152
}
153
+ return theme ;
117
154
}
118
155
119
156
/**
@@ -130,6 +167,15 @@ public Theme getTheme(String themeName) {
130
167
protected MessageSource createMessageSource (String basename ) {
131
168
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource ();
132
169
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
+ }
133
179
return messageSource ;
134
180
}
135
181
0 commit comments