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.handler.chain;
021
022import org.apache.mina.core.session.IoSession;
023
024/**
025 * <p>A {@link IoHandlerCommand} encapsulates a unit of processing work to be
026 * performed, whose purpose is to examine and/or modify the state of a
027 * transaction that is represented by custom attributes provided by
028 * {@link IoSession}.  Individual {@link IoHandlerCommand}s can be assembled into
029 * a {@link IoHandlerChain}, which allows them to either complete the
030 * required processing or delegate further processing to the next
031 * {@link IoHandlerCommand} in the {@link IoHandlerChain}.</p>
032 *
033 * <p>{@link IoHandlerCommand} implementations typically retrieve and store state
034 * information in the {@link IoSession} that is passed as a parameter to
035 * the {@link #execute(NextCommand,IoSession,Object)} method, using custom
036 * session attributes.  If you think getting attributes is tedious process,
037 * you can create a bean which contains getters and setters of all properties
038 * and store the bean as a session attribute:</p>
039 *
040 * <pre>
041 * public class MyContext {
042 *   public String getPropertyX() { ... };
043 *   public void setPropertyX(String propertyX) { ... };
044 *   public int getPropertyZ() { ... };
045 *   public void setPropertyZ(int propertyZ) { ... };
046 * }
047 *
048 * public class MyHandlderCommand implements IoHandlerCommand {
049 *   public void execute( NextCommand next, IoSession session, Object message ) throws Exception {
050 *     MyContext ctx = session.getAttribute( "mycontext" );
051 *     ...
052 *   }
053 * }
054 * </pre>
055 *
056 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
057 */
058public interface IoHandlerCommand {
059    /**
060     * <p>Execute a unit of processing work to be performed.  This
061     * {@link IoHandlerCommand} may either complete the required processing
062     * and just return to stop the processing, or delegate remaining
063     * processing to the next {@link IoHandlerCommand} in a {@link IoHandlerChain}
064     * containing this {@link IoHandlerCommand} by calling
065     * {@link NextCommand#execute(IoSession,Object)}.
066     *
067     * @param next an indirect reference to the next {@link IoHandlerCommand} that
068     *             provides a way to forward the request to the next {@link IoHandlerCommand}.
069     * @param session the {@link IoSession} which is associated with
070     *                this request
071     * @param message the message object of this request
072     *
073     * @exception Exception general purpose exception return
074     *                      to indicate abnormal termination
075     */
076    void execute(NextCommand next, IoSession session, Object message) throws Exception;
077
078    /**
079     * Represents an indirect reference to the next {@link IoHandlerCommand} of
080     * the {@link IoHandlerChain}.  This interface provides a way to forward
081     * the request to the next {@link IoHandlerCommand}.
082     *
083     * @author <a href="http://mina.apache.org">Apache MINA Project</a>
084     */
085    interface NextCommand {
086        /**
087         * Forwards the request to the next {@link IoHandlerCommand} in the
088         * {@link IoHandlerChain}.
089         * 
090         * @param session The current session
091         * @param message The message to pass on
092         * @throws Exception If anything went wrong
093         */
094        void execute(IoSession session, Object message) throws Exception;
095    }
096}