Package org.apache.shiro.concurrent
Class SubjectAwareExecutor
- java.lang.Object
-
- org.apache.shiro.concurrent.SubjectAwareExecutor
-
- All Implemented Interfaces:
Executor
- Direct Known Subclasses:
SubjectAwareExecutorService
public class SubjectAwareExecutor extends Object implements Executor
Executor
implementation that will automatically first associate any argumentRunnable
instances with the currently availableSubject
and then dispatch the Subject-enabled runnable to an underlying delegateExecutor
instance. This is a simplification for applications that want to execute code as the currently executingSubject
on another thread, but don't want or need to call theSubject.associateWith(Runnable)
method and dispatch to a Thread manually. This simplifies code and reduces Shiro dependencies across application source code. Consider this code that could be repeated in many places across an application:Runnable
applicationWork = //instantiate or acquire Runnable from somewhereSubject
subject =SecurityUtils
.getSubject()
;Runnable
work = subject.associateWith(applicationWork)
;anExecutor
.execute(work)
;Executor
instance used in application code is an instance of this class (which delegates to the target Executor that you want), all places in code like the above reduce to this:Runnable
applicationWork = //instantiate or acquire Runnable from somewhereanExecutor
.execute(work)
;- Since:
- 1.0
- See Also:
SubjectAwareExecutorService
-
-
Constructor Summary
Constructors Constructor Description SubjectAwareExecutor()
SubjectAwareExecutor(Executor targetExecutor)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Runnable
associateWithSubject(Runnable r)
Utility method for subclasses to associate the argumentRunnable
with the currently executing subject and then return the associated Runnable.void
execute(Runnable command)
Executes the specified runnable by first associating it with the currently executingSubject
and then dispatches the associated Runnable to the underlying targetExecutor
instance.protected Subject
getSubject()
Returns the currently Subject instance that should be associated with Runnable or Callable instances before being dispatched to the targetExecutor
instance.Executor
getTargetExecutor()
Returns the target Executor instance that will actually execute the subject-associated Runnable instances.void
setTargetExecutor(Executor targetExecutor)
Sets target Executor instance that will actually execute the subject-associated Runnable instances.
-
-
-
Constructor Detail
-
SubjectAwareExecutor
public SubjectAwareExecutor()
-
SubjectAwareExecutor
public SubjectAwareExecutor(Executor targetExecutor)
-
-
Method Detail
-
getTargetExecutor
public Executor getTargetExecutor()
Returns the target Executor instance that will actually execute the subject-associated Runnable instances.- Returns:
- target Executor instance that will actually execute the subject-associated Runnable instances.
-
setTargetExecutor
public void setTargetExecutor(Executor targetExecutor)
Sets target Executor instance that will actually execute the subject-associated Runnable instances.- Parameters:
targetExecutor
- the target Executor instance that will actually execute the subject-associated Runnable instances.
-
getSubject
protected Subject getSubject()
Returns the currently Subject instance that should be associated with Runnable or Callable instances before being dispatched to the targetExecutor
instance. This implementation merely defaults to returningSecurityUtils
.getSubject()
.- Returns:
- the currently Subject instance that should be associated with Runnable or Callable instances before
being dispatched to the target
Executor
instance.
-
associateWithSubject
protected Runnable associateWithSubject(Runnable r)
Utility method for subclasses to associate the argumentRunnable
with the currently executing subject and then return the associated Runnable. The default implementation merely defaults toSubject subject =
getSubject()
; return subject.associateWith(r)
;- Parameters:
r
- the argument runnable to be associated with the current subject- Returns:
- the associated runnable instance reflecting the current subject
-
-