1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.mina.filter.codec;
21
22 import java.util.Queue;
23
24 import org.apache.mina.core.buffer.IoBuffer;
25 import org.apache.mina.core.filterchain.IoFilter.NextFilter;
26 import org.apache.mina.core.future.DefaultWriteFuture;
27 import org.apache.mina.core.future.WriteFuture;
28 import org.apache.mina.core.session.DummySession;
29 import org.apache.mina.core.session.IoSession;
30
31 /**
32 * A virtual {@link IoSession} that provides {@link ProtocolEncoderOutput}
33 * and {@link ProtocolDecoderOutput}. It is useful for unit-testing
34 * codec and reusing codec for non-network-use (e.g. serialization).
35 *
36 * <h2>Encoding</h2>
37 * <pre>
38 * ProtocolCodecSession session = new ProtocolCodecSession();
39 * ProtocolEncoder encoder = ...;
40 * MessageX in = ...;
41 *
42 * encoder.encode(session, in, session.getProtocolEncoderOutput());
43 *
44 * IoBuffer buffer = session.getProtocolDecoderOutputQueue().poll();
45 * </pre>
46 *
47 * <h2>Decoding</h2>
48 * <pre>
49 * ProtocolCodecSession session = new ProtocolCodecSession();
50 * ProtocolDecoder decoder = ...;
51 * IoBuffer in = ...;
52 *
53 * decoder.decode(session, in, session.getProtocolDecoderOutput());
54 *
55 * Object message = session.getProtocolDecoderOutputQueue().poll();
56 * </pre>
57 *
58 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
59 */
60 public class ProtocolCodecSession extends DummySession {
61
62 private final WriteFuture notWrittenFuture = DefaultWriteFuture.newNotWrittenFuture(this,
63 new UnsupportedOperationException());
64
65 private final AbstractProtocolEncoderOutput encoderOutput = new AbstractProtocolEncoderOutput() {
66 public WriteFuture flush() {
67 return notWrittenFuture;
68 }
69 };
70
71 private final AbstractProtocolDecoderOutput decoderOutput = new AbstractProtocolDecoderOutput() {
72 public void flush(NextFilter nextFilter, IoSession session) {
73 // Do nothing
74 }
75 };
76
77 /**
78 * Creates a new instance.
79 */
80 public ProtocolCodecSession() {
81 // Do nothing
82 }
83
84 /**
85 * Returns the {@link ProtocolEncoderOutput} that buffers
86 * {@link IoBuffer}s generated by {@link ProtocolEncoder}.
87 */
88 public ProtocolEncoderOutput getEncoderOutput() {
89 return encoderOutput;
90 }
91
92 /**
93 * Returns the {@link Queue} of the buffered encoder output.
94 */
95 public Queue<Object> getEncoderOutputQueue() {
96 return encoderOutput.getMessageQueue();
97 }
98
99 /**
100 * Returns the {@link ProtocolEncoderOutput} that buffers
101 * messages generated by {@link ProtocolDecoder}.
102 */
103 public ProtocolDecoderOutput getDecoderOutput() {
104 return decoderOutput;
105 }
106
107 /**
108 * Returns the {@link Queue} of the buffered decoder output.
109 */
110 public Queue<Object> getDecoderOutputQueue() {
111 return decoderOutput.getMessageQueue();
112 }
113 }