1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2016 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.
44
44
public abstract class LocaleContextHolder {
45
45
46
46
private static final ThreadLocal <LocaleContext > localeContextHolder =
47
- new NamedThreadLocal <LocaleContext >("Locale context " );
47
+ new NamedThreadLocal <LocaleContext >("LocaleContext " );
48
48
49
49
private static final ThreadLocal <LocaleContext > inheritableLocaleContextHolder =
50
- new NamedInheritableThreadLocal <LocaleContext >("Locale context" );
50
+ new NamedInheritableThreadLocal <LocaleContext >("LocaleContext" );
51
+
52
+ // Shared default locale at the framework level
53
+ private static Locale defaultLocale ;
54
+
55
+ // Shared default time zone at the framework level
56
+ private static TimeZone defaultTimeZone ;
51
57
52
58
53
59
/**
@@ -152,18 +158,38 @@ else if (locale != null) {
152
158
setLocaleContext (localeContext , inheritable );
153
159
}
154
160
161
+ /**
162
+ * Set a shared default locale at the framework level,
163
+ * as an alternative to the JVM-wide default locale.
164
+ * <p><b>NOTE:</b> This can be useful to set an application-level
165
+ * default locale which differs from the JVM-wide default locale.
166
+ * However, this requires each such application to operate against
167
+ * locally deployed Spring Framework jars. Do not deploy Spring
168
+ * as a shared library at the server level in such a scenario!
169
+ * @param locale the default locale (or {@code null} for none,
170
+ * letting lookups fall back to {@link Locale#getDefault()})
171
+ * @since 4.3.5
172
+ * @see #getLocale()
173
+ * @see Locale#getDefault()
174
+ */
175
+ public static void setDefaultLocale (Locale locale ) {
176
+ LocaleContextHolder .defaultLocale = locale ;
177
+ }
178
+
155
179
/**
156
180
* Return the Locale associated with the current thread, if any,
157
- * or the system default Locale else . This is effectively a
181
+ * or the system default Locale otherwise . This is effectively a
158
182
* replacement for {@link java.util.Locale#getDefault()},
159
183
* able to optionally respect a user-level Locale setting.
160
- * <p>Note: This method has a fallback to the system default Locale.
184
+ * <p>Note: This method has a fallback to the shared default Locale,
185
+ * either at the framework level or at the JVM-wide system level.
161
186
* If you'd like to check for the raw LocaleContext content
162
187
* (which may indicate no specific locale through {@code null}, use
163
188
* {@link #getLocaleContext()} and call {@link LocaleContext#getLocale()}
164
189
* @return the current Locale, or the system default Locale if no
165
190
* specific Locale has been associated with the current thread
166
191
* @see LocaleContext#getLocale()
192
+ * @see #setDefaultLocale(Locale)
167
193
* @see java.util.Locale#getDefault()
168
194
*/
169
195
public static Locale getLocale () {
@@ -174,7 +200,7 @@ public static Locale getLocale() {
174
200
return locale ;
175
201
}
176
202
}
177
- return Locale .getDefault ();
203
+ return ( defaultLocale != null ? defaultLocale : Locale .getDefault () );
178
204
}
179
205
180
206
/**
@@ -217,19 +243,39 @@ else if (locale != null) {
217
243
setLocaleContext (localeContext , inheritable );
218
244
}
219
245
246
+ /**
247
+ * Set a shared default time zone at the framework level,
248
+ * as an alternative to the JVM-wide default time zone.
249
+ * <p><b>NOTE:</b> This can be useful to set an application-level
250
+ * default time zone which differs from the JVM-wide default time zone.
251
+ * However, this requires each such application to operate against
252
+ * locally deployed Spring Framework jars. Do not deploy Spring
253
+ * as a shared library at the server level in such a scenario!
254
+ * @param timeZone the default time zone (or {@code null} for none,
255
+ * letting lookups fall back to {@link TimeZone#getDefault()})
256
+ * @since 4.3.5
257
+ * @see #getTimeZone()
258
+ * @see TimeZone#getDefault()
259
+ */
260
+ public static void setDefaultTimeZone (TimeZone timeZone ) {
261
+ defaultTimeZone = timeZone ;
262
+ }
263
+
220
264
/**
221
265
* Return the TimeZone associated with the current thread, if any,
222
- * or the system default TimeZone else . This is effectively a
266
+ * or the system default TimeZone otherwise . This is effectively a
223
267
* replacement for {@link java.util.TimeZone#getDefault()},
224
268
* able to optionally respect a user-level TimeZone setting.
225
- * <p>Note: This method has a fallback to the system default Locale.
269
+ * <p>Note: This method has a fallback to the shared default TimeZone,
270
+ * either at the framework level or at the JVM-wide system level.
226
271
* If you'd like to check for the raw LocaleContext content
227
272
* (which may indicate no specific time zone through {@code null}, use
228
273
* {@link #getLocaleContext()} and call {@link TimeZoneAwareLocaleContext#getTimeZone()}
229
274
* after downcasting to {@link TimeZoneAwareLocaleContext}.
230
275
* @return the current TimeZone, or the system default TimeZone if no
231
276
* specific TimeZone has been associated with the current thread
232
277
* @see TimeZoneAwareLocaleContext#getTimeZone()
278
+ * @see #setDefaultTimeZone(TimeZone)
233
279
* @see java.util.TimeZone#getDefault()
234
280
*/
235
281
public static TimeZone getTimeZone () {
@@ -240,7 +286,7 @@ public static TimeZone getTimeZone() {
240
286
return timeZone ;
241
287
}
242
288
}
243
- return TimeZone .getDefault ();
289
+ return ( defaultTimeZone != null ? defaultTimeZone : TimeZone .getDefault () );
244
290
}
245
291
246
292
}
0 commit comments