|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.shiro.util.ThreadContext
public abstract class ThreadContext
A ThreadContext provides a means of binding and unbinding objects to the current thread based on key/value pairs.
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).
remove()
Field Summary | |
---|---|
static String |
SECURITY_MANAGER_KEY
|
static String |
SUBJECT_KEY
|
Constructor Summary | |
---|---|
protected |
ThreadContext()
Default no-argument constructor. |
Method Summary | |
---|---|
static void |
bind(SecurityManager securityManager)
Convenience method that simplifies binding the application's SecurityManager instance to the ThreadContext. |
static void |
bind(Subject subject)
Convenience method that simplifies binding a Subject to the ThreadContext. |
static Object |
get(Object key)
Returns the object for the specified key that is bound to
the current thread. |
static Map<Object,Object> |
getResources()
Returns the ThreadLocal Map. |
static SecurityManager |
getSecurityManager()
Convenience method that simplifies retrieval of the application's SecurityManager instance from the current thread. |
static Subject |
getSubject()
Convenience method that simplifies retrieval of a thread-bound Subject. |
static void |
put(Object key,
Object value)
Binds value for the given key to the current thread. |
static void |
remove()
Remove s the underlying ThreadLocal from the thread. |
static Object |
remove(Object key)
Unbinds the value for the given key from the current
thread. |
static void |
setResources(Map<Object,Object> newResources)
Allows a caller to explicitly set the entire resource map. |
static SecurityManager |
unbindSecurityManager()
Convenience method that simplifies removal of the application's SecurityManager instance from the thread. |
static Subject |
unbindSubject()
Convenience method that simplifies removal of a thread-local Subject from the thread. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String SECURITY_MANAGER_KEY
public static final String SUBJECT_KEY
Constructor Detail |
---|
protected ThreadContext()
Method Detail |
---|
public static Map<Object,Object> getResources()
public static void setResources(Map<Object,Object> newResources)
getResources()
method, which will give you the existing state.
newResources
- the resources to replace the existing resources
.public static Object get(Object key)
key
that is bound to
the current thread.
key
- the key that identifies the value to return
key
or null
if
no value exists for the specified key
public static void put(Object key, Object value)
key
to 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 ); }
key
- The key with which to identify the value
.value
- The value to bind to the thread.
IllegalArgumentException
- if the key
argument is null.public static Object remove(Object key)
key
from the current
thread.
key
- The key identifying the value bound to the current thread.
public static void remove()
Remove
s the underlying ThreadLocal
from 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.
public static SecurityManager getSecurityManager()
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 call unbindSecurityManager()
instead.
public static void bind(SecurityManager securityManager)
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); }
securityManager
- the application's SecurityManager instance to bind to the thread. If the argument is
null, nothing will be done.public static SecurityManager unbindSecurityManager()
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 the getSecurityManager()
method instead.
public static Subject getSubject()
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 call unbindSubject()
instead.
public static void bind(Subject subject)
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 ); }
subject
- the Subject object to bind to the thread. If the argument is null, nothing will be done.public static Subject unbindSubject()
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 the getSubject()
method for that purpose.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |