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