org.apache.shiro.subject.support
Class SubjectCallable<V>

java.lang.Object
  extended by org.apache.shiro.subject.support.SubjectCallable<V>
All Implemented Interfaces:
Callable<V>

public class SubjectCallable<V>
extends Object
implements Callable<V>

A SubjectCallable associates a Subject with a target/delegate Callable to ensure proper Subject thread-state management when the Callable executes. This ensures that any calls to SecurityUtils.getSubject() during the target Callable's execution still work correctly even if the Callable executes on a different thread than the one that created it. This allows Subject access during asynchronous operations.

When instances of this class execute (typically via a ExecutorService), the following occurs:

  1. The specified Subject any of its associated thread state is first bound to the thread that executes the Callable.
  2. The delegate/target Callable is executed
  3. The previous thread state that might have existed before the Subject was bound is fully restored

This behavior ensures that the thread that executes this Callable, which is often a different thread than the one that created the instance, retains a Subject to support SecurityUtils.getSubject() invocations. It also guarantees that the running thread remains 'clean' in any thread-pooled environments.

Usage

This is typically considered a support class and is not often directly referenced. Most people prefer to use the Subject.associateWith method, which will automatically return an instance of this class.

An even more convenient alternative is to use a SubjectAwareExecutorService, which transparently uses instances of this class.

Since:
1.0
See Also:
Subject.associateWith(Callable), SubjectAwareExecutorService

Field Summary
protected  ThreadState threadState
           
 
Constructor Summary
  SubjectCallable(Subject subject, Callable<V> delegate)
           
protected SubjectCallable(ThreadState threadState, Callable<V> delegate)
           
 
Method Summary
 V call()
           
protected  V doCall(Callable<V> target)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

threadState

protected final ThreadState threadState
Constructor Detail

SubjectCallable

public SubjectCallable(Subject subject,
                       Callable<V> delegate)

SubjectCallable

protected SubjectCallable(ThreadState threadState,
                          Callable<V> delegate)
Method Detail

call

public V call()
       throws Exception
Specified by:
call in interface Callable<V>
Throws:
Exception

doCall

protected V doCall(Callable<V> target)
            throws Exception
Throws:
Exception


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