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
79 private static final Class<?>[] EMPTY_PARAMS = new Class[0];
80
81 public DemuxingProtocolDecoder() {
82
83 }
84
85 public void addMessageDecoder(Class<? extends MessageDecoder> decoderClass) {
86 if (decoderClass == null) {
87 throw new IllegalArgumentException("decoderClass");
88 }
89
90 try {
91 decoderClass.getConstructor(EMPTY_PARAMS);
92 } catch (NoSuchMethodException e) {
93 throw new IllegalArgumentException("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("Unregisterable type: " + decoderClass);
104 }
105 }
106
107 public void addMessageDecoder(MessageDecoder decoder) {
108 addMessageDecoder(new SingletonMessageDecoderFactory(decoder));
109 }
110
111 public void addMessageDecoder(MessageDecoderFactory factory) {
112 if (factory == null) {
113 throw new IllegalArgumentException("factory");
114 }
115 MessageDecoderFactory[] decoderFactories = this.decoderFactories;
116 MessageDecoderFactory[] newDecoderFactories = new MessageDecoderFactory[decoderFactories.length + 1];
117 System.arraycopy(decoderFactories, 0, newDecoderFactories, 0, decoderFactories.length);
118 newDecoderFactories[decoderFactories.length] = factory;
119 this.decoderFactories = newDecoderFactories;
120 }
121
122
123
124
125 @Override
126 protected boolean doDecode(IoSession session, IoBuffer in, 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("Unexpected decode result (see your decodable()): " + result);
154 }
155 }
156
157 if (undecodables == decoders.length) {
158
159 String dump = in.getHexDump();
160 in.position(in.limit());
161 ProtocolDecoderException e = new ProtocolDecoderException("No appropriate message decoder: " + dump);
162 e.setHexdump(dump);
163 throw e;
164 }
165
166 if (state.currentDecoder == null) {
167
168 return false;
169 }
170 }
171
172 try {
173 MessageDecoderResult result = state.currentDecoder.decode(session, in, out);
174 if (result == MessageDecoder.OK) {
175 state.currentDecoder = null;
176 return true;
177 } else if (result == MessageDecoder.NEED_DATA) {
178 return false;
179 } else if (result == MessageDecoder.NOT_OK) {
180 state.currentDecoder = null;
181 throw new ProtocolDecoderException("Message decoder returned NOT_OK.");
182 } else {
183 state.currentDecoder = null;
184 throw new IllegalStateException("Unexpected decode result (see your decode()): " + result);
185 }
186 } catch (Exception e) {
187 state.currentDecoder = null;
188 throw e;
189 }
190 }
191
192
193
194
195 @Override
196 public void finishDecode(IoSession session, ProtocolDecoderOutput out) 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
208
209
210 @Override
211 public void dispose(IoSession session) throws Exception {
212 super.dispose(session);
213 session.removeAttribute(STATE);
214 }
215
216 private State getState(IoSession session) throws Exception {
217 State state = (State) session.getAttribute(STATE);
218
219 if (state == null) {
220 state = new State();
221 State oldState = (State) session.setAttributeIfAbsent(STATE, state);
222
223 if (oldState != null) {
224 state = oldState;
225 }
226 }
227
228 return state;
229 }
230
231 private class State {
232 private final MessageDecoder[] decoders;
233
234 private MessageDecoder currentDecoder;
235
236 private State() throws Exception {
237 MessageDecoderFactory[] decoderFactories = DemuxingProtocolDecoder.this.decoderFactories;
238 decoders = new MessageDecoder[decoderFactories.length];
239 for (int i = decoderFactories.length - 1; i >= 0; i--) {
240 decoders[i] = decoderFactories[i].getDecoder();
241 }
242 }
243 }
244
245 private static class SingletonMessageDecoderFactory implements MessageDecoderFactory {
246 private final MessageDecoder decoder;
247
248 private SingletonMessageDecoderFactory(MessageDecoder decoder) {
249 if (decoder == null) {
250 throw new IllegalArgumentException("decoder");
251 }
252 this.decoder = decoder;
253 }
254
255 public MessageDecoder getDecoder() {
256 return decoder;
257 }
258 }
259
260 private static class DefaultConstructorMessageDecoderFactory implements MessageDecoderFactory {
261 private final Class<?> decoderClass;
262
263 private DefaultConstructorMessageDecoderFactory(Class<?> decoderClass) {
264 if (decoderClass == null) {
265 throw new IllegalArgumentException("decoderClass");
266 }
267
268 if (!MessageDecoder.class.isAssignableFrom(decoderClass)) {
269 throw new IllegalArgumentException("decoderClass is not assignable to MessageDecoder");
270 }
271 this.decoderClass = decoderClass;
272 }
273
274 public MessageDecoder getDecoder() throws Exception {
275 return (MessageDecoder) decoderClass.newInstance();
276 }
277 }
278 }