KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > i18n > LocaleContextHolder


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.context.i18n;
18
19 import java.util.Locale JavaDoc;
20
21 /**
22  * Simple holder class that associates a LocaleContext instance
23  * with the current thread. The LocaleContext will be inherited
24  * by any child threads spawned by the current thread.
25  *
26  * <p>Used as a central holder for the current Locale in Spring,
27  * wherever necessary: for example, in MessageSourceAccessor.
28  * DispatcherServlet automatically exposes its current Locale here.
29  * Other applications can expose theirs too, to make classes like
30  * MessageSourceAccessor automatically use that Locale.
31  *
32  * @author Juergen Hoeller
33  * @since 1.2
34  * @see LocaleContext
35  * @see org.springframework.context.support.MessageSourceAccessor
36  * @see org.springframework.web.servlet.DispatcherServlet
37  */

38 public abstract class LocaleContextHolder {
39
40     private static final ThreadLocal JavaDoc localeContextHolder = new ThreadLocal JavaDoc();
41
42     private static final ThreadLocal JavaDoc inheritableLocaleContextHolder = new InheritableThreadLocal JavaDoc();
43
44
45     /**
46      * Reset the LocaleContext for the current thread.
47      */

48     public static void resetLocaleContext() {
49         localeContextHolder.set(null);
50         inheritableLocaleContextHolder.set(null);
51     }
52
53     /**
54      * Associate the given LocaleContext with the current thread,
55      * <i>not</i> exposing it as inheritable for child threads.
56      * @param localeContext the current LocaleContext, or <code>null</code> to reset
57      * the thread-bound context
58      */

59     public static void setLocaleContext(LocaleContext localeContext) {
60         setLocaleContext(localeContext, false);
61     }
62
63     /**
64      * Associate the given LocaleContext with the current thread.
65      * @param localeContext the current LocaleContext, or <code>null</code> to reset
66      * the thread-bound context
67      * @param inheritable whether to expose the LocaleContext as inheritable
68      * for child threads (using an {@link java.lang.InheritableThreadLocal})
69      */

70     public static void setLocaleContext(LocaleContext localeContext, boolean inheritable) {
71         if (inheritable) {
72             inheritableLocaleContextHolder.set(localeContext);
73             localeContextHolder.set(null);
74         }
75         else {
76             localeContextHolder.set(localeContext);
77             inheritableLocaleContextHolder.set(null);
78         }
79     }
80
81     /**
82      * Return the LocaleContext associated with the current thread, if any.
83      * @return the current LocaleContext, or <code>null</code> if none
84      */

85     public static LocaleContext getLocaleContext() {
86         LocaleContext localeContext = (LocaleContext) localeContextHolder.get();
87         if (localeContext == null) {
88             localeContext = (LocaleContext) inheritableLocaleContextHolder.get();
89         }
90         return localeContext;
91     }
92
93     /**
94      * Associate the given Locale with the current thread.
95      * <p>Will implicitly create a LocaleContext for the given Locale,
96      * <i>not</i> exposing it as inheritable for child threads.
97      * @param locale the current Locale, or <code>null</code> to reset
98      * the thread-bound context
99      * @see SimpleLocaleContext#SimpleLocaleContext(java.util.Locale)
100      */

101     public static void setLocale(Locale JavaDoc locale) {
102         setLocale(locale, false);
103     }
104
105     /**
106      * Associate the given Locale with the current thread.
107      * <p>Will implicitly create a LocaleContext for the given Locale.
108      * @param locale the current Locale, or <code>null</code> to reset
109      * the thread-bound context
110      * @param inheritable whether to expose the LocaleContext as inheritable
111      * for child threads (using an {@link java.lang.InheritableThreadLocal})
112      * @see SimpleLocaleContext#SimpleLocaleContext(java.util.Locale)
113      */

114     public static void setLocale(Locale JavaDoc locale, boolean inheritable) {
115         LocaleContext localeContext = (locale != null ? new SimpleLocaleContext(locale) : null);
116         setLocaleContext(localeContext, inheritable);
117     }
118
119     /**
120      * Return the Locale associated with the current thread, if any,
121      * or the system default Locale else.
122      * @return the current Locale, or the system default Locale if no
123      * specific Locale has been associated with the current thread
124      * @see LocaleContext#getLocale()
125      * @see java.util.Locale#getDefault()
126      */

127     public static Locale JavaDoc getLocale() {
128         LocaleContext localeContext = getLocaleContext();
129         return (localeContext != null ? localeContext.getLocale() : Locale.getDefault());
130     }
131
132 }
133
Popular Tags