View Javadoc

1   package com.atlassian.sal.core.executor;
2   
3   import com.atlassian.sal.api.executor.ThreadLocalContextManager;
4   import com.atlassian.sal.api.executor.ThreadLocalDelegateExecutorFactory;
5   
6   import java.util.concurrent.Callable;
7   import java.util.concurrent.Executor;
8   import java.util.concurrent.ExecutorService;
9   import java.util.concurrent.ScheduledExecutorService;
10  
11  import static com.google.common.base.Preconditions.checkNotNull;
12  
13  /**
14   * Creates delegating executable classes that copy thread local state
15   */
16  public class DefaultThreadLocalDelegateExecutorFactory<C> implements ThreadLocalDelegateExecutorFactory {
17      private final ThreadLocalContextManager<C> manager;
18  
19      protected DefaultThreadLocalDelegateExecutorFactory(ThreadLocalContextManager<C> manager) {
20          this.manager = checkNotNull(manager);
21      }
22  
23      /**
24       * Creates an executor that ensures the executed delegate instance runs in the same thread local context as the calling
25       * code.
26       *
27       * @param delegate The Executor instance to delegate to
28       * @return The wrapping executor that manages thread local state transfer
29       */
30      public Executor createExecutor(Executor delegate) {
31          return new ThreadLocalDelegateExecutor<C>(manager, delegate);
32      }
33  
34      /**
35       * Creates an executor service that ensures the executed delegate instance runs in the same thread local context as the
36       * calling code.
37       *
38       * @param delegate The ExecutorService instance to delegate to
39       * @return The wrapping ExecutorService that manages thread local state transfer
40       */
41      public ExecutorService createExecutorService(ExecutorService delegate) {
42          return new ThreadLocalDelegateExecutorService<C>(manager, delegate);
43      }
44  
45      /**
46       * Creates a scheduled executor service that ensures the executed delegate instance runs in the same thread local
47       * context as the calling code.
48       *
49       * @param delegate The ScheduledExecutorService instance to delegate to
50       * @return The wrapping ScheduledExecutorService that manages thread local state transfer
51       */
52      public ScheduledExecutorService createScheduledExecutorService(ScheduledExecutorService delegate) {
53          return new ThreadLocalDelegateScheduledExecutorService<C>(manager, delegate);
54      }
55  
56      /**
57       * Creates a runnable that ensures the executed runnable instance runs in the same thread local context as the calling
58       * code
59       *
60       * @param delegate The runnable to delegate to
61       * @return The wrapping Runnable that manages thread local state transfer
62       */
63      public Runnable createRunnable(Runnable delegate) {
64          return new ThreadLocalDelegateRunnable<C>(manager, delegate);
65      }
66  
67      /**
68       * Creates a callable that ensures the executed runnable instance runs in the same thread local context as the calling
69       * code.
70       *
71       * @param delegate The callable to delegate to
72       * @param <T>      The type that the callable returns
73       * @return The wrapping Callable that manages thread local state transfer
74       */
75      public <T> Callable<T> createCallable(Callable<T> delegate) {
76          return new ThreadLocalDelegateCallable<C, T>(manager, delegate);
77      }
78  }