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:
Juergen Hoeller
2010-10-11 18:55:21 +00:00
parent c046419acd
commit ae3cfff380
3 changed files with 50 additions and 24 deletions

View File

@@ -24,7 +24,8 @@ import org.springframework.core.NamedThreadLocal;
/** /**
* Simple holder class that associates a LocaleContext instance * Simple holder class that associates a LocaleContext instance
* with the current thread. The LocaleContext will be inherited * 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, * <p>Used as a central holder for the current Locale in Spring,
* wherever necessary: for example, in MessageSourceAccessor. * wherever necessary: for example, in MessageSourceAccessor.
@@ -58,8 +59,7 @@ public abstract class LocaleContextHolder {
/** /**
* Associate the given LocaleContext with the current thread, * Associate the given LocaleContext with the current thread,
* <i>not</i> exposing it as inheritable for child threads. * <i>not</i> exposing it as inheritable for child threads.
* @param localeContext the current LocaleContext, or <code>null</code> to reset * @param localeContext the current LocaleContext
* the thread-bound context
*/ */
public static void setLocaleContext(LocaleContext localeContext) { public static void setLocaleContext(LocaleContext localeContext) {
setLocaleContext(localeContext, false); setLocaleContext(localeContext, false);
@@ -67,19 +67,24 @@ public abstract class LocaleContextHolder {
/** /**
* Associate the given LocaleContext with the current thread. * Associate the given LocaleContext with the current thread.
* @param localeContext the current LocaleContext, or <code>null</code> to reset * @param localeContext the current LocaleContext,
* the thread-bound context * or <code>null</code> to reset the thread-bound context
* @param inheritable whether to expose the LocaleContext as inheritable * @param inheritable whether to expose the LocaleContext as inheritable
* for child threads (using an {@link java.lang.InheritableThreadLocal}) * for child threads (using an {@link java.lang.InheritableThreadLocal})
*/ */
public static void setLocaleContext(LocaleContext localeContext, boolean inheritable) { public static void setLocaleContext(LocaleContext localeContext, boolean inheritable) {
if (inheritable) { if (localeContext == null) {
inheritableLocaleContextHolder.set(localeContext); resetLocaleContext();
localeContextHolder.remove();
} }
else { else {
localeContextHolder.set(localeContext); if (inheritable) {
inheritableLocaleContextHolder.remove(); inheritableLocaleContextHolder.set(localeContext);
localeContextHolder.remove();
}
else {
localeContextHolder.set(localeContext);
inheritableLocaleContextHolder.remove();
}
} }
} }

View File

@@ -20,27 +20,40 @@ import java.util.Locale;
import org.joda.time.format.DateTimeFormatter; 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}. * A holder for a thread-local user {@link JodaTimeContext}.
* *
* @author Keith Donald * @author Keith Donald
* @author Juergen Hoeller
* @since 3.0 * @since 3.0
*/ */
public final class JodaTimeContextHolder { public final class JodaTimeContextHolder {
private static final ThreadLocal<JodaTimeContext> 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. * Reset the JodaTimeContext for the current thread.
* @param context the current JodaTimeContext, or <code>null</code> to clear
* the thread-bound context
*/ */
public static void setJodaTimeContext(JodaTimeContext context) { public static void resetJodaTimeContext() {
jodaTimeContextHolder.set(context); 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);
}
} }
/** /**

View File

@@ -24,7 +24,9 @@ import org.springframework.util.ClassUtils;
/** /**
* Holder class to expose the web request in the form of a thread-bound * Holder class to expose the web request in the form of a thread-bound
* {@link RequestAttributes} object. * {@link RequestAttributes} object. The request will be inherited
* by any child threads spawned by the current thread if the
* <code>inheritable<code> flag is set to <code>true</code>.
* *
* <p>Use {@link RequestContextListener} or * <p>Use {@link RequestContextListener} or
* {@link org.springframework.web.filter.RequestContextFilter} to expose * {@link org.springframework.web.filter.RequestContextFilter} to expose
@@ -73,18 +75,24 @@ public abstract class RequestContextHolder {
/** /**
* Bind the given RequestAttributes to the current thread. * Bind the given RequestAttributes to the current thread.
* @param attributes the RequestAttributes to expose * @param attributes the RequestAttributes to expose,
* or <code>null</code> to reset the thread-bound context
* @param inheritable whether to expose the RequestAttributes as inheritable * @param inheritable whether to expose the RequestAttributes as inheritable
* for child threads (using an {@link java.lang.InheritableThreadLocal}) * for child threads (using an {@link java.lang.InheritableThreadLocal})
*/ */
public static void setRequestAttributes(RequestAttributes attributes, boolean inheritable) { public static void setRequestAttributes(RequestAttributes attributes, boolean inheritable) {
if (inheritable) { if (attributes == null) {
inheritableRequestAttributesHolder.set(attributes); resetRequestAttributes();
requestAttributesHolder.remove();
} }
else { else {
requestAttributesHolder.set(attributes); if (inheritable) {
inheritableRequestAttributesHolder.remove(); inheritableRequestAttributesHolder.set(attributes);
requestAttributesHolder.remove();
}
else {
requestAttributesHolder.set(attributes);
inheritableRequestAttributesHolder.remove();
}
} }
} }