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
This commit is contained in:
@@ -24,7 +24,8 @@ import org.springframework.core.NamedThreadLocal;
|
||||
/**
|
||||
* Simple holder class that associates a LocaleContext instance
|
||||
* with the current thread. The LocaleContext will be inherited
|
||||
* by any child threads spawned by the current thread.
|
||||
* by any child threads spawned by the current thread if the
|
||||
* <code>inheritable<code> flag is set to <code>true</code>.
|
||||
*
|
||||
* <p>Used as a central holder for the current Locale in Spring,
|
||||
* wherever necessary: for example, in MessageSourceAccessor.
|
||||
@@ -58,8 +59,7 @@ public abstract class LocaleContextHolder {
|
||||
/**
|
||||
* Associate the given LocaleContext with the current thread,
|
||||
* <i>not</i> exposing it as inheritable for child threads.
|
||||
* @param localeContext the current LocaleContext, or <code>null</code> to reset
|
||||
* the thread-bound context
|
||||
* @param localeContext the current LocaleContext
|
||||
*/
|
||||
public static void setLocaleContext(LocaleContext localeContext) {
|
||||
setLocaleContext(localeContext, false);
|
||||
@@ -67,19 +67,24 @@ public abstract class LocaleContextHolder {
|
||||
|
||||
/**
|
||||
* Associate the given LocaleContext with the current thread.
|
||||
* @param localeContext the current LocaleContext, or <code>null</code> to reset
|
||||
* the thread-bound context
|
||||
* @param localeContext the current LocaleContext,
|
||||
* or <code>null</code> to reset the thread-bound context
|
||||
* @param inheritable whether to expose the LocaleContext as inheritable
|
||||
* for child threads (using an {@link java.lang.InheritableThreadLocal})
|
||||
*/
|
||||
public static void setLocaleContext(LocaleContext localeContext, boolean inheritable) {
|
||||
if (inheritable) {
|
||||
inheritableLocaleContextHolder.set(localeContext);
|
||||
localeContextHolder.remove();
|
||||
if (localeContext == null) {
|
||||
resetLocaleContext();
|
||||
}
|
||||
else {
|
||||
localeContextHolder.set(localeContext);
|
||||
inheritableLocaleContextHolder.remove();
|
||||
if (inheritable) {
|
||||
inheritableLocaleContextHolder.set(localeContext);
|
||||
localeContextHolder.remove();
|
||||
}
|
||||
else {
|
||||
localeContextHolder.set(localeContext);
|
||||
inheritableLocaleContextHolder.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,27 +20,40 @@ import java.util.Locale;
|
||||
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
|
||||
import org.springframework.core.NamedInheritableThreadLocal;
|
||||
import org.springframework.core.NamedThreadLocal;
|
||||
|
||||
/**
|
||||
* A holder for a thread-local user {@link JodaTimeContext}.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
*/
|
||||
public final class JodaTimeContextHolder {
|
||||
|
||||
private static final ThreadLocal<JodaTimeContext> jodaTimeContextHolder =
|
||||
new NamedInheritableThreadLocal<JodaTimeContext>("JodaTime Context");
|
||||
new NamedThreadLocal<JodaTimeContext>("JodaTime Context");
|
||||
|
||||
|
||||
/**
|
||||
* Associate the given JodaTimeContext with the current thread.
|
||||
* @param context the current JodaTimeContext, or <code>null</code> to clear
|
||||
* the thread-bound context
|
||||
* Reset the JodaTimeContext for the current thread.
|
||||
*/
|
||||
public static void setJodaTimeContext(JodaTimeContext context) {
|
||||
jodaTimeContextHolder.set(context);
|
||||
public static void resetJodaTimeContext() {
|
||||
jodaTimeContextHolder.remove();
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate the given JodaTimeContext with the current thread.
|
||||
* @param jodaTimeContext the current JodaTimeContext,
|
||||
* or <code>null</code> to reset the thread-bound context
|
||||
*/
|
||||
public static void setJodaTimeContext(JodaTimeContext jodaTimeContext) {
|
||||
if (jodaTimeContext == null) {
|
||||
resetJodaTimeContext();
|
||||
}
|
||||
else {
|
||||
jodaTimeContextHolder.set(jodaTimeContext);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user