/** * Associate the given LocaleContext with the current thread, * <i>not</i> exposing it as inheritable for child threads. * <p>The given LocaleContext may be a {@link TimeZoneAwareLocaleContext}, * containing a locale with associated time zone information. * @param localeContext the current LocaleContext, * or {@code null} to reset the thread-bound context * @see SimpleLocaleContext * @see SimpleTimeZoneAwareLocaleContext */ public static void setLocaleContext(@Nullable LocaleContext localeContext) { setLocaleContext(localeContext, false); }
private void resetContextHolders(HttpServletRequest request, @Nullable LocaleContext prevLocaleContext, @Nullable RequestAttributes previousAttributes) { LocaleContextHolder.setLocaleContext(prevLocaleContext, this.threadContextInheritable); RequestContextHolder.setRequestAttributes(previousAttributes, this.threadContextInheritable); }
/** * Associate the given LocaleContext with the current thread, * <i>not</i> exposing it as inheritable for child threads. * <p>The given LocaleContext may be a {@link TimeZoneAwareLocaleContext}, * containing a locale with associated time zone information. * @param localeContext the current LocaleContext, * or {@code null} to reset the thread-bound context * @see SimpleLocaleContext * @see SimpleTimeZoneAwareLocaleContext */ public static void setLocaleContext(@Nullable LocaleContext localeContext) { setLocaleContext(localeContext, false); }
private void initContextHolders(HttpServletRequest request, @Nullable LocaleContext localeContext, @Nullable RequestAttributes requestAttributes) { if (localeContext != null) { LocaleContextHolder.setLocaleContext(localeContext, this.threadContextInheritable); } if (requestAttributes != null) { RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable); } }
private void initContextHolders(HttpServletRequest request, @Nullable LocaleContext localeContext, @Nullable RequestAttributes requestAttributes) { if (localeContext != null) { LocaleContextHolder.setLocaleContext(localeContext, this.threadContextInheritable); } if (requestAttributes != null) { RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable); } }
private void resetContextHolders(HttpServletRequest request, @Nullable LocaleContext prevLocaleContext, @Nullable RequestAttributes previousAttributes) { LocaleContextHolder.setLocaleContext(prevLocaleContext, this.threadContextInheritable); RequestContextHolder.setRequestAttributes(previousAttributes, this.threadContextInheritable); }
/** * Associate the given Locale with the current thread, * preserving any TimeZone that may have been set already. * <p>Will implicitly create a LocaleContext for the given Locale. * @param locale the current Locale, or {@code null} to reset * the locale part of thread-bound context * @param inheritable whether to expose the LocaleContext as inheritable * for child threads (using an {@link InheritableThreadLocal}) * @see #setTimeZone(TimeZone, boolean) * @see SimpleLocaleContext#SimpleLocaleContext(Locale) */ public static void setLocale(@Nullable Locale locale, boolean inheritable) { LocaleContext localeContext = getLocaleContext(); TimeZone timeZone = (localeContext instanceof TimeZoneAwareLocaleContext ? ((TimeZoneAwareLocaleContext) localeContext).getTimeZone() : null); if (timeZone != null) { localeContext = new SimpleTimeZoneAwareLocaleContext(locale, timeZone); } else if (locale != null) { localeContext = new SimpleLocaleContext(locale); } else { localeContext = null; } setLocaleContext(localeContext, inheritable); }
/** * Associate the given TimeZone with the current thread, * preserving any Locale that may have been set already. * <p>Will implicitly create a LocaleContext for the given Locale. * @param timeZone the current TimeZone, or {@code null} to reset * the time zone part of the thread-bound context * @param inheritable whether to expose the LocaleContext as inheritable * for child threads (using an {@link InheritableThreadLocal}) * @see #setLocale(Locale, boolean) * @see SimpleTimeZoneAwareLocaleContext#SimpleTimeZoneAwareLocaleContext(Locale, TimeZone) */ public static void setTimeZone(@Nullable TimeZone timeZone, boolean inheritable) { LocaleContext localeContext = getLocaleContext(); Locale locale = (localeContext != null ? localeContext.getLocale() : null); if (timeZone != null) { localeContext = new SimpleTimeZoneAwareLocaleContext(locale, timeZone); } else if (locale != null) { localeContext = new SimpleLocaleContext(locale); } else { localeContext = null; } setLocaleContext(localeContext, inheritable); }
/** * Associate the given TimeZone with the current thread, * preserving any Locale that may have been set already. * <p>Will implicitly create a LocaleContext for the given Locale. * @param timeZone the current TimeZone, or {@code null} to reset * the time zone part of the thread-bound context * @param inheritable whether to expose the LocaleContext as inheritable * for child threads (using an {@link InheritableThreadLocal}) * @see #setLocale(Locale, boolean) * @see SimpleTimeZoneAwareLocaleContext#SimpleTimeZoneAwareLocaleContext(Locale, TimeZone) */ public static void setTimeZone(@Nullable TimeZone timeZone, boolean inheritable) { LocaleContext localeContext = getLocaleContext(); Locale locale = (localeContext != null ? localeContext.getLocale() : null); if (timeZone != null) { localeContext = new SimpleTimeZoneAwareLocaleContext(locale, timeZone); } else if (locale != null) { localeContext = new SimpleLocaleContext(locale); } else { localeContext = null; } setLocaleContext(localeContext, inheritable); }
/** * Associate the given Locale with the current thread, * preserving any TimeZone that may have been set already. * <p>Will implicitly create a LocaleContext for the given Locale. * @param locale the current Locale, or {@code null} to reset * the locale part of thread-bound context * @param inheritable whether to expose the LocaleContext as inheritable * for child threads (using an {@link InheritableThreadLocal}) * @see #setTimeZone(TimeZone, boolean) * @see SimpleLocaleContext#SimpleLocaleContext(Locale) */ public static void setLocale(@Nullable Locale locale, boolean inheritable) { LocaleContext localeContext = getLocaleContext(); TimeZone timeZone = (localeContext instanceof TimeZoneAwareLocaleContext ? ((TimeZoneAwareLocaleContext) localeContext).getTimeZone() : null); if (timeZone != null) { localeContext = new SimpleTimeZoneAwareLocaleContext(locale, timeZone); } else if (locale != null) { localeContext = new SimpleLocaleContext(locale); } else { localeContext = null; } setLocaleContext(localeContext, inheritable); }
@Test public void testSetTimeZoneAwareLocaleContext() { LocaleContext lc = new SimpleTimeZoneAwareLocaleContext(Locale.GERMANY, TimeZone.getTimeZone("GMT+1")); LocaleContextHolder.setLocaleContext(lc); assertSame(lc, LocaleContextHolder.getLocaleContext()); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getTimeZone("GMT+1"), LocaleContextHolder.getTimeZone()); LocaleContextHolder.resetLocaleContext(); assertNull(LocaleContextHolder.getLocaleContext()); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); }
@Test public void testSetLocaleContext() { LocaleContext lc = new SimpleLocaleContext(Locale.GERMAN); LocaleContextHolder.setLocaleContext(lc); assertSame(lc, LocaleContextHolder.getLocaleContext()); assertEquals(Locale.GERMAN, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); lc = new SimpleLocaleContext(Locale.GERMANY); LocaleContextHolder.setLocaleContext(lc); assertSame(lc, LocaleContextHolder.getLocaleContext()); assertEquals(Locale.GERMANY, LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); LocaleContextHolder.resetLocaleContext(); assertNull(LocaleContextHolder.getLocaleContext()); assertEquals(Locale.getDefault(), LocaleContextHolder.getLocale()); assertEquals(TimeZone.getDefault(), LocaleContextHolder.getTimeZone()); }
/** * Associate the given LocaleContext with the current thread, * <i>not</i> exposing it as inheritable for child threads. * <p>The given LocaleContext may be a {@link TimeZoneAwareLocaleContext}, * containing a locale with associated time zone information. * @param localeContext the current LocaleContext, * or {@code null} to reset the thread-bound context * @see SimpleLocaleContext * @see SimpleTimeZoneAwareLocaleContext */ public static void setLocaleContext(@Nullable LocaleContext localeContext) { setLocaleContext(localeContext, false); }
@Override protected void setThreadLocalValue( HttpServletRequest request, HttpServletResponse response, IPortletExecutionContext context, LocaleContext value) { if (value == null) { LocaleContextHolder.resetLocaleContext(); } else { LocaleContextHolder.setLocaleContext(value); } } }
@Override protected void setThreadLocalValue( HttpServletRequest request, HttpServletResponse response, IPortletExecutionContext context, LocaleContext value) { if (value == null) { LocaleContextHolder.resetLocaleContext(); } else { LocaleContextHolder.setLocaleContext(value); } } }
private void resetContextHolders(HttpServletRequest request, LocaleContext prevLocaleContext, RequestAttributes previousAttributes) { LocaleContextHolder.setLocaleContext(prevLocaleContext, this.threadContextInheritable); RequestContextHolder.setRequestAttributes(previousAttributes, this.threadContextInheritable); if (logger.isTraceEnabled()) { logger.trace("Cleared thread-bound request context: " + request); } }
@Override protected void doRenderService(RenderRequest request, RenderResponse response) throws Exception { LocaleContext currentLocaleContext = LocaleContextHolder.getLocaleContext(); try { super.doRenderService(request, response); } finally { LocaleContextHolder.setLocaleContext(currentLocaleContext); } }
@Override protected void doActionService(ActionRequest request, ActionResponse response) throws Exception { LocaleContext currentLocaleContext = LocaleContextHolder.getLocaleContext(); RequestAttributes currentRequestAttributes = RequestContextHolder.getRequestAttributes(); try { super.doActionService(request, response); } finally { LocaleContextHolder.setLocaleContext(currentLocaleContext); RequestContextHolder.setRequestAttributes(currentRequestAttributes); } }
public void run() { RequestContextHolder.setRequestAttributes(requestAttributes); LocaleContextHolder.setLocaleContext(localeContext); try { super.run(); } finally { RequestContextHolder.setRequestAttributes(null); LocaleContextHolder.setLocaleContext(null); if ( isRunOnce() ) { requestAttributes = null; localeContext = null; } } }
/** * Associate the given TimeZone with the current thread, * preserving any Locale that may have been set already. * <p>Will implicitly create a LocaleContext for the given Locale. * @param timeZone the current TimeZone, or {@code null} to reset * the time zone part of the thread-bound context * @param inheritable whether to expose the LocaleContext as inheritable * for child threads (using an {@link InheritableThreadLocal}) * @see #setLocale(Locale, boolean) * @see SimpleTimeZoneAwareLocaleContext#SimpleTimeZoneAwareLocaleContext(Locale, TimeZone) */ public static void setTimeZone(@Nullable TimeZone timeZone, boolean inheritable) { LocaleContext localeContext = getLocaleContext(); Locale locale = (localeContext != null ? localeContext.getLocale() : null); if (timeZone != null) { localeContext = new SimpleTimeZoneAwareLocaleContext(locale, timeZone); } else if (locale != null) { localeContext = new SimpleLocaleContext(locale); } else { localeContext = null; } setLocaleContext(localeContext, inheritable); }