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.filter.util;
021
022import org.apache.mina.core.filterchain.IoFilterAdapter;
023import org.apache.mina.core.filterchain.IoFilterEvent;
024import org.apache.mina.core.session.IdleStatus;
025import org.apache.mina.core.session.IoEventType;
026import org.apache.mina.core.session.IoSession;
027import org.apache.mina.core.write.WriteRequest;
028
029/**
030 * Extend this class when you want to create a filter that
031 * wraps the same logic around all 9 IoEvents
032 * 
033 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
034 */
035public abstract class CommonEventFilter extends IoFilterAdapter {
036
037    public CommonEventFilter() {
038        // Do nothing
039    }
040
041    protected abstract void filter(IoFilterEvent event) throws Exception;
042
043    @Override
044    public final void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception {
045        filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_CREATED, session, null));
046    }
047
048    @Override
049    public final void sessionOpened(NextFilter nextFilter, IoSession session) throws Exception {
050        filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_OPENED, session, null));
051    }
052
053    @Override
054    public final void sessionClosed(NextFilter nextFilter, IoSession session) throws Exception {
055        filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_CLOSED, session, null));
056    }
057
058    @Override
059    public final void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) throws Exception {
060        filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_IDLE, session, status));
061    }
062
063    @Override
064    public final void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) throws Exception {
065        filter(new IoFilterEvent(nextFilter, IoEventType.EXCEPTION_CAUGHT, session, cause));
066    }
067
068    @Override
069    public final void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
070        filter(new IoFilterEvent(nextFilter, IoEventType.MESSAGE_RECEIVED, session, message));
071    }
072
073    @Override
074    public final void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
075        filter(new IoFilterEvent(nextFilter, IoEventType.MESSAGE_SENT, session, writeRequest));
076    }
077
078    @Override
079    public final void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
080        filter(new IoFilterEvent(nextFilter, IoEventType.WRITE, session, writeRequest));
081    }
082
083    @Override
084    public final void filterClose(NextFilter nextFilter, IoSession session) throws Exception {
085        filter(new IoFilterEvent(nextFilter, IoEventType.CLOSE, session, null));
086    }
087}