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