View Javadoc

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 =
63          DefaultWriteFuture.newNotWrittenFuture(this, new UnsupportedOperationException());
64  
65      private final AbstractProtocolEncoderOutput encoderOutput =
66          new AbstractProtocolEncoderOutput() {
67              public WriteFuture flush() {
68                  return notWrittenFuture;
69              }
70      };
71  
72      private final AbstractProtocolDecoderOutput decoderOutput =
73          new AbstractProtocolDecoderOutput() {
74              public void flush(NextFilter nextFilter, IoSession session) {
75                  // Do nothing
76              }
77      };
78  
79      /**
80       * Creates a new instance.
81       */
82      public ProtocolCodecSession() {
83          // Do nothing
84      }
85  
86      /**
87       * Returns the {@link ProtocolEncoderOutput} that buffers
88       * {@link IoBuffer}s generated by {@link ProtocolEncoder}.
89       */
90      public ProtocolEncoderOutput getEncoderOutput() {
91          return encoderOutput;
92      }
93  
94      /**
95       * Returns the {@link Queue} of the buffered encoder output.
96       */
97      public Queue<Object> getEncoderOutputQueue() {
98          return encoderOutput.getMessageQueue();
99      }
100 
101     /**
102      * Returns the {@link ProtocolEncoderOutput} that buffers
103      * messages generated by {@link ProtocolDecoder}.
104      */
105     public ProtocolDecoderOutput getDecoderOutput() {
106         return decoderOutput;
107     }
108 
109     /**
110      * Returns the {@link Queue} of the buffered decoder output.
111      */
112     public Queue<Object> getDecoderOutputQueue() {
113         return decoderOutput.getMessageQueue();
114     }
115 }