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: */
020: package org.apache.mina.filter.codec;
021:
022: import java.util.Queue;
023:
024: import org.apache.mina.common.DefaultWriteFuture;
025: import org.apache.mina.common.DummySession;
026: import org.apache.mina.common.IoBuffer;
027: import org.apache.mina.common.IoSession;
028: import org.apache.mina.common.WriteFuture;
029:
030: /**
031: * A virtual {@link IoSession} that provides {@link ProtocolEncoderOutput}
032: * and {@link ProtocolDecoderOutput}. It is useful for unit-testing
033: * codec and reusing codec for non-network-use (e.g. serialization).
034: *
035: * <h2>Encoding</h2>
036: * <pre>
037: * ProtocolCodecSession session = new ProtocolCodecSession();
038: * ProtocolEncoder encoder = ...;
039: * MessageX in = ...;
040: *
041: * encoder.encode(session, in, session.getProtocolEncoderOutput());
042: *
043: * IoBuffer buffer = session.getProtocolDecoderOutputQueue().poll();
044: * </pre>
045: *
046: * <h2>Decoding</h2>
047: * <pre>
048: * ProtocolCodecSession session = new ProtocolCodecSession();
049: * ProtocolDecoder decoder = ...;
050: * IoBuffer in = ...;
051: *
052: * decoder.decode(session, in, session.getProtocolDecoderOutput());
053: *
054: * Object message = session.getProtocolDecoderOutputQueue().poll();
055: * </pre>
056: *
057: * @author The Apache MINA Project (dev@mina.apache.org)
058: * @version $Rev: 607167 $, $Date: 2007-12-27 20:46:03 -0700 (Thu, 27 Dec 2007) $
059: */
060: public class ProtocolCodecSession extends DummySession {
061:
062: private final WriteFuture notWrittenFuture = DefaultWriteFuture
063: .newNotWrittenFuture(this ,
064: new UnsupportedOperationException());
065:
066: private final AbstractProtocolEncoderOutput encoderOutput = new AbstractProtocolEncoderOutput() {
067: public WriteFuture flush() {
068: return notWrittenFuture;
069: }
070: };
071:
072: private final AbstractProtocolDecoderOutput decoderOutput = new AbstractProtocolDecoderOutput() {
073: public void flush() {
074: }
075: };
076:
077: /**
078: * Creates a new instance.
079: */
080: public ProtocolCodecSession() {
081: }
082:
083: /**
084: * Returns the {@link ProtocolEncoderOutput} that buffers
085: * {@link IoBuffer}s generated by {@link ProtocolEncoder}.
086: */
087: public ProtocolEncoderOutput getEncoderOutput() {
088: return encoderOutput;
089: }
090:
091: /**
092: * Returns the {@link Queue} of the buffered encoder output.
093: */
094: public Queue<Object> getEncoderOutputQueue() {
095: return encoderOutput.getMessageQueue();
096: }
097:
098: /**
099: * Returns the {@link ProtocolEncoderOutput} that buffers
100: * messages generated by {@link ProtocolDecoder}.
101: */
102: public ProtocolDecoderOutput getDecoderOutput() {
103: return decoderOutput;
104: }
105:
106: /**
107: * Returns the {@link Queue} of the buffered decoder output.
108: */
109: public Queue<Object> getDecoderOutputQueue() {
110: return decoderOutput.getMessageQueue();
111: }
112: }
|