org.apache.shiro.concurrent
Class SubjectAwareExecutorService

java.lang.Object
  extended by org.apache.shiro.concurrent.SubjectAwareExecutor
      extended by org.apache.shiro.concurrent.SubjectAwareExecutorService
All Implemented Interfaces:
Executor, ExecutorService
Direct Known Subclasses:
SubjectAwareScheduledExecutorService

public class SubjectAwareExecutorService
extends SubjectAwareExecutor
implements ExecutorService

ExecutorService implementation that will automatically first associate any argument Runnable or Callable instances with the currently available subject and then dispatch the Subject-enabled runnable or callable to an underlying delegate ExecutorService instance. The principle is the same as the parent SubjectAwareExecutor class, but enables the richer ExecutorService API.

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) or Subject.associateWith(Callable) methods and dispatch them 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:

 Callable applicationWork = //instantiate or acquire Callable from somewhere
 Subject subject = SecurityUtils.getSubject();
 Callable work = subject.associateWith(applicationWork);
 anExecutorService.submit(work);
 
Instead, if the ExecutorService instance used at runtime is an instance of this class (which delegates to the target ExecutorService that you want), all places in code like the above reduce to this:
 Callable applicationWork = //instantiate or acquire Callable from somewhere
 anExecutorService.submit(work);
 
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

Constructor Summary
SubjectAwareExecutorService()
           
SubjectAwareExecutorService(ExecutorService target)
           
 
Method Summary
protected
<T> Callable<T>
associateWithSubject(Callable<T> task)
           
protected
<T> Collection<Callable<T>>
associateWithSubject(Collection<? extends Callable<T>> tasks)
           
 boolean awaitTermination(long timeout, TimeUnit unit)
           
 ExecutorService getTargetExecutorService()
           
<T> List<Future<T>>
invokeAll(Collection<? extends Callable<T>> tasks)
           
<T> List<Future<T>>
invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
           
<T> T
invokeAny(Collection<? extends Callable<T>> tasks)
           
<T> T
invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
           
 boolean isShutdown()
           
 boolean isTerminated()
           
 void setTargetExecutor(Executor targetExecutor)
          Sets target Executor instance that will actually execute the subject-associated Runnable instances.
 void setTargetExecutorService(ExecutorService targetExecutorService)
           
 void shutdown()
           
 List<Runnable> shutdownNow()
           
<T> Future<T>
submit(Callable<T> task)
           
 Future<?> submit(Runnable task)
           
<T> Future<T>
submit(Runnable task, T result)
           
 
Methods inherited from class org.apache.shiro.concurrent.SubjectAwareExecutor
associateWithSubject, execute, getSubject, getTargetExecutor
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.concurrent.Executor
execute
 

Constructor Detail

SubjectAwareExecutorService

public SubjectAwareExecutorService()

SubjectAwareExecutorService

public SubjectAwareExecutorService(ExecutorService target)
Method Detail

getTargetExecutorService

public ExecutorService getTargetExecutorService()

setTargetExecutorService

public void setTargetExecutorService(ExecutorService targetExecutorService)

setTargetExecutor

public void setTargetExecutor(Executor targetExecutor)
Description copied from class: SubjectAwareExecutor
Sets target Executor instance that will actually execute the subject-associated Runnable instances.

Overrides:
setTargetExecutor in class SubjectAwareExecutor
Parameters:
targetExecutor - the target Executor instance that will actually execute the subject-associated Runnable instances.

shutdown

public void shutdown()
Specified by:
shutdown in interface ExecutorService

shutdownNow

public List<Runnable> shutdownNow()
Specified by:
shutdownNow in interface ExecutorService

isShutdown

public boolean isShutdown()
Specified by:
isShutdown in interface ExecutorService

isTerminated

public boolean isTerminated()
Specified by:
isTerminated in interface ExecutorService

awaitTermination

public boolean awaitTermination(long timeout,
                                TimeUnit unit)
                         throws InterruptedException
Specified by:
awaitTermination in interface ExecutorService
Throws:
InterruptedException

associateWithSubject

protected <T> Callable<T> associateWithSubject(Callable<T> task)

submit

public <T> Future<T> submit(Callable<T> task)
Specified by:
submit in interface ExecutorService

submit

public <T> Future<T> submit(Runnable task,
                            T result)
Specified by:
submit in interface ExecutorService

submit

public Future<?> submit(Runnable task)
Specified by:
submit in interface ExecutorService

associateWithSubject

protected <T> Collection<Callable<T>> associateWithSubject(Collection<? extends Callable<T>> tasks)

invokeAll

public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
                          throws InterruptedException
Specified by:
invokeAll in interface ExecutorService
Throws:
InterruptedException

invokeAll

public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
                                     long timeout,
                                     TimeUnit unit)
                          throws InterruptedException
Specified by:
invokeAll in interface ExecutorService
Throws:
InterruptedException

invokeAny

public <T> T invokeAny(Collection<? extends Callable<T>> tasks)
            throws InterruptedException,
                   ExecutionException
Specified by:
invokeAny in interface ExecutorService
Throws:
InterruptedException
ExecutionException

invokeAny

public <T> T invokeAny(Collection<? extends Callable<T>> tasks,
                       long timeout,
                       TimeUnit unit)
            throws InterruptedException,
                   ExecutionException,
                   TimeoutException
Specified by:
invokeAny in interface ExecutorService
Throws:
InterruptedException
ExecutionException
TimeoutException


Copyright © 2004-2012 The Apache Software Foundation. All Rights Reserved.