1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.concurrent;
20
21 import org.apache.shiro.subject.Subject;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.concurrent.*;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class SubjectAwareExecutorService extends SubjectAwareExecutor implements ExecutorService {
59
60 private ExecutorService targetExecutorService;
61
62 public SubjectAwareExecutorService() {
63 }
64
65 public SubjectAwareExecutorService(ExecutorService target) {
66 setTargetExecutorService(target);
67 }
68
69 public ExecutorService getTargetExecutorService() {
70 return targetExecutorService;
71 }
72
73 public void setTargetExecutorService(ExecutorService targetExecutorService) {
74 super.setTargetExecutor(targetExecutorService);
75 this.targetExecutorService = targetExecutorService;
76 }
77
78 @Override
79 public void setTargetExecutor(Executor targetExecutor) {
80 if (!(targetExecutor instanceof ExecutorService)) {
81 String msg = "The " + getClass().getName() + " implementation only accepts " +
82 ExecutorService.class.getName() + " target instances.";
83 throw new IllegalArgumentException(msg);
84 }
85 super.setTargetExecutor(targetExecutor);
86 }
87
88 public void shutdown() {
89 this.targetExecutorService.shutdown();
90 }
91
92 public List<Runnable> shutdownNow() {
93 return this.targetExecutorService.shutdownNow();
94 }
95
96 public boolean isShutdown() {
97 return this.targetExecutorService.isShutdown();
98 }
99
100 public boolean isTerminated() {
101 return this.targetExecutorService.isTerminated();
102 }
103
104 public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
105 return this.targetExecutorService.awaitTermination(timeout, unit);
106 }
107
108 protected <T> Callable<T> associateWithSubject(Callable<T> task) {
109 Subject subject = getSubject();
110 return subject.associateWith(task);
111 }
112
113 public <T> Future<T> submit(Callable<T> task) {
114 Callable<T> work = associateWithSubject(task);
115 return this.targetExecutorService.submit(work);
116 }
117
118 public <T> Future<T> submit(Runnable task, T result) {
119 Runnable work = associateWithSubject(task);
120 return this.targetExecutorService.submit(work, result);
121 }
122
123 public Future<?> submit(Runnable task) {
124 Runnable work = associateWithSubject(task);
125 return this.targetExecutorService.submit(work);
126 }
127
128 protected <T> Collection<Callable<T>> associateWithSubject(Collection<? extends Callable<T>> tasks) {
129 Collection<Callable<T>> workItems = new ArrayList<Callable<T>>(tasks.size());
130 for (Callable<T> task : tasks) {
131 Callable<T> work = associateWithSubject(task);
132 workItems.add(work);
133 }
134 return workItems;
135 }
136
137 public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
138 Collection<Callable<T>> workItems = associateWithSubject(tasks);
139 return this.targetExecutorService.invokeAll(workItems);
140 }
141
142 public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
143 throws InterruptedException {
144 Collection<Callable<T>> workItems = associateWithSubject(tasks);
145 return this.targetExecutorService.invokeAll(workItems, timeout, unit);
146 }
147
148 public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
149 Collection<Callable<T>> workItems = associateWithSubject(tasks);
150 return this.targetExecutorService.invokeAny(workItems);
151 }
152
153 public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
154 throws InterruptedException, ExecutionException, TimeoutException {
155 Collection<Callable<T>> workItems = associateWithSubject(tasks);
156 return this.targetExecutorService.invokeAny(workItems, timeout, unit);
157 }
158 }