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.future.DefaultWriteFuture; 26 import org.apache.mina.core.future.WriteFuture; 27 import org.apache.mina.core.session.DummySession; 28 import org.apache.mina.core.session.IoSession; 29 30 /** 31 * A virtual {@link IoSession} that provides {@link ProtocolEncoderOutput} 32 * and {@link ProtocolDecoderOutput}. It is useful for unit-testing 33 * codec and reusing codec for non-network-use (e.g. serialization). 34 * 35 * <h2>Encoding</h2> 36 * <pre> 37 * ProtocolCodecSession session = new ProtocolCodecSession(); 38 * ProtocolEncoder encoder = ...; 39 * MessageX in = ...; 40 * 41 * encoder.encode(session, in, session.getProtocolEncoderOutput()); 42 * 43 * IoBuffer buffer = session.getProtocolDecoderOutputQueue().poll(); 44 * </pre> 45 * 46 * <h2>Decoding</h2> 47 * <pre> 48 * ProtocolCodecSession session = new ProtocolCodecSession(); 49 * ProtocolDecoder decoder = ...; 50 * IoBuffer in = ...; 51 * 52 * decoder.decode(session, in, session.getProtocolDecoderOutput()); 53 * 54 * Object message = session.getProtocolDecoderOutputQueue().poll(); 55 * </pre> 56 * 57 * @author The Apache MINA Project (dev@mina.apache.org) 58 * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $ 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() { 75 } 76 }; 77 78 /** 79 * Creates a new instance. 80 */ 81 public ProtocolCodecSession() { 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 }