public class ThreadLocalManager extends Object
ThreadLocal
.
public Object invoke(MethodInvocation invocation) throws Throwable { // 1) fetch jdbc connection: Connection connection = this.dataSource.getConnection(); // ... // 2) bind resource to thread-local manager, whereby GlobalVars.SOME_STATIC_KEY is a global constant ThreadLocalManager.bindResource(GlobalVars.SOME_STATIC_KEY, connection); // ... // 3) proceed execution of intercepted method, commit connection Object ret = invocation.proceed(); connection.commit(); // ... // 4) unbind the connection and close it ThreadLocalManager.unbindResource(GlobalVars.SOME_STATIC_KEY); connection.close(); }To access the bound resource, the intercepted method can call
getBoundResource(String)
:
public void interceptMeJDBC() { // ... Connection con = ThreadLocalManager.getBoundResource(GlobalVars.SOME_STATIC_KEY); // ... }
Modifier and Type | Field and Description |
---|---|
static String |
LOGIN_LOCALE_KEY
The key under which the locale of a logged in user
is bound to the current thread.
|
static String |
LOGIN_PRINCIPAL_KEY
The key under which the principal of a logged in user
is bound to the current thread.
|
static String |
LOGIN_TIMEZONE_KEY
The key under which the time zone of a logged in user
is bound to the current thread.
|
Constructor and Description |
---|
ThreadLocalManager() |
Modifier and Type | Method and Description |
---|---|
static void |
bindLoginLocale(Locale loc)
A shortcut for:
ThreadLocalManager.bindResource(LOGIN_LOCALE_KEY,loc); |
static void |
bindLoginPrincipal(Principal principal)
A shortcut for:
ThreadLocalManager.bindResource(LOGIN_PRINCIPAL_KEY,principal); |
static void |
bindLoginTimeZone(TimeZone tz)
A shortcut for:
ThreadLocalManager.bindResource(LOGIN_TIMEZONE_KEY,loc); |
static void |
bindResource(String key,
Object resource)
Bind a resource from a singleton factory to the current thread.
|
static <T> T |
getBoundResource(String key)
Fetch a resource from a singleton factory from the current thread, which has previously been
bound using
bindResource(String, Object) . |
static Locale |
getLoginLocale() |
static Principal |
getLoginPrincipal() |
static TimeZone |
getLoginTimeZone() |
static Locale |
unbindLoginLocale()
Unbind a locale previously bound by
bindLoginLocale(Locale) . |
static Principal |
unbindLoginPrincipal()
Unbind a principal previously bound by
bindLoginPrincipal(Principal) . |
static TimeZone |
unbindLoginTimeZone()
Unbind a time zone previously bound by
bindLoginTimeZone(TimeZone) . |
static <T> T |
unbindResource(String key)
Unbind a resource from a singleton factory from the current thread, which has previously been
bound using
bindResource(String, Object) . |
static <T> T |
unbindResourceLeniently(String key)
Unbind a resource from a singleton factory from the current thread, which maybe
has previously been bound using
bindResource(String, Object) . |
public static final String LOGIN_LOCALE_KEY
public static final String LOGIN_TIMEZONE_KEY
public static final String LOGIN_PRINCIPAL_KEY
public static final void bindResource(String key, Object resource)
key
- A key for storing the given resource, usually a unique
string like a JDBC URL or a remoting connection address.resource
- A resource like a JDBC connection or a remoting context.public static final <T> T unbindResource(String key)
bindResource(String, Object)
.
If no resource has been bound to the current thread before, null
is returned
and a warning is logged.key
- A key for storing the given resource, usually a unique
string like a JDBC URL or a remoting connection address.unbindResourceLeniently(String)
public static final <T> T unbindResourceLeniently(String key)
bindResource(String, Object)
.
If no resource has been bound to the current thread before, null
is returned
and no warning is logged.key
- A key for storing the given resource, usually a unique
string like a JDBC URL or a remoting connection address.unbindResource(String)
public static final <T> T getBoundResource(String key)
bindResource(String, Object)
.key
- A key for storing the given resource, usually a unique
string like a JDBC URL or a remoting connection address.null
is returned, if no resource has been bound to the current
thread under the given key.public static final void bindLoginLocale(Locale loc)
ThreadLocalManager.bindResource(LOGIN_LOCALE_KEY,loc);
loc
- The locale to bind to the thread under LOGIN_LOCALE_KEY
.public static final Locale getLoginLocale()
bindLoginLocale(Locale)
or Locale.getDefault()
if no locale is bound to this
thread.public static final Locale unbindLoginLocale()
Unbind a locale previously bound by bindLoginLocale(Locale)
.
ThreadLocalManager.unbindResource(LOGIN_LOCALE_KEY);
bindLoginLocale(Locale)
or
null
, if no locale has been bound.public static final void bindLoginTimeZone(TimeZone tz)
ThreadLocalManager.bindResource(LOGIN_TIMEZONE_KEY,loc);
tz
- The time zone to bind to the thread under LOGIN_TIMEZONE_KEY
.public static final TimeZone getLoginTimeZone()
bindLoginTimeZone(TimeZone)
or TimeZone.getDefault()
if no time zone is bound to this
thread.public static final TimeZone unbindLoginTimeZone()
Unbind a time zone previously bound by bindLoginTimeZone(TimeZone)
.
ThreadLocalManager.unbindResource(LOGIN_TIMEZONE_KEY);
bindLoginTimeZone(TimeZone)
or null
if no time zone is bound to this
thread.public static final void bindLoginPrincipal(Principal principal)
ThreadLocalManager.bindResource(LOGIN_PRINCIPAL_KEY,principal);
principal
- The principal to bind to the thread under LOGIN_PRINCIPAL_KEY
.public static final Principal getLoginPrincipal()
bindLoginPrincipal(Principal)
or null
,
if no logged in user is known.public static final Principal unbindLoginPrincipal()
Unbind a principal previously bound by bindLoginPrincipal(Principal)
.
ThreadLocalManager.unbindResource(LOGIN_PRINCIPAL_KEY);
bindLoginPrincipal(Principal)
or
null
, if no logged in user is known.Copyright © 2016 Clazzes.org. All rights reserved.