Package org.apache.shiro.subject.support
Class SubjectRunnable
- java.lang.Object
-
- org.apache.shiro.subject.support.SubjectRunnable
-
- All Implemented Interfaces:
Runnable
public class SubjectRunnable extends Object implements Runnable
ASubjectRunnableensures that a target/delegateRunnablewill execute such that any call toSecurityUtils.getSubject()during theRunnable's execution will return the associatedSubjectinstance. TheSubjectRunnableinstance 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 classrun(), 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
Runnableisrun - Any previous thread state that might have existed before the
Subjectwas bound is fully restored
Usage
This is typically considered a support class and is not often directly referenced. Most people prefer to use theSubject.executeorSubject.associateWithmethods, 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 ThreadStatethreadState
-
Constructor Summary
Constructors Modifier Constructor Description SubjectRunnable(Subject subject, Runnable delegate)Creates a newSubjectRunnablethat, when executed, will execute the targetdelegate, but guarantees that it will run associated with the specifiedSubject.protectedSubjectRunnable(ThreadState threadState, Runnable delegate)Creates a newSubjectRunnablethat, when executed, will perform thread statebindingand guaranteedrestorationbefore and after theRunnable's execution, respectively.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voiddoRun(Runnable runnable)voidrun()Binds the Subject thread state, executes the targetRunnableand then guarantees the previous thread state'srestoration:
-
-
-
Field Detail
-
threadState
protected final ThreadState threadState
-
-
Constructor Detail
-
SubjectRunnable
public SubjectRunnable(Subject subject, Runnable delegate)
Creates a newSubjectRunnablethat, 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, Runnable delegate) throws IllegalArgumentException
Creates a newSubjectRunnablethat, when executed, will perform thread statebindingand guaranteedrestorationbefore and after theRunnable's execution, respectively.- Parameters:
threadState- the thread state to bind and unbind before and after the runnable's execution.delegate- the delegateRunnableto execute when this instance isrun().- Throws:
IllegalArgumentException- if either theThreadStateorRunnablearguments arenull.
-
-
Method Detail
-
run
public void run()
Binds the Subject thread state, executes the targetRunnableand then guarantees the previous thread state'srestoration:try { threadState.bind();doRun(targetRunnable); } finally { threadState.restore()}
-
doRun
protected void doRun(Runnable runnable)
- Parameters:
runnable- the target runnable to run.
-
-