View Javadoc

1   package com.atlassian.sal.core.executor;
2   
3   import java.util.concurrent.Callable;
4   
5   /**
6    * A wrapping callable that copies the thread local state into the calling code
7    *
8    * @since 2.0
9    */
10  class ThreadLocalDelegateCallable<T> implements Callable<T>
11  {
12      private final Callable<T> delegate;
13      private final ThreadLocalContextManager manager;
14      private final Object context;
15      private final ClassLoader contextClassLoader;
16  
17      /**
18       * Create a new callable
19       *
20       * @param manager The context manager to get the context from
21       * @param delegate The callable to delegate to
22       */
23      ThreadLocalDelegateCallable(ThreadLocalContextManager manager, Callable<T> delegate)
24      {
25          this.delegate = delegate;
26          this.manager = manager;
27          this.context = manager.getThreadLocalContext();
28          this.contextClassLoader = Thread.currentThread().getContextClassLoader();
29      }
30  
31      public T call() throws Exception
32      {
33          ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
34          try
35          {
36              Thread.currentThread().setContextClassLoader(contextClassLoader);
37              manager.setThreadLocalContext(context);
38              return delegate.call();
39          }
40          finally
41          {
42              Thread.currentThread().setContextClassLoader(oldContextClassLoader);
43              manager.clearThreadLocalContext();
44          }
45      }
46  }