1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.core.service;
21
22 import java.lang.reflect.Constructor;
23 import java.util.Arrays;
24 import java.util.concurrent.Executor;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27
28 import org.apache.mina.core.RuntimeIoException;
29 import org.apache.mina.core.session.AbstractIoSession;
30 import org.apache.mina.core.session.AttributeKey;
31 import org.apache.mina.core.session.IoSession;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public class SimpleIoProcessorPool<S extends AbstractIoSession> implements IoProcessor<S> {
78
79 private final static Logger LOGGER = LoggerFactory.getLogger(SimpleIoProcessorPool.class);
80
81
82 private static final int DEFAULT_SIZE = Runtime.getRuntime().availableProcessors() + 1;
83
84
85 private static final AttributeKey PROCESSOR = new AttributeKey( SimpleIoProcessorPool.class, "processor");
86
87
88 private final IoProcessor<S>[] pool;
89
90
91 private final Executor executor;
92
93
94 private final boolean createdExecutor;
95
96
97 private final Object disposalLock = new Object();
98
99
100 private volatile boolean disposing;
101
102
103 private volatile boolean disposed;
104
105
106
107
108
109
110
111 public SimpleIoProcessorPool(Class<? extends IoProcessor<S>> processorType) {
112 this(processorType, null, DEFAULT_SIZE);
113 }
114
115
116
117
118
119
120
121
122 public SimpleIoProcessorPool(Class<? extends IoProcessor<S>> processorType, int size) {
123 this(processorType, null, size);
124 }
125
126
127
128
129
130
131
132 public SimpleIoProcessorPool(Class<? extends IoProcessor<S>> processorType, Executor executor) {
133 this(processorType, executor, DEFAULT_SIZE);
134 }
135
136
137
138
139
140
141
142
143 @SuppressWarnings("unchecked")
144 public SimpleIoProcessorPool(Class<? extends IoProcessor<S>> processorType,
145 Executor executor, int size) {
146 if (processorType == null) {
147 throw new IllegalArgumentException("processorType");
148 }
149
150 if (size <= 0) {
151 throw new IllegalArgumentException("size: " + size
152 + " (expected: positive integer)");
153 }
154
155
156 createdExecutor = (executor == null);
157
158 if (createdExecutor) {
159 this.executor = Executors.newCachedThreadPool();
160 } else {
161 this.executor = executor;
162 }
163
164 pool = new IoProcessor[size];
165
166 boolean success = false;
167 Constructor<? extends IoProcessor<S>> processorConstructor = null;
168 boolean usesExecutorArg = true;
169
170 try {
171
172 try {
173 try {
174 processorConstructor = processorType.getConstructor(ExecutorService.class);
175 pool[0] = processorConstructor.newInstance(this.executor);
176 } catch (NoSuchMethodException e1) {
177
178 try {
179 processorConstructor = processorType.getConstructor(Executor.class);
180 pool[0] = processorConstructor.newInstance(this.executor);
181 } catch (NoSuchMethodException e2) {
182
183 try {
184 processorConstructor = processorType.getConstructor();
185 usesExecutorArg = false;
186 pool[0] = processorConstructor.newInstance();
187 } catch (NoSuchMethodException e3) {
188
189 }
190 }
191 }
192 } catch (RuntimeException re) {
193 LOGGER.error("Cannot create an IoProcessor :{}", re.getMessage());
194 throw re;
195 } catch (Exception e) {
196 String msg = "Failed to create a new instance of " + processorType.getName() + ":" + e.getMessage();
197 LOGGER.error(msg, e);
198 throw new RuntimeIoException(msg , e);
199 }
200
201 if (processorConstructor == null) {
202
203 String msg = String.valueOf(processorType)
204 + " must have a public constructor with one "
205 + ExecutorService.class.getSimpleName()
206 + " parameter, a public constructor with one "
207 + Executor.class.getSimpleName()
208 + " parameter or a public default constructor.";
209 LOGGER.error(msg);
210 throw new IllegalArgumentException(msg);
211 }
212
213
214 for (int i = 1; i < pool.length; i++) {
215 try {
216 if (usesExecutorArg) {
217 pool[i] = processorConstructor.newInstance(this.executor);
218 } else {
219 pool[i] = processorConstructor.newInstance();
220 }
221 } catch (Exception e) {
222
223 }
224 }
225
226 success = true;
227 } finally {
228 if (!success) {
229 dispose();
230 }
231 }
232 }
233
234
235
236
237 public final void add(S session) {
238 getProcessor(session).add(session);
239 }
240
241
242
243
244 public final void flush(S session) {
245 getProcessor(session).flush(session);
246 }
247
248
249
250
251 public final void remove(S session) {
252 getProcessor(session).remove(session);
253 }
254
255
256
257
258 public final void updateTrafficControl(S session) {
259 getProcessor(session).updateTrafficControl(session);
260 }
261
262
263
264
265 public boolean isDisposed() {
266 return disposed;
267 }
268
269
270
271
272 public boolean isDisposing() {
273 return disposing;
274 }
275
276
277
278
279 public final void dispose() {
280 if (disposed) {
281 return;
282 }
283
284 synchronized (disposalLock) {
285 if (!disposing) {
286 disposing = true;
287
288 for (IoProcessor<S> ioProcessor : pool) {
289 if (ioProcessor == null) {
290
291 continue;
292 }
293
294 if (ioProcessor.isDisposing()) {
295 continue;
296 }
297
298 try {
299 ioProcessor.dispose();
300 } catch (Exception e) {
301 LOGGER.warn("Failed to dispose the {} IoProcessor.", ioProcessor.getClass().getSimpleName(), e);
302 }
303 }
304
305 if (createdExecutor) {
306 ((ExecutorService) executor).shutdown();
307 }
308 }
309
310 Arrays.fill(pool, null);
311 disposed = true;
312 }
313 }
314
315
316
317
318
319 @SuppressWarnings("unchecked")
320 private IoProcessor<S> getProcessor(S session) {
321 IoProcessor<S> processor = (IoProcessor<S>) session.getAttribute(PROCESSOR);
322
323 if (processor == null) {
324 if (disposed || disposing) {
325 throw new IllegalStateException("A disposed processor cannot be accessed.");
326 }
327
328 processor = pool[Math.abs((int) session.getId()) % pool.length];
329
330 if (processor == null) {
331 throw new IllegalStateException("A disposed processor cannot be accessed.");
332 }
333
334 session.setAttributeIfAbsent(PROCESSOR, processor);
335 }
336
337 return processor;
338 }
339 }