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 The Apache MINA Project (dev@mina.apache.org)
59   * @version $Rev: 713125 $, $Date: 2008-11-11 20:32:26 +0100 (Tue, 11 Nov 2008) $
60   */
61  public class ProtocolCodecSession extends DummySession {
62  
63      private final WriteFuture notWrittenFuture =
64          DefaultWriteFuture.newNotWrittenFuture(this, new UnsupportedOperationException());
65  
66      private final AbstractProtocolEncoderOutput encoderOutput =
67          new AbstractProtocolEncoderOutput() {
68              public WriteFuture flush() {
69                  return notWrittenFuture;
70              }
71      };
72  
73      private final AbstractProtocolDecoderOutput decoderOutput =
74          new AbstractProtocolDecoderOutput() {
75              public void flush(NextFilter nextFilter, IoSession session) {
76              }
77      };
78  
79      /**
80       * Creates a new instance.
81       */
82      public ProtocolCodecSession() {
83      }
84  
85      /**
86       * Returns the {@link ProtocolEncoderOutput} that buffers
87       * {@link IoBuffer}s generated by {@link ProtocolEncoder}.
88       */
89      public ProtocolEncoderOutput getEncoderOutput() {
90          return encoderOutput;
91      }
92  
93      /**
94       * Returns the {@link Queue} of the buffered encoder output.
95       */
96      public Queue<Object> getEncoderOutputQueue() {
97          return encoderOutput.getMessageQueue();
98      }
99  
100     /**
101      * Returns the {@link ProtocolEncoderOutput} that buffers
102      * messages generated by {@link ProtocolDecoder}.
103      */
104     public ProtocolDecoderOutput getDecoderOutput() {
105         return decoderOutput;
106     }
107 
108     /**
109      * Returns the {@link Queue} of the buffered decoder output.
110      */
111     public Queue<Object> getDecoderOutputQueue() {
112         return decoderOutput.getMessageQueue();
113     }
114 }