X Tutup
Skip to content

Commit ae3cfff

Browse files
committed
fixed JodaTimeContextHolder to use a non-inheritable ThreadLocal and expose a reset method (SPR-7441); use of remove() even when being called with a null argument
1 parent c046419 commit ae3cfff

File tree

3 files changed

+49
-23
lines changed

3 files changed

+49
-23
lines changed

org.springframework.context/src/main/java/org/springframework/context/i18n/LocaleContextHolder.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
/**
2525
* Simple holder class that associates a LocaleContext instance
2626
* with the current thread. The LocaleContext will be inherited
27-
* by any child threads spawned by the current thread.
27+
* by any child threads spawned by the current thread if the
28+
* <code>inheritable<code> flag is set to <code>true</code>.
2829
*
2930
* <p>Used as a central holder for the current Locale in Spring,
3031
* wherever necessary: for example, in MessageSourceAccessor.
@@ -58,28 +59,32 @@ public static void resetLocaleContext() {
5859
/**
5960
* Associate the given LocaleContext with the current thread,
6061
* <i>not</i> exposing it as inheritable for child threads.
61-
* @param localeContext the current LocaleContext, or <code>null</code> to reset
62-
* the thread-bound context
62+
* @param localeContext the current LocaleContext
6363
*/
6464
public static void setLocaleContext(LocaleContext localeContext) {
6565
setLocaleContext(localeContext, false);
6666
}
6767

6868
/**
6969
* Associate the given LocaleContext with the current thread.
70-
* @param localeContext the current LocaleContext, or <code>null</code> to reset
71-
* the thread-bound context
70+
* @param localeContext the current LocaleContext,
71+
* or <code>null</code> to reset the thread-bound context
7272
* @param inheritable whether to expose the LocaleContext as inheritable
7373
* for child threads (using an {@link java.lang.InheritableThreadLocal})
7474
*/
7575
public static void setLocaleContext(LocaleContext localeContext, boolean inheritable) {
76-
if (inheritable) {
77-
inheritableLocaleContextHolder.set(localeContext);
78-
localeContextHolder.remove();
76+
if (localeContext == null) {
77+
resetLocaleContext();
7978
}
8079
else {
81-
localeContextHolder.set(localeContext);
82-
inheritableLocaleContextHolder.remove();
80+
if (inheritable) {
81+
inheritableLocaleContextHolder.set(localeContext);
82+
localeContextHolder.remove();
83+
}
84+
else {
85+
localeContextHolder.set(localeContext);
86+
inheritableLocaleContextHolder.remove();
87+
}
8388
}
8489
}
8590

org.springframework.context/src/main/java/org/springframework/format/datetime/joda/JodaTimeContextHolder.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,40 @@
2020

2121
import org.joda.time.format.DateTimeFormatter;
2222

23-
import org.springframework.core.NamedInheritableThreadLocal;
23+
import org.springframework.core.NamedThreadLocal;
2424

2525
/**
2626
* A holder for a thread-local user {@link JodaTimeContext}.
2727
*
2828
* @author Keith Donald
29+
* @author Juergen Hoeller
2930
* @since 3.0
3031
*/
3132
public final class JodaTimeContextHolder {
3233

3334
private static final ThreadLocal<JodaTimeContext> jodaTimeContextHolder =
34-
new NamedInheritableThreadLocal<JodaTimeContext>("JodaTime Context");
35+
new NamedThreadLocal<JodaTimeContext>("JodaTime Context");
3536

3637

38+
/**
39+
* Reset the JodaTimeContext for the current thread.
40+
*/
41+
public static void resetJodaTimeContext() {
42+
jodaTimeContextHolder.remove();
43+
}
44+
3745
/**
3846
* Associate the given JodaTimeContext with the current thread.
39-
* @param context the current JodaTimeContext, or <code>null</code> to clear
40-
* the thread-bound context
47+
* @param jodaTimeContext the current JodaTimeContext,
48+
* or <code>null</code> to reset the thread-bound context
4149
*/
42-
public static void setJodaTimeContext(JodaTimeContext context) {
43-
jodaTimeContextHolder.set(context);
50+
public static void setJodaTimeContext(JodaTimeContext jodaTimeContext) {
51+
if (jodaTimeContext == null) {
52+
resetJodaTimeContext();
53+
}
54+
else {
55+
jodaTimeContextHolder.set(jodaTimeContext);
56+
}
4457
}
4558

4659
/**

org.springframework.web/src/main/java/org/springframework/web/context/request/RequestContextHolder.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525
/**
2626
* Holder class to expose the web request in the form of a thread-bound
27-
* {@link RequestAttributes} object.
27+
* {@link RequestAttributes} object. The request will be inherited
28+
* by any child threads spawned by the current thread if the
29+
* <code>inheritable<code> flag is set to <code>true</code>.
2830
*
2931
* <p>Use {@link RequestContextListener} or
3032
* {@link org.springframework.web.filter.RequestContextFilter} to expose
@@ -73,18 +75,24 @@ public static void setRequestAttributes(RequestAttributes attributes) {
7375

7476
/**
7577
* Bind the given RequestAttributes to the current thread.
76-
* @param attributes the RequestAttributes to expose
78+
* @param attributes the RequestAttributes to expose,
79+
* or <code>null</code> to reset the thread-bound context
7780
* @param inheritable whether to expose the RequestAttributes as inheritable
7881
* for child threads (using an {@link java.lang.InheritableThreadLocal})
7982
*/
8083
public static void setRequestAttributes(RequestAttributes attributes, boolean inheritable) {
81-
if (inheritable) {
82-
inheritableRequestAttributesHolder.set(attributes);
83-
requestAttributesHolder.remove();
84+
if (attributes == null) {
85+
resetRequestAttributes();
8486
}
8587
else {
86-
requestAttributesHolder.set(attributes);
87-
inheritableRequestAttributesHolder.remove();
88+
if (inheritable) {
89+
inheritableRequestAttributesHolder.set(attributes);
90+
requestAttributesHolder.remove();
91+
}
92+
else {
93+
requestAttributesHolder.set(attributes);
94+
inheritableRequestAttributesHolder.remove();
95+
}
8896
}
8997
}
9098

0 commit comments

Comments
 (0)
X Tutup