org.clazzes.util.aop
Class ThreadLocalManager

java.lang.Object
  extended by org.clazzes.util.aop.ThreadLocalManager

public class ThreadLocalManager
extends Object

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.

Usage

An example usecase could be to provide jdbc-connections from a pool to database transaction methods.

In the invoke() method of the interceptor, the resource is bound before the method is allowed to proceed, and unbound when the method was executed (Exception handling ommitted for better readability):
 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
<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).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOGIN_LOCALE_KEY

public static final String LOGIN_LOCALE_KEY
The key under which the locale of a logged in user is bound to the current thread.

See Also:
bindLoginLocale(Locale), getLoginLocale(), unbindLoginLocale(), Constant Field Values

LOGIN_TIMEZONE_KEY

public static final String LOGIN_TIMEZONE_KEY
The key under which the time zone of a logged in user is bound to the current thread.

See Also:
bindLoginTimeZone(TimeZone), getLoginTimeZone(), unbindLoginTimeZone(), Constant Field Values

LOGIN_PRINCIPAL_KEY

public static final String LOGIN_PRINCIPAL_KEY
The key under which the principal of a logged in user is bound to the current thread.

See Also:
bindLoginPrincipal(Principal), getLoginPrincipal(), unbindLoginPrincipal(), Constant Field Values
Constructor Detail

ThreadLocalManager

public ThreadLocalManager()
Method Detail

bindResource

public static final void bindResource(String key,
                                      Object resource)
Bind a resource from a singleton factory to the current thread.

Parameters:
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.

unbindResource

public static final <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). If no resource has been bound to the current thread before, null is returned and a warning is logged.

Parameters:
key - A key for storing the given resource, usually a unique string like a JDBC URL or a remoting connection address.
Returns:
The previously bound resource like a JDBC connection or a remoting context.

getBoundResource

public static final <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).

Parameters:
key - A key for storing the given resource, usually a unique string like a JDBC URL or a remoting connection address.
Returns:
The previously bound resource like a JDBC connection or a remoting context. null is returned, if no resource has been bound to the current thread under the given key.

bindLoginLocale

public static final void bindLoginLocale(Locale loc)
A shortcut for:
ThreadLocalManager.bindResource(LOGIN_LOCALE_KEY,loc);

Parameters:
loc - The locale to bind to the thread under LOGIN_LOCALE_KEY.

getLoginLocale

public static final Locale getLoginLocale()
Returns:
A locale previously being bound by bindLoginLocale(Locale) or Locale.getDefault() if no locale is bound to this thread.

unbindLoginLocale

public static final Locale unbindLoginLocale()

Unbind a locale previously bound by bindLoginLocale(Locale).

A shortcut for:
ThreadLocalManager.unbindResource(LOGIN_LOCALE_KEY);

Returns:
A locale being bound by bindLoginLocale(Locale) or null, if no locale has been bound.

bindLoginTimeZone

public static final void bindLoginTimeZone(TimeZone tz)
A shortcut for:
ThreadLocalManager.bindResource(LOGIN_TIMEZONE_KEY,loc);

Parameters:
tz - The time zone to bind to the thread under LOGIN_TIMEZONE_KEY.

getLoginTimeZone

public static final TimeZone getLoginTimeZone()
Returns:
A time zone previously being bound by bindLoginTimeZone(TimeZone) or TimeZone.getDefault() if no time zone is bound to this thread.

unbindLoginTimeZone

public static final TimeZone unbindLoginTimeZone()

Unbind a time zone previously bound by bindLoginTimeZone(TimeZone).

A shortcut for:
ThreadLocalManager.unbindResource(LOGIN_TIMEZONE_KEY);

Returns:
A time zone previously being bound by bindLoginTimeZone(TimeZone) or null if no time zone is bound to this thread.

bindLoginPrincipal

public static final void bindLoginPrincipal(Principal principal)
A shortcut for:
ThreadLocalManager.bindResource(LOGIN_PRINCIPAL_KEY,principal);

Parameters:
principal - The principal to bind to the thread under LOGIN_PRINCIPAL_KEY.

getLoginPrincipal

public static final Principal getLoginPrincipal()
Returns:
The login principal previously being bound by bindLoginPrincipal(Principal) or null, if no logged in user is known.

unbindLoginPrincipal

public static final Principal unbindLoginPrincipal()

Unbind a principal previously bound by bindLoginPrincipal(Principal).

A shortcut for:
ThreadLocalManager.unbindResource(LOGIN_PRINCIPAL_KEY);

Returns:
A princiapl being bound by bindLoginPrincipal(Principal) or null, if no logged in user is known.


Copyright © 2012. All Rights Reserved.