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.proxy.event;
021
022import org.apache.mina.core.filterchain.IoFilter.NextFilter;
023import org.apache.mina.core.session.IdleStatus;
024import org.apache.mina.core.session.IoSession;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028/**
029 * IoSessionEvent.java - Wrapper Class for enqueued events.
030 * 
031 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
032 * @since MINA 2.0.0-M3
033 */
034public class IoSessionEvent {
035    private final static Logger logger = LoggerFactory.getLogger(IoSessionEvent.class);
036
037    /**
038     * The next filter in the chain.
039     */
040    private final NextFilter nextFilter;
041
042    /**
043     * The session.
044     */
045    private final IoSession session;
046
047    /**
048     * The event type.
049     */
050    private final IoSessionEventType type;
051
052    /**
053     * The idle status if type value is {@link IoSessionEventType#IDLE},
054     * null otherwise.
055     */
056    private IdleStatus status;
057
058    /**
059     * Creates an instance of this class when event type differs from 
060     * {@link IoSessionEventType#IDLE}.
061     * 
062     * @param nextFilter the next filter
063     * @param session the session
064     * @param type the event type
065     */
066    public IoSessionEvent(final NextFilter nextFilter, final IoSession session, final IoSessionEventType type) {
067        this.nextFilter = nextFilter;
068        this.session = session;
069        this.type = type;
070    }
071
072    /**
073     * Creates an instance of this class when event type is 
074     * {@link IoSessionEventType#IDLE}.
075     * 
076     * @param nextFilter the next filter
077     * @param session the session
078     * @param status the idle status
079     */
080    public IoSessionEvent(final NextFilter nextFilter, final IoSession session, final IdleStatus status) {
081        this(nextFilter, session, IoSessionEventType.IDLE);
082        this.status = status;
083    }
084
085    /**
086     * Delivers this event to the next filter.
087     */
088    public void deliverEvent() {
089        logger.debug("Delivering event {}", this);
090        deliverEvent(this.nextFilter, this.session, this.type, this.status);
091    }
092
093    /**
094     * Static method which effectively delivers the specified event to the next filter
095     * <code>nextFilter</code> on the <code>session</code>.
096     * 
097     * @param nextFilter the next filter
098     * @param session the session on which the event occured
099     * @param type the event type
100     * @param status the idle status should only be non null only if the event type is 
101     * {@link IoSessionEventType#IDLE} 
102     */
103    private static void deliverEvent(final NextFilter nextFilter, final IoSession session,
104            final IoSessionEventType type, final IdleStatus status) {
105        switch (type) {
106        case CREATED:
107            nextFilter.sessionCreated(session);
108            break;
109        case OPENED:
110            nextFilter.sessionOpened(session);
111            break;
112        case IDLE:
113            nextFilter.sessionIdle(session, status);
114            break;
115        case CLOSED:
116            nextFilter.sessionClosed(session);
117            break;
118        }
119    }
120
121    /**
122     * {@inheritDoc}
123     */
124    @Override
125    public String toString() {
126        StringBuilder sb = new StringBuilder(IoSessionEvent.class.getSimpleName());
127        sb.append('@');
128        sb.append(Integer.toHexString(hashCode()));
129        sb.append(" - [ ").append(session);
130        sb.append(", ").append(type);
131        sb.append(']');
132        return sb.toString();
133    }
134
135    /**
136     * @return the idle status of the event
137     */
138    public IdleStatus getStatus() {
139        return status;
140    }
141
142    /**
143     * @return the next filter to which the event should be sent.
144     */
145    public NextFilter getNextFilter() {
146        return nextFilter;
147    }
148
149    /**
150     * @return the session on which the event occured.
151     */
152    public IoSession getSession() {
153        return session;
154    }
155
156    /**
157     * @return the event type that occured.
158     */
159    public IoSessionEventType getType() {
160        return type;
161    }
162}