Class ThreadContext
An internal HashMap is used to maintain the key/value pairs
for each thread.
If the desired behavior is to ensure that bound data is not shared across threads in a pooled or reusable threaded environment, the application (or more likely a framework) must bind and remove any necessary values at the beginning and end of stack execution, respectively (i.e. individually explicitly or all via the clear method).
- Since:
- 0.1
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidbind(SecurityManager securityManager) Convenience method that simplifies binding the application's SecurityManager instance to the ThreadContext.static voidConvenience method that simplifies binding a Subject to the ThreadContext.static ObjectReturns the object for the specifiedkeythat is bound to the current thread.Returns the ThreadLocal Map.static SecurityManagerConvenience method that simplifies retrieval of the application's SecurityManager instance from the current thread.static SubjectConvenience method that simplifies retrieval of a thread-bound Subject.static voidBinds value for the givenkeyto the current thread.static voidremove()Removes the underlyingThreadLocalfrom the thread.static ObjectUnbinds the value for the givenkeyfrom the current thread.static voidsetResources(Map<Object, Object> newResources) Allows a caller to explicitly set the entire resource map.static SecurityManagerConvenience method that simplifies removal of the application's SecurityManager instance from the thread.static SubjectConvenience method that simplifies removal of a thread-local Subject from the thread.
-
Field Details
-
SECURITY_MANAGER_KEY
security manager key. -
SUBJECT_KEY
subject key.
-
-
Constructor Details
-
ThreadContext
protected ThreadContext()Default no-argument constructor.
-
-
Method Details
-
getResources
Returns the ThreadLocal Map. This Map is used internally to bind objects to the current thread by storing each object under a unique key.- Returns:
- the map of bound resources
-
setResources
Allows a caller to explicitly set the entire resource map. This operation overwrites everything that existed previously in the ThreadContext - if you need to retain what was on the thread prior to calling this method, call thegetResources()method, which will give you the existing state.- Parameters:
newResources- the resources to replace the existingresources.- Since:
- 1.0
-
get
-
put
Binds value for the givenkeyto the current thread.A null value has the same effect as if remove was called for the given key, i.e.:
if ( value == null ) { remove( key ); }- Parameters:
key- The key with which to identify thevalue.value- The value to bind to the thread.- Throws:
IllegalArgumentException- if thekeyargument is null.
-
remove
-
remove
Removes the underlyingThreadLocalfrom the thread. This method is meant to be the final 'clean up' operation that is called at the end of thread execution to prevent thread corruption in pooled thread environments.- Since:
- 1.0
-
getSecurityManager
Convenience method that simplifies retrieval of the application's SecurityManager instance from the current thread. If there is no SecurityManager bound to the thread (probably because framework code did not bind it to the thread), this method returns null. It is merely a convenient wrapper for the following:return (SecurityManager)get( SECURITY_MANAGER_KEY );This method only returns the bound value if it exists - it does not remove it from the thread. To remove it, one must callunbindSecurityManager()instead.- Returns:
- the Subject object bound to the thread, or null if there isn't one bound.
- Since:
- 0.9
-
bind
Convenience method that simplifies binding the application's SecurityManager instance to the ThreadContext.The method's existence is to help reduce casting in code and to simplify remembering of ThreadContext key names. The implementation is simple in that, if the SecurityManager is not null, it binds it to the thread, i.e.:
if (securityManager != null) { put( SECURITY_MANAGER_KEY, securityManager); }- Parameters:
securityManager- the application's SecurityManager instance to bind to the thread. If the argument is null, nothing will be done.- Since:
- 0.9
-
unbindSecurityManager
Convenience method that simplifies removal of the application's SecurityManager instance from the thread. The implementation just helps reduce casting and remembering of the ThreadContext key name, i.e it is merely a convenient wrapper for the following:return (SecurityManager)remove( SECURITY_MANAGER_KEY );If you wish to just retrieve the object from the thread without removing it (so it can be retrieved later during thread execution), use thegetSecurityManager()method instead.- Returns:
- the application's SecurityManager instance previously bound to the thread, or null if there was none bound.
- Since:
- 0.9
-
getSubject
Convenience method that simplifies retrieval of a thread-bound Subject. If there is no Subject bound to the thread, this method returns null. It is merely a convenient wrapper for the following:return (Subject)get( SUBJECT_KEY );This method only returns the bound value if it exists - it does not remove it from the thread. To remove it, one must callunbindSubject()instead.- Returns:
- the Subject object bound to the thread, or null if there isn't one bound.
- Since:
- 0.2
-
bind
Convenience method that simplifies binding a Subject to the ThreadContext.The method's existence is to help reduce casting in your own code and to simplify remembering of ThreadContext key names. The implementation is simple in that, if the Subject is not null, it binds it to the thread, i.e.:
if (subject != null) { put( SUBJECT_KEY, subject ); }- Parameters:
subject- the Subject object to bind to the thread. If the argument is null, nothing will be done.- Since:
- 0.2
-
unbindSubject
Convenience method that simplifies removal of a thread-local Subject from the thread. The implementation just helps reduce casting and remembering of the ThreadContext key name, i.e it is merely a convenient wrapper for the following:return (Subject)remove( SUBJECT_KEY );If you wish to just retrieve the object from the thread without removing it (so it can be retrieved later during thread execution), you should use thegetSubject()method for that purpose.- Returns:
- the Subject object previously bound to the thread, or null if there was none bound.
- Since:
- 0.2
-