Class ActiveLocale
- java.lang.Object
-
- org.marketcetera.util.log.ActiveLocale
-
public class ActiveLocale extends Object
Manages the active locale.Locale management relies on the concept of scopes. The search for the currently active locale, effected by
getLocale(), proceeds from the top-most to the bottom-most scope until a non-null locale is identified.At the lowest scope resides the system default locale, which cannot be altered via this class. Above it is the process-wide locale (more precisely, specific to the caller's classloader), managed via
setProcessLocale(Locale)andgetProcessLocale(). Above it is a thread-specific stack of locales that mirrors the thread's call stack. This locale stack is manipulated indirectly, without direct access to stack management operations. Locales are pushed and popped off the stack as code blocks are executed viarunWithLocale(Runnable,Locale)orrunWithLocale(Callable,Locale). Or, the top of the stack can be directly altered viasetThreadLocale(Locale)(you can assume the stack has a top, and therefore use this method, even if there was no explicit prior call to execute a code block).A null locale may be supplied as an argument, or be returned as the result, by several methods as noted. Its intended semantics are skip me: a null locale at a scope allows the locale at the scope below (or, if null as well, the locale below it and so on) to show through as the active locale.
- Since:
- 0.6.0
- Version:
- $Id: ActiveLocale.java 17760 2018-11-14 14:54:11Z colin $
- Author:
- tlerios@marketcetera.com
-
-
Field Summary
Fields Modifier and Type Field Description private static LocalesProcessLocaleprivate static InheritableThreadLocal<Stack<Locale>>sThreadStack
-
Constructor Summary
Constructors Modifier Constructor Description privateActiveLocale()Constructor.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description (package private) static voidclear()Clears all management settings for the calling thread.static LocalegetLocale()Returns the caller's active locale.static LocalegetProcessLocale()Returns the process-specific (more precisely, specific to the caller's classloader) locale.static voidpopLocale()Pop the locale at the top of the thread-specific (that is, specific to the caller's thread) locale stack.static voidpushLocale(Locale locale)Pushes the given locale onto the thread-specific (that is, specific to the caller's thread) locale stack.static voidrunWithLocale(Runnable runnable, Locale locale)Initiates execution of the given runnable within the context of the given locale as the active one.static <V> VrunWithLocale(Callable<V> callable, Locale locale)Initiates execution of the given callable within the context of the given locale as the active one.static voidsetProcessLocale(Locale locale)Sets the process-specific (more precisely, specific to the caller's classloader) locale to the given one.static voidsetThreadLocale(Locale locale)Sets the locale at the top of the thread-specific (that is, specific to the caller's thread) locale stack to the given locale.
-
-
-
Field Detail
-
sThreadStack
private static InheritableThreadLocal<Stack<Locale>> sThreadStack
-
sProcessLocale
private static Locale sProcessLocale
-
-
Method Detail
-
clear
static void clear()
Clears all management settings for the calling thread. This method is intended for testing purposes only.
-
getLocale
public static Locale getLocale()
Returns the caller's active locale.- Returns:
- The locale.
-
setProcessLocale
public static void setProcessLocale(Locale locale)
Sets the process-specific (more precisely, specific to the caller's classloader) locale to the given one.- Parameters:
locale- The locale. It may be null.
-
getProcessLocale
public static Locale getProcessLocale()
Returns the process-specific (more precisely, specific to the caller's classloader) locale.- Returns:
- The locale. It may be null.
-
setThreadLocale
public static void setThreadLocale(Locale locale)
Sets the locale at the top of the thread-specific (that is, specific to the caller's thread) locale stack to the given locale.- Parameters:
locale- The locale. It may be null.
-
pushLocale
public static void pushLocale(Locale locale)
Pushes the given locale onto the thread-specific (that is, specific to the caller's thread) locale stack. This call must be paired up with a call topopLocale().- Parameters:
locale- The locale. It may be null.
-
popLocale
public static void popLocale()
Pop the locale at the top of the thread-specific (that is, specific to the caller's thread) locale stack. This call must be paired up with a call topushLocale(Locale).- Throws:
EmptyStackException- Thrown if the locale stack is empty.
-
runWithLocale
public static void runWithLocale(Runnable runnable, Locale locale)
Initiates execution of the given runnable within the context of the given locale as the active one. When execution ends, the previously active locale is restored.- Parameters:
runnable- The runnable.locale- The locale. It may be null.
-
runWithLocale
public static <V> V runWithLocale(Callable<V> callable, Locale locale) throws Exception
Initiates execution of the given callable within the context of the given locale as the active one. When execution ends, the previously active locale is restored, and the callable's return value is returned.- Type Parameters:
V- the return value type- Parameters:
callable- The callable.locale- The locale. It may be null.- Returns:
- The callable's return value.
- Throws:
Exception- Propagated from the callable's invocation.
-
-