public class SubjectRunnable extends Object implements Runnable
SubjectRunnable
ensures that a target/delegate Runnable
will execute such that any
call to SecurityUtils.
getSubject()
during the
Runnable
's execution will return the associated Subject
instance. The SubjectRunnable
instance can be run on any thread (the current thread or asynchronously on another thread) and the
SecurityUtils.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 run()
, the following occurs:
Runnable
.Runnable
is run
Subject
was bound is fully restoredSubject.
execute
or
Subject.
associateWith
methods, which transparently perform the
necessary association logic.
An even more convenient alternative is to use a
SubjectAwareExecutor
, which transparently uses
instances of this class but does not require referencing Shiro's API at all.Subject.associateWith(Runnable)
,
SubjectAwareExecutor
Modifier and Type | Field and Description |
---|---|
protected ThreadState |
threadState |
Modifier | Constructor and Description |
---|---|
|
SubjectRunnable(Subject subject,
Runnable delegate)
Creates a new
SubjectRunnable that, when executed, will execute the target delegate , but
guarantees that it will run associated with the specified Subject . |
protected |
SubjectRunnable(ThreadState threadState,
Runnable delegate)
Creates a new
SubjectRunnable that, when executed, will perform thread state
binding and guaranteed restoration before and after the
Runnable 's execution, respectively. |
Modifier and Type | Method and Description |
---|---|
protected void |
doRun(Runnable runnable)
|
void |
run()
Bind s the Subject thread state, executes the target Runnable and then guarantees
the previous thread state's restoration : |
protected final ThreadState threadState
public SubjectRunnable(Subject subject, Runnable delegate)
SubjectRunnable
that, when executed, will execute the target delegate
, but
guarantees that it will run associated with the specified Subject
.subject
- the Subject to associate with the delegate's execution.delegate
- the runnable to run.protected SubjectRunnable(ThreadState threadState, Runnable delegate) throws IllegalArgumentException
SubjectRunnable
that, when executed, will perform thread state
binding
and guaranteed restoration
before and after the
Runnable
's execution, respectively.threadState
- the thread state to bind and unbind before and after the runnable's execution.delegate
- the delegate Runnable
to execute when this instance is run()
.IllegalArgumentException
- if either the ThreadState
or Runnable
arguments are null
.Copyright © 2004-2016 The Apache Software Foundation. All Rights Reserved.