1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.execution;
17
18
19 import java.util.concurrent.Executor;
20 import java.util.concurrent.ExecutorService;
21
22 import org.jboss.netty.util.ExternalResourceReleasable;
23
24
25
26
27
28 public class ChainedExecutor implements Executor, ExternalResourceReleasable {
29
30 private final Executor cur;
31 private final Executor next;
32 private final ChannelEventRunnableFilter filter;
33
34
35
36
37
38
39
40
41
42
43
44 public ChainedExecutor(ChannelEventRunnableFilter filter, Executor cur, Executor next) {
45 if (filter == null) {
46 throw new NullPointerException("filter");
47 }
48 if (cur == null) {
49 throw new NullPointerException("cur");
50 }
51 if (next == null) {
52 throw new NullPointerException("next");
53 }
54
55 this.filter = filter;
56 this.cur = cur;
57 this.next = next;
58 }
59
60
61
62
63
64
65 public void execute(Runnable command) {
66 assert command instanceof ChannelEventRunnable;
67 if (filter.filter((ChannelEventRunnable) command)) {
68 cur.execute(command);
69 } else {
70 next.execute(command);
71 }
72 }
73
74 public void releaseExternalResources() {
75 if (cur instanceof ExecutorService) {
76 ((ExecutorService) cur).shutdown();
77 }
78 if (next instanceof ExecutorService) {
79 ((ExecutorService) next).shutdown();
80 }
81 releaseExternal(cur);
82 releaseExternal(next);
83 }
84
85 private static void releaseExternal(Executor executor) {
86 if (executor instanceof ExternalResourceReleasable) {
87 ((ExternalResourceReleasable) executor).releaseExternalResources();
88 }
89 }
90 }