Package org.apache.shiro.subject.support
Class SubjectRunnable
-
- org.apache.shiro.subject.support.SubjectRunnable
-
- All Implemented Interfaces:
public class SubjectRunnable extends implements
ASubjectRunnable
ensures that a target/delegate will execute such that any call toSecurityUtils.
getSubject()
during theRunnable
's execution will return the associatedSubject
instance. TheSubjectRunnable
instance can be run on any thread (the current thread or asynchronously on another thread) and theSecurityUtils.getSubject()
call will still work properly. This implementation also guarantees that Shiro's thread state will be identical before and after execution to ensure threads remain clean in any thread-pooled environment. When instances of this class , the following occurs:- The Subject and any of its associated thread state is first bound to the thread that executes the
Runnable
. - The delegate/target
Runnable
isrun
- Any previous thread state that might have existed before the
Subject
was bound is fully restored
Usage
This is typically considered a support class and is not often directly referenced. Most people prefer to use theSubject.
execute
orSubject.
associateWith
methods, which transparently perform the necessary association logic. An even more convenient alternative is to use aSubjectAwareExecutor
, which transparently uses instances of this class but does not require referencing Shiro's API at all.- Since:
- 1.0
- See Also:
Subject.associateWith(Runnable)
,SubjectAwareExecutor
-
-
Field Summary
Fields Modifier and Type Field Description protected ThreadState
threadState
-
Constructor Summary
Constructors Modifier Constructor Description SubjectRunnable(Subject subject, delegate)
Creates a newSubjectRunnable
that, when executed, will execute the targetdelegate
, but guarantees that it will run associated with the specifiedSubject
.protected
SubjectRunnable(ThreadState threadState, delegate)
Creates a newSubjectRunnable
that, when executed, will perform thread statebinding
and guaranteedrestoration
before and after the 's execution, respectively.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
runnable)
Simply calls the target 's method.void
run()
Bind
s the Subject thread state, executes the targetRunnable
and then guarantees the previous thread state'srestoration
:
-
-
-
Field Detail
-
threadState
protected final ThreadState threadState
-
-
Constructor Detail
-
SubjectRunnable
public SubjectRunnable(Subject subject, delegate)
Creates a newSubjectRunnable
that, when executed, will execute the targetdelegate
, but guarantees that it will run associated with the specifiedSubject
.- Parameters:
subject
- the Subject to associate with the delegate's execution.delegate
- the runnable to run.
-
SubjectRunnable
protected SubjectRunnable(ThreadState threadState, delegate) throws
Creates a newSubjectRunnable
that, when executed, will perform thread statebinding
and guaranteedrestoration
before and after the 's execution, respectively.- Parameters:
threadState
- the thread state to bind and unbind before and after the runnable's execution.delegate
- the delegateRunnable
to execute when this instance isrun()
.- Throws:
- if either the
ThreadState
or arguments arenull
.
-
-
Method Detail
-
run
public void run()
Bind
s the Subject thread state, executes the targetRunnable
and then guarantees the previous thread state'srestoration
:try { threadState.
bind()
;doRun
(targetRunnable); } finally { threadState.restore()
}- Specified by:
in interface
-
doRun
protected void doRun( runnable)
Simply calls the target 's method.- Parameters:
runnable
- the target runnable to run.
-
-