Class SubjectAwareExecutor
java.lang.Object
org.apache.shiro.concurrent.SubjectAwareExecutor
- All Implemented Interfaces:
Executor
- Direct Known Subclasses:
SubjectAwareExecutorService
Executor implementation that will automatically first associate any argument
Runnable instances with the currently available Subject and then
dispatch the Subject-enabled runnable to an underlying delegate Executor
instance.
This is a simplification for applications that want to execute code as the currently
executing Subject on another thread, but don't want or need to call the
Subject.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:
Instead, if theRunnableapplicationWork = //instantiate or acquire Runnable from somewhereSubjectsubject =SecurityUtils.getSubject();Runnablework = 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:
Notice there is no use of the Shiro API in the 2nd code block, encouraging the principle of loose coupling across your codebase.RunnableapplicationWork = //instantiate or acquire Runnable from somewhereanExecutor.execute(work);
- Since:
- 1.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected RunnableUtility method for subclasses to associate the argumentRunnablewith the currently executing subject and then return the associated Runnable.voidExecutes the specified runnable by first associating it with the currently executingSubjectand then dispatches the associated Runnable to the underlying targetExecutorinstance.protected SubjectReturns the currently Subject instance that should be associated with Runnable or Callable instances before being dispatched to the targetExecutorinstance.Returns the target Executor instance that will actually execute the subject-associated Runnable instances.voidsetTargetExecutor(Executor targetExecutor) Sets target Executor instance that will actually execute the subject-associated Runnable instances.
-
Constructor Details
-
SubjectAwareExecutor
public SubjectAwareExecutor() -
SubjectAwareExecutor
-
-
Method Details
-
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
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
Returns the currently Subject instance that should be associated with Runnable or Callable instances before being dispatched to the targetExecutorinstance. 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
Executorinstance.
-
associateWithSubject
Utility method for subclasses to associate the argumentRunnablewith 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
-
execute
Executes the specified runnable by first associating it with the currently executingSubjectand then dispatches the associated Runnable to the underlying targetExecutorinstance.
-