1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.filter.codec.demux;
21
22 import org.apache.mina.core.buffer.IoBuffer;
23 import org.apache.mina.core.session.AttributeKey;
24 import org.apache.mina.core.session.IoSession;
25 import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
26 import org.apache.mina.filter.codec.ProtocolDecoder;
27 import org.apache.mina.filter.codec.ProtocolDecoderException;
28 import org.apache.mina.filter.codec.ProtocolDecoderOutput;
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 DemuxingProtocolDecoder extends CumulativeProtocolDecoder {
74
75 private final AttributeKey STATE = new AttributeKey(getClass(), "state");
76
77 private MessageDecoderFactory[] decoderFactories = new MessageDecoderFactory[0];
78 private static final Class<?>[] EMPTY_PARAMS = new Class[0];
79
80 public DemuxingProtocolDecoder() {
81
82 }
83
84 public void addMessageDecoder(Class<? extends MessageDecoder> decoderClass) {
85 if (decoderClass == null) {
86 throw new NullPointerException("decoderClass");
87 }
88
89 try {
90 decoderClass.getConstructor(EMPTY_PARAMS);
91 } catch (NoSuchMethodException e) {
92 throw new IllegalArgumentException(
93 "The specified class doesn't have a public default constructor.");
94 }
95
96 boolean registered = false;
97 if (MessageDecoder.class.isAssignableFrom(decoderClass)) {
98 addMessageDecoder(new DefaultConstructorMessageDecoderFactory(decoderClass));
99 registered = true;
100 }
101
102 if (!registered) {
103 throw new IllegalArgumentException(
104 "Unregisterable type: " + decoderClass);
105 }
106 }
107
108 public void addMessageDecoder(MessageDecoder decoder) {
109 addMessageDecoder(new SingletonMessageDecoderFactory(decoder));
110 }
111
112 public void addMessageDecoder(MessageDecoderFactory factory) {
113 if (factory == null) {
114 throw new NullPointerException("factory");
115 }
116 MessageDecoderFactory[] decoderFactories = this.decoderFactories;
117 MessageDecoderFactory[] newDecoderFactories = new MessageDecoderFactory[decoderFactories.length + 1];
118 System.arraycopy(decoderFactories, 0, newDecoderFactories, 0,
119 decoderFactories.length);
120 newDecoderFactories[decoderFactories.length] = factory;
121 this.decoderFactories = newDecoderFactories;
122 }
123
124 @Override
125 protected boolean doDecode(IoSession session, IoBuffer in,
126 ProtocolDecoderOutput out) throws Exception {
127 State state = getState(session);
128
129 if (state.currentDecoder == null) {
130 MessageDecoder[] decoders = state.decoders;
131 int undecodables = 0;
132
133 for (int i = decoders.length - 1; i >= 0; i--) {
134 MessageDecoder decoder = decoders[i];
135 int limit = in.limit();
136 int pos = in.position();
137
138 MessageDecoderResult result;
139
140 try {
141 result = decoder.decodable(session, in);
142 } finally {
143 in.position(pos);
144 in.limit(limit);
145 }
146
147 if (result == MessageDecoder.OK) {
148 state.currentDecoder = decoder;
149 break;
150 } else if (result == MessageDecoder.NOT_OK) {
151 undecodables++;
152 } else if (result != MessageDecoder.NEED_DATA) {
153 throw new IllegalStateException(
154 "Unexpected decode result (see your decodable()): "
155 + result);
156 }
157 }
158
159 if (undecodables == decoders.length) {
160
161 String dump = in.getHexDump();
162 in.position(in.limit());
163 ProtocolDecoderException e = new ProtocolDecoderException(
164 "No appropriate message decoder: " + dump);
165 e.setHexdump(dump);
166 throw e;
167 }
168
169 if (state.currentDecoder == null) {
170
171 return false;
172 }
173 }
174
175 MessageDecoderResult result = state.currentDecoder.decode(session, in,
176 out);
177 if (result == MessageDecoder.OK) {
178 state.currentDecoder = null;
179 return true;
180 } else if (result == MessageDecoder.NEED_DATA) {
181 return false;
182 } else if (result == MessageDecoder.NOT_OK) {
183 state.currentDecoder = null;
184 throw new ProtocolDecoderException(
185 "Message decoder returned NOT_OK.");
186 } else {
187 state.currentDecoder = null;
188 throw new IllegalStateException(
189 "Unexpected decode result (see your decode()): "
190 + result);
191 }
192 }
193
194 @Override
195 public void finishDecode(IoSession session, ProtocolDecoderOutput out)
196 throws Exception {
197 super.finishDecode(session, out);
198 State state = getState(session);
199 MessageDecoder currentDecoder = state.currentDecoder;
200 if (currentDecoder == null) {
201 return;
202 }
203
204 currentDecoder.finishDecode(session, out);
205 }
206
207 @Override
208 public void dispose(IoSession session) throws Exception {
209 super.dispose(session);
210 session.removeAttribute(STATE);
211 }
212
213 private State getState(IoSession session) throws Exception {
214 State state = (State) session.getAttribute(STATE);
215
216 if (state == null) {
217 state = new State();
218 State oldState = (State) session.setAttributeIfAbsent(STATE, state);
219
220 if (oldState != null) {
221 state = oldState;
222 }
223 }
224
225 return state;
226 }
227
228 private class State {
229 private final MessageDecoder[] decoders;
230 private MessageDecoder currentDecoder;
231
232 private State() throws Exception {
233 MessageDecoderFactory[] decoderFactories = DemuxingProtocolDecoder.this.decoderFactories;
234 decoders = new MessageDecoder[decoderFactories.length];
235 for (int i = decoderFactories.length - 1; i >= 0; i--) {
236 decoders[i] = decoderFactories[i].getDecoder();
237 }
238 }
239 }
240
241 private static class SingletonMessageDecoderFactory implements
242 MessageDecoderFactory {
243 private final MessageDecoder decoder;
244
245 private SingletonMessageDecoderFactory(MessageDecoder decoder) {
246 if (decoder == null) {
247 throw new NullPointerException("decoder");
248 }
249 this.decoder = decoder;
250 }
251
252 public MessageDecoder getDecoder() {
253 return decoder;
254 }
255 }
256
257 private static class DefaultConstructorMessageDecoderFactory implements
258 MessageDecoderFactory {
259 private final Class<?> decoderClass;
260
261 private DefaultConstructorMessageDecoderFactory(Class<?> decoderClass) {
262 if (decoderClass == null) {
263 throw new NullPointerException("decoderClass");
264 }
265
266 if (!MessageDecoder.class.isAssignableFrom(decoderClass)) {
267 throw new IllegalArgumentException(
268 "decoderClass is not assignable to MessageDecoder");
269 }
270 this.decoderClass = decoderClass;
271 }
272
273 public MessageDecoder getDecoder() throws Exception {
274 return (MessageDecoder) decoderClass.newInstance();
275 }
276 }
277 }