1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.common;
21
22 import java.util.concurrent.Executor;
23 import java.util.concurrent.ExecutorService;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.atomic.AtomicInteger;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public class SimpleIoProcessorPool<T extends AbstractIoSession> implements IoProcessor<T> {
74
75 private static final int DEFAULT_SIZE = Runtime.getRuntime().availableProcessors() + 1;
76 private static final AttributeKey PROCESSOR = new AttributeKey(SimpleIoProcessorPool.class, "processor");
77
78 private final Logger logger = LoggerFactory.getLogger(getClass());
79
80 private final IoProcessor<T>[] pool;
81 private final AtomicInteger processorDistributor = new AtomicInteger();
82 private final Executor executor;
83 private final boolean createdExecutor;
84
85 private final Object disposalLock = new Object();
86 private volatile boolean disposing;
87 private volatile boolean disposed;
88
89 public SimpleIoProcessorPool(Class<? extends IoProcessor<T>> processorType) {
90 this(processorType, null, DEFAULT_SIZE);
91 }
92
93 public SimpleIoProcessorPool(Class<? extends IoProcessor<T>> processorType, int size) {
94 this(processorType, null, size);
95 }
96
97 public SimpleIoProcessorPool(Class<? extends IoProcessor<T>> processorType, Executor executor) {
98 this(processorType, executor, DEFAULT_SIZE);
99 }
100
101 @SuppressWarnings("unchecked")
102 public SimpleIoProcessorPool(Class<? extends IoProcessor<T>> processorType, Executor executor, int size) {
103 if (processorType == null) {
104 throw new NullPointerException("processorType");
105 }
106 if (size <= 0) {
107 throw new IllegalArgumentException(
108 "size: " + size + " (expected: positive integer)");
109 }
110
111 if (executor == null) {
112 this.executor = executor = Executors.newCachedThreadPool();
113 this.createdExecutor = true;
114 } else {
115 this.executor = executor;
116 this.createdExecutor = false;
117 }
118
119 pool = new IoProcessor[size];
120
121 boolean success = false;
122 try {
123 for (int i = 0; i < pool.length; i ++) {
124 IoProcessor<T> processor = null;
125
126
127 try {
128 try {
129 processor = processorType.getConstructor(ExecutorService.class).newInstance(executor);
130 } catch (NoSuchMethodException e) {
131
132 }
133
134 if (processor == null) {
135 try {
136 processor = processorType.getConstructor(Executor.class).newInstance(executor);
137 } catch (NoSuchMethodException e) {
138
139 }
140 }
141
142 if (processor == null) {
143 try {
144 processor = processorType.getConstructor().newInstance();
145 } catch (NoSuchMethodException e) {
146
147 }
148 }
149 } catch (RuntimeException e) {
150 throw e;
151 } catch (Exception e) {
152 throw new RuntimeIoException(
153 "Failed to create a new instance of " + processorType.getName(), e);
154 }
155
156
157 if (processor == null) {
158 throw new IllegalArgumentException(
159 String.valueOf(processorType) + " must have a public constructor " +
160 "with one " + ExecutorService.class.getSimpleName() + " parameter, " +
161 "a public constructor with one " + Executor.class.getSimpleName() +
162 " parameter or a public default constructor.");
163 }
164
165 pool[i] = processor;
166 }
167
168 success = true;
169 } finally {
170 if (!success) {
171 dispose();
172 }
173 }
174 }
175
176 public final void add(T session) {
177 getProcessor(session).add(session);
178 }
179
180 public final void flush(T session) {
181 getProcessor(session).flush(session);
182 }
183
184 public final void remove(T session) {
185 getProcessor(session).remove(session);
186 }
187
188 public final void updateTrafficMask(T session) {
189 getProcessor(session).updateTrafficMask(session);
190 }
191
192 public boolean isDisposed() {
193 return disposed;
194 }
195
196 public boolean isDisposing() {
197 return disposing;
198 }
199
200 public final void dispose() {
201 if (disposed) {
202 return;
203 }
204
205 synchronized (disposalLock) {
206 if (!disposing) {
207 disposing = true;
208 for (int i = pool.length - 1; i >= 0; i --) {
209 if (pool[i] == null || pool[i].isDisposing()) {
210 continue;
211 }
212
213 try {
214 pool[i].dispose();
215 } catch (Exception e) {
216 logger.warn(
217 "Failed to dispose a " +
218 pool[i].getClass().getSimpleName() +
219 " at index " + i + ".", e);
220 } finally {
221 pool[i] = null;
222 }
223 }
224
225 if (createdExecutor) {
226 ((ExecutorService) executor).shutdown();
227 }
228 }
229 }
230
231 disposed = true;
232 }
233
234 @SuppressWarnings("unchecked")
235 private IoProcessor<T> getProcessor(T session) {
236 IoProcessor<T> p = (IoProcessor<T>) session.getAttribute(PROCESSOR);
237 if (p == null) {
238 p = nextProcessor();
239 IoProcessor<T> oldp =
240 (IoProcessor<T>) session.setAttributeIfAbsent(PROCESSOR, p);
241 if (oldp != null) {
242 p = oldp;
243 }
244 }
245
246 return p;
247 }
248
249 private IoProcessor<T> nextProcessor() {
250 checkDisposal();
251 return pool[Math.abs(processorDistributor.getAndIncrement()) % pool.length];
252 }
253
254 private void checkDisposal() {
255 if (disposed) {
256 throw new IllegalStateException("A disposed processor cannot be accessed.");
257 }
258 }
259 }