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 * Only provided to provide backwards compatibility. To be removed in SAL 2.11
27 * @deprecated use {@link DefaultThreadLocalDelegateExecutorFactory#DefaultThreadLocalDelegateExecutorFactory(com.atlassian.sal.api.executor.ThreadLocalContextManager)}}
28 */
29 protected DefaultThreadLocalDelegateExecutorFactory(com.atlassian.sal.core.executor.ThreadLocalContextManager<C> manager)
30 {
31 this.manager = checkNotNull(manager);
32 }
33
34 /**
35 * Creates an executor that ensures the executed delegate instance runs in the same thread local context as the calling
36 * code.
37 *
38 * @param delegate The Executor instance to delegate to
39 * @return The wrapping executor that manages thread local state transfer
40 */
41 public Executor createExecutor(Executor delegate)
42 {
43 return new ThreadLocalDelegateExecutor<C>(manager, delegate);
44 }
45
46 /**
47 * Creates an executor service that ensures the executed delegate instance runs in the same thread local context as the
48 * calling code.
49 *
50 * @param delegate The ExecutorService instance to delegate to
51 * @return The wrapping ExecutorService that manages thread local state transfer
52 */
53 public ExecutorService createExecutorService(ExecutorService delegate)
54 {
55 return new ThreadLocalDelegateExecutorService<C>(manager, delegate);
56 }
57
58 /**
59 * Creates a scheduled executor service that ensures the executed delegate instance runs in the same thread local
60 * context as the calling code.
61 *
62 * @param delegate The ScheduledExecutorService instance to delegate to
63 * @return The wrapping ScheduledExecutorService that manages thread local state transfer
64 */
65 public ScheduledExecutorService createScheduledExecutorService(ScheduledExecutorService delegate)
66 {
67 return new ThreadLocalDelegateScheduledExecutorService<C>(manager, delegate);
68 }
69
70 /**
71 * Creates a runnable that ensures the executed runnable instance runs in the same thread local context as the calling
72 * code
73 *
74 * @param delegate The runnable to delegate to
75 * @return The wrapping Runnable that manages thread local state transfer
76 */
77 public Runnable createRunnable(Runnable delegate)
78 {
79 return new ThreadLocalDelegateRunnable<C>(manager, delegate);
80 }
81
82 /**
83 * Creates a callable that ensures the executed runnable instance runs in the same thread local context as the calling
84 * code.
85 *
86 * @param delegate The callable to delegate to
87 * @param <T> The type that the callable returns
88 * @return The wrapping Callable that manages thread local state transfer
89 */
90 public <T> Callable<T> createCallable(Callable<T> delegate)
91 {
92 return new ThreadLocalDelegateCallable<C, T>(manager, delegate);
93 }
94 }