001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.filter.codec;
021
022import java.util.Queue;
023
024import org.apache.mina.core.buffer.IoBuffer;
025import org.apache.mina.core.filterchain.IoFilter.NextFilter;
026import org.apache.mina.core.future.DefaultWriteFuture;
027import org.apache.mina.core.future.WriteFuture;
028import org.apache.mina.core.session.DummySession;
029import org.apache.mina.core.session.IoSession;
030
031/**
032 * A virtual {@link IoSession} that provides {@link ProtocolEncoderOutput}
033 * and {@link ProtocolDecoderOutput}.  It is useful for unit-testing
034 * codec and reusing codec for non-network-use (e.g. serialization).
035 *
036 * <h2>Encoding</h2>
037 * <pre>
038 * ProtocolCodecSession session = new ProtocolCodecSession();
039 * ProtocolEncoder encoder = ...;
040 * MessageX in = ...;
041 *
042 * encoder.encode(session, in, session.getProtocolEncoderOutput());
043 *
044 * IoBuffer buffer = session.getProtocolDecoderOutputQueue().poll();
045 * </pre>
046 *
047 * <h2>Decoding</h2>
048 * <pre>
049 * ProtocolCodecSession session = new ProtocolCodecSession();
050 * ProtocolDecoder decoder = ...;
051 * IoBuffer in = ...;
052 *
053 * decoder.decode(session, in, session.getProtocolDecoderOutput());
054 *
055 * Object message = session.getProtocolDecoderOutputQueue().poll();
056 * </pre>
057 *
058 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
059 */
060public class ProtocolCodecSession extends DummySession {
061
062    private final WriteFuture notWrittenFuture = DefaultWriteFuture.newNotWrittenFuture(this,
063            new UnsupportedOperationException());
064
065    private final AbstractProtocolEncoderOutput encoderOutput = new AbstractProtocolEncoderOutput() {
066        public WriteFuture flush() {
067            return notWrittenFuture;
068        }
069    };
070
071    private final AbstractProtocolDecoderOutput decoderOutput = new AbstractProtocolDecoderOutput() {
072        public void flush(NextFilter nextFilter, IoSession session) {
073            // Do nothing
074        }
075    };
076
077    /**
078     * Creates a new instance.
079     */
080    public ProtocolCodecSession() {
081        // Do nothing
082    }
083
084    /**
085     * @return the {@link ProtocolEncoderOutput} that buffers
086     * {@link IoBuffer}s generated by {@link ProtocolEncoder}.
087     */
088    public ProtocolEncoderOutput getEncoderOutput() {
089        return encoderOutput;
090    }
091
092    /**
093     * @return the {@link Queue} of the buffered encoder output.
094     */
095    public Queue<Object> getEncoderOutputQueue() {
096        return encoderOutput.getMessageQueue();
097    }
098
099    /**
100     * @return the {@link ProtocolEncoderOutput} that buffers
101     * messages generated by {@link ProtocolDecoder}.
102     */
103    public ProtocolDecoderOutput getDecoderOutput() {
104        return decoderOutput;
105    }
106
107    /**
108     * @return the {@link Queue} of the buffered decoder output.
109     */
110    public Queue<Object> getDecoderOutputQueue() {
111        return decoderOutput.getMessageQueue();
112    }
113}