Class SubjectAwareExecutor

  • All Implemented Interfaces:
    Direct Known Subclasses:
    SubjectAwareExecutorService

    public class SubjectAwareExecutor
    extends 
    implements 
    Executor implementation that will automatically first associate any argument instances with the currently available Subject and then dispatch the Subject-enabled runnable to an underlying delegate 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:

      applicationWork = //instantiate or acquire Runnable from somewhere
     Subject subject = SecurityUtils.getSubject();
      work = subject.associateWith(applicationWork);
     .;
     
    Instead, if the 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:
      applicationWork = //instantiate or acquire Runnable from somewhere
     .;
     
    Notice there is no use of the Shiro API in the 2nd code block, encouraging the principle of loose coupling across your codebase.
    Since:
    1.0
    See Also:
    SubjectAwareExecutorService
    • Constructor Summary

      Constructors 
      Constructor Description
      SubjectAwareExecutor()  
       targetExecutor)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected  r)
      Utility method for subclasses to associate the argument Runnable with the currently executing subject and then return the associated Runnable.
      void  command)
      Executes the specified runnable by first associating it with the currently executing Subject and then dispatches the associated Runnable to the underlying target instance.
      protected Subject getSubject()
      Returns the currently Subject instance that should be associated with Runnable or Callable instances before being dispatched to the target Executor instance.
      getTargetExecutor()
      Returns the target Executor instance that will actually execute the subject-associated Runnable instances.
      void  targetExecutor)
      Sets target Executor instance that will actually execute the subject-associated Runnable instances.
      • Methods inherited from class java.lang.

        , , , , , , , , , ,
    • Constructor Detail

      • SubjectAwareExecutor

        public SubjectAwareExecutor()
      • SubjectAwareExecutor

        public SubjectAwareExecutor​( targetExecutor)
    • Method Detail

      • getTargetExecutor

        public  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​( 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 target Executor instance. This implementation merely defaults to returning SecurityUtils.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  associateWithSubject​( r)
        Utility method for subclasses to associate the argument Runnable with the currently executing subject and then return the associated Runnable. The default implementation merely defaults to
         Subject 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

        public void execute​( command)
        Executes the specified runnable by first associating it with the currently executing Subject and then dispatches the associated Runnable to the underlying target instance.
        Specified by:
         in interface 
        Parameters:
        command - the runnable to associate with the currently executing subject and then to execute via the target Executor instance.