|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.clazzes.util.aop.ThreadLocalManager
public class ThreadLocalManager
This class provides accessible methods for binding resources like pooled
database or remoting clients to the current thread, utilizing ThreadLocal
.
This class provides convenience methods binding, using and unbinding
resources to threads using a String key, for example in call-interceptors in an application framework.
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); // ... }
Field Summary | |
---|---|
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 Summary | |
---|---|
ThreadLocalManager()
|
Method Summary | ||
---|---|---|
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
|
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
|
unbindResource(String key)
Unbind a resource from a singleton factory from the current thread, which has previously been bound using bindResource(String, Object) . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String LOGIN_LOCALE_KEY
bindLoginLocale(Locale)
,
getLoginLocale()
,
unbindLoginLocale()
,
Constant Field Valuespublic static final String LOGIN_TIMEZONE_KEY
bindLoginTimeZone(TimeZone)
,
getLoginTimeZone()
,
unbindLoginTimeZone()
,
Constant Field Valuespublic static final String LOGIN_PRINCIPAL_KEY
bindLoginPrincipal(Principal)
,
getLoginPrincipal()
,
unbindLoginPrincipal()
,
Constant Field ValuesConstructor Detail |
---|
public ThreadLocalManager()
Method Detail |
---|
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.
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.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |