001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.context.i18n;
018:
019: import java.util.Locale;
020:
021: /**
022: * Simple holder class that associates a LocaleContext instance
023: * with the current thread. The LocaleContext will be inherited
024: * by any child threads spawned by the current thread.
025: *
026: * <p>Used as a central holder for the current Locale in Spring,
027: * wherever necessary: for example, in MessageSourceAccessor.
028: * DispatcherServlet automatically exposes its current Locale here.
029: * Other applications can expose theirs too, to make classes like
030: * MessageSourceAccessor automatically use that Locale.
031: *
032: * @author Juergen Hoeller
033: * @since 1.2
034: * @see LocaleContext
035: * @see org.springframework.context.support.MessageSourceAccessor
036: * @see org.springframework.web.servlet.DispatcherServlet
037: */
038: public abstract class LocaleContextHolder {
039:
040: private static final ThreadLocal localeContextHolder = new ThreadLocal();
041:
042: private static final ThreadLocal inheritableLocaleContextHolder = new InheritableThreadLocal();
043:
044: /**
045: * Reset the LocaleContext for the current thread.
046: */
047: public static void resetLocaleContext() {
048: localeContextHolder.set(null);
049: inheritableLocaleContextHolder.set(null);
050: }
051:
052: /**
053: * Associate the given LocaleContext with the current thread,
054: * <i>not</i> exposing it as inheritable for child threads.
055: * @param localeContext the current LocaleContext, or <code>null</code> to reset
056: * the thread-bound context
057: */
058: public static void setLocaleContext(LocaleContext localeContext) {
059: setLocaleContext(localeContext, false);
060: }
061:
062: /**
063: * Associate the given LocaleContext with the current thread.
064: * @param localeContext the current LocaleContext, or <code>null</code> to reset
065: * the thread-bound context
066: * @param inheritable whether to expose the LocaleContext as inheritable
067: * for child threads (using an {@link java.lang.InheritableThreadLocal})
068: */
069: public static void setLocaleContext(LocaleContext localeContext,
070: boolean inheritable) {
071: if (inheritable) {
072: inheritableLocaleContextHolder.set(localeContext);
073: localeContextHolder.set(null);
074: } else {
075: localeContextHolder.set(localeContext);
076: inheritableLocaleContextHolder.set(null);
077: }
078: }
079:
080: /**
081: * Return the LocaleContext associated with the current thread, if any.
082: * @return the current LocaleContext, or <code>null</code> if none
083: */
084: public static LocaleContext getLocaleContext() {
085: LocaleContext localeContext = (LocaleContext) localeContextHolder
086: .get();
087: if (localeContext == null) {
088: localeContext = (LocaleContext) inheritableLocaleContextHolder
089: .get();
090: }
091: return localeContext;
092: }
093:
094: /**
095: * Associate the given Locale with the current thread.
096: * <p>Will implicitly create a LocaleContext for the given Locale,
097: * <i>not</i> exposing it as inheritable for child threads.
098: * @param locale the current Locale, or <code>null</code> to reset
099: * the thread-bound context
100: * @see SimpleLocaleContext#SimpleLocaleContext(java.util.Locale)
101: */
102: public static void setLocale(Locale locale) {
103: setLocale(locale, false);
104: }
105:
106: /**
107: * Associate the given Locale with the current thread.
108: * <p>Will implicitly create a LocaleContext for the given Locale.
109: * @param locale the current Locale, or <code>null</code> to reset
110: * the thread-bound context
111: * @param inheritable whether to expose the LocaleContext as inheritable
112: * for child threads (using an {@link java.lang.InheritableThreadLocal})
113: * @see SimpleLocaleContext#SimpleLocaleContext(java.util.Locale)
114: */
115: public static void setLocale(Locale locale, boolean inheritable) {
116: LocaleContext localeContext = (locale != null ? new SimpleLocaleContext(
117: locale)
118: : null);
119: setLocaleContext(localeContext, inheritable);
120: }
121:
122: /**
123: * Return the Locale associated with the current thread, if any,
124: * or the system default Locale else.
125: * @return the current Locale, or the system default Locale if no
126: * specific Locale has been associated with the current thread
127: * @see LocaleContext#getLocale()
128: * @see java.util.Locale#getDefault()
129: */
130: public static Locale getLocale() {
131: LocaleContext localeContext = getLocaleContext();
132: return (localeContext != null ? localeContext.getLocale()
133: : Locale.getDefault());
134: }
135:
136: }
|