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;
21
22 import org.apache.mina.common.ByteBuffer;
23 import org.apache.mina.common.ByteBufferProxy;
24 import org.apache.mina.common.IoFilter;
25 import org.apache.mina.common.IoFilterAdapter;
26 import org.apache.mina.common.IoFilterChain;
27 import org.apache.mina.common.IoSession;
28 import org.apache.mina.common.WriteFuture;
29 import org.apache.mina.common.support.DefaultWriteFuture;
30 import org.apache.mina.filter.codec.support.SimpleProtocolDecoderOutput;
31 import org.apache.mina.filter.codec.support.SimpleProtocolEncoderOutput;
32 import org.apache.mina.util.SessionLog;
33
34
35
36
37
38
39
40
41
42 public class ProtocolCodecFilter extends IoFilterAdapter {
43 public static final String ENCODER = ProtocolCodecFilter.class.getName()
44 + ".encoder";
45
46 public static final String DECODER = ProtocolCodecFilter.class.getName()
47 + ".decoder";
48
49 private static final String DECODER_OUT = ProtocolCodecFilter.class.getName()
50 + ".decoderOut";
51
52 private static final Class<?>[] EMPTY_PARAMS = new Class[0];
53
54 private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.wrap(new byte[0]);
55
56 private final ProtocolCodecFactory factory;
57
58 public ProtocolCodecFilter(ProtocolCodecFactory factory) {
59 if (factory == null) {
60 throw new NullPointerException("factory");
61 }
62 this.factory = factory;
63 }
64
65 public ProtocolCodecFilter(final ProtocolEncoder encoder,
66 final ProtocolDecoder decoder) {
67 if (encoder == null) {
68 throw new NullPointerException("encoder");
69 }
70 if (decoder == null) {
71 throw new NullPointerException("decoder");
72 }
73
74 this.factory = new ProtocolCodecFactory() {
75 public ProtocolEncoder getEncoder() {
76 return encoder;
77 }
78
79 public ProtocolDecoder getDecoder() {
80 return decoder;
81 }
82 };
83 }
84
85 public ProtocolCodecFilter(
86 final Class<? extends ProtocolEncoder> encoderClass,
87 final Class<? extends ProtocolDecoder> decoderClass) {
88 if (encoderClass == null) {
89 throw new NullPointerException("encoderClass");
90 }
91 if (decoderClass == null) {
92 throw new NullPointerException("decoderClass");
93 }
94 if (!ProtocolEncoder.class.isAssignableFrom(encoderClass)) {
95 throw new IllegalArgumentException("encoderClass: "
96 + encoderClass.getName());
97 }
98 if (!ProtocolDecoder.class.isAssignableFrom(decoderClass)) {
99 throw new IllegalArgumentException("decoderClass: "
100 + decoderClass.getName());
101 }
102 try {
103 encoderClass.getConstructor(EMPTY_PARAMS);
104 } catch (NoSuchMethodException e) {
105 throw new IllegalArgumentException(
106 "encoderClass doesn't have a public default constructor.");
107 }
108 try {
109 decoderClass.getConstructor(EMPTY_PARAMS);
110 } catch (NoSuchMethodException e) {
111 throw new IllegalArgumentException(
112 "decoderClass doesn't have a public default constructor.");
113 }
114
115 this.factory = new ProtocolCodecFactory() {
116 public ProtocolEncoder getEncoder() throws Exception {
117 return encoderClass.newInstance();
118 }
119
120 public ProtocolDecoder getDecoder() throws Exception {
121 return decoderClass.newInstance();
122 }
123 };
124 }
125
126 @Override
127 public void onPreAdd(IoFilterChain parent, String name,
128 NextFilter nextFilter) throws Exception {
129 if (parent.contains(ProtocolCodecFilter.class)) {
130 throw new IllegalStateException(
131 "A filter chain cannot contain more than one ProtocolCodecFilter.");
132 }
133 }
134
135 public void onPostRemove(IoFilterChain parent, String name,
136 NextFilter nextFilter) throws Exception {
137 disposeEncoder(parent.getSession());
138 disposeDecoder(parent.getSession());
139 disposeDecoderOut(parent.getSession());
140 }
141
142 @Override
143 public void messageReceived(NextFilter nextFilter, IoSession session,
144 Object message) throws Exception {
145 if (!(message instanceof ByteBuffer)) {
146 nextFilter.messageReceived(session, message);
147 return;
148 }
149
150 ByteBuffer in = (ByteBuffer) message;
151 ProtocolDecoder decoder = getDecoder(session);
152 ProtocolDecoderOutput decoderOut = getDecoderOut(session, nextFilter);
153
154 try {
155 synchronized (decoderOut) {
156 decoder.decode(session, in, decoderOut);
157 }
158 } catch (Throwable t) {
159 ProtocolDecoderException pde;
160 if (t instanceof ProtocolDecoderException) {
161 pde = (ProtocolDecoderException) t;
162 } else {
163 pde = new ProtocolDecoderException(t);
164 }
165 pde.setHexdump(in.getHexDump());
166 throw pde;
167 } finally {
168
169 if (session.getTransportType().isConnectionless()) {
170 disposeDecoder(session);
171 }
172
173
174 in.release();
175
176 decoderOut.flush();
177 }
178 }
179
180 @Override
181 public void messageSent(NextFilter nextFilter, IoSession session,
182 Object message) throws Exception {
183 if (message instanceof HiddenByteBuffer) {
184 return;
185 }
186
187 if (!(message instanceof MessageByteBuffer)) {
188 nextFilter.messageSent(session, message);
189 return;
190 }
191
192 nextFilter.messageSent(session, ((MessageByteBuffer) message).message);
193 }
194
195 @Override
196 public void filterWrite(NextFilter nextFilter, IoSession session,
197 WriteRequest writeRequest) throws Exception {
198 Object message = writeRequest.getMessage();
199 if (message instanceof ByteBuffer) {
200 nextFilter.filterWrite(session, writeRequest);
201 return;
202 }
203
204 ProtocolEncoder encoder = getEncoder(session);
205 ProtocolEncoderOutputImpl encoderOut = getEncoderOut(session,
206 nextFilter, writeRequest);
207
208 try {
209 encoder.encode(session, message, encoderOut);
210 encoderOut.flush();
211 nextFilter.filterWrite(session, new WriteRequest(
212 new MessageByteBuffer(writeRequest.getMessage()),
213 writeRequest.getFuture(), writeRequest.getDestination()));
214 } catch (Throwable t) {
215 ProtocolEncoderException pee;
216 if (t instanceof ProtocolEncoderException) {
217 pee = (ProtocolEncoderException) t;
218 } else {
219 pee = new ProtocolEncoderException(t);
220 }
221 throw pee;
222 } finally {
223
224 if (session.getTransportType().isConnectionless()) {
225 disposeEncoder(session);
226 }
227 }
228 }
229
230 @Override
231 public void sessionClosed(NextFilter nextFilter, IoSession session)
232 throws Exception {
233
234 ProtocolDecoder decoder = getDecoder(session);
235 ProtocolDecoderOutput decoderOut = getDecoderOut(session, nextFilter);
236 try {
237 decoder.finishDecode(session, decoderOut);
238 } catch (Throwable t) {
239 ProtocolDecoderException pde;
240 if (t instanceof ProtocolDecoderException) {
241 pde = (ProtocolDecoderException) t;
242 } else {
243 pde = new ProtocolDecoderException(t);
244 }
245 throw pde;
246 } finally {
247
248 disposeEncoder(session);
249 disposeDecoder(session);
250 disposeDecoderOut(session);
251 decoderOut.flush();
252 }
253
254 nextFilter.sessionClosed(session);
255 }
256
257 private ProtocolEncoder getEncoder(IoSession session) throws Exception {
258 ProtocolEncoder encoder = (ProtocolEncoder) session
259 .getAttribute(ENCODER);
260 if (encoder == null) {
261 encoder = factory.getEncoder();
262 session.setAttribute(ENCODER, encoder);
263 }
264 return encoder;
265 }
266
267 private ProtocolEncoderOutputImpl getEncoderOut(IoSession session,
268 NextFilter nextFilter, WriteRequest writeRequest) {
269 return new ProtocolEncoderOutputImpl(session, nextFilter, writeRequest);
270 }
271
272 private ProtocolDecoder getDecoder(IoSession session) throws Exception {
273 ProtocolDecoder decoder = (ProtocolDecoder) session
274 .getAttribute(DECODER);
275 if (decoder == null) {
276 decoder = factory.getDecoder();
277 session.setAttribute(DECODER, decoder);
278 }
279 return decoder;
280 }
281
282 private ProtocolDecoderOutput getDecoderOut(IoSession session,
283 NextFilter nextFilter) {
284 ProtocolDecoderOutput out = (ProtocolDecoderOutput) session.getAttribute(DECODER_OUT);
285 if (out == null) {
286 out = new SimpleProtocolDecoderOutput(session, nextFilter);
287 session.setAttribute(DECODER_OUT, out);
288 }
289 return out;
290 }
291
292 private void disposeEncoder(IoSession session) {
293 ProtocolEncoder encoder = (ProtocolEncoder) session
294 .removeAttribute(ENCODER);
295 if (encoder == null) {
296 return;
297 }
298
299 try {
300 encoder.dispose(session);
301 } catch (Throwable t) {
302 SessionLog.warn(session, "Failed to dispose: "
303 + encoder.getClass().getName() + " (" + encoder + ')');
304 }
305 }
306
307 private void disposeDecoder(IoSession session) {
308 ProtocolDecoder decoder = (ProtocolDecoder) session
309 .removeAttribute(DECODER);
310 if (decoder == null) {
311 return;
312 }
313
314 try {
315 decoder.dispose(session);
316 } catch (Throwable t) {
317 SessionLog.warn(session, "Falied to dispose: "
318 + decoder.getClass().getName() + " (" + decoder + ')');
319 }
320 }
321
322 private void disposeDecoderOut(IoSession session) {
323 session.removeAttribute(DECODER_OUT);
324 }
325
326 private static class HiddenByteBuffer extends ByteBufferProxy {
327 private HiddenByteBuffer(ByteBuffer buf) {
328 super(buf);
329 }
330 }
331
332 private static class MessageByteBuffer extends ByteBufferProxy {
333 private final Object message;
334
335 private MessageByteBuffer(Object message) {
336 super(EMPTY_BUFFER);
337 this.message = message;
338 }
339
340 @Override
341 public void acquire() {
342
343 }
344
345 @Override
346 public void release() {
347
348 }
349 }
350
351 private static class ProtocolEncoderOutputImpl extends
352 SimpleProtocolEncoderOutput {
353 private final IoSession session;
354
355 private final NextFilter nextFilter;
356
357 private final WriteRequest writeRequest;
358
359 ProtocolEncoderOutputImpl(IoSession session, NextFilter nextFilter,
360 WriteRequest writeRequest) {
361 this.session = session;
362 this.nextFilter = nextFilter;
363 this.writeRequest = writeRequest;
364 }
365
366 @Override
367 protected WriteFuture doFlush(ByteBuffer buf) {
368 WriteFuture future = new DefaultWriteFuture(session);
369 nextFilter.filterWrite(session, new WriteRequest(
370 new HiddenByteBuffer(buf), future, writeRequest
371 .getDestination()));
372 return future;
373 }
374 }
375 }