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