View Javadoc

    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.util;
    21
    22  import org.apache.mina.core.filterchain.IoFilterAdapter;
    23  import org.apache.mina.core.filterchain.IoFilterEvent;
    24  import org.apache.mina.core.session.IdleStatus;
    25  import org.apache.mina.core.session.IoEventType;
    26  import org.apache.mina.core.session.IoSession;
    27  import org.apache.mina.core.write.WriteRequest;
    28
    29  /**
    30   * Extend this class when you want to create a filter that
    31   * wraps the same logic around all 9 IoEvents
    32   *
    33   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    34   */
    35  public abstract class CommonEventFilter extends IoFilterAdapter {
    36
    37      public CommonEventFilter() {
    38          // Do nothing
    39      }
    40
    41      protected abstract void filter(IoFilterEvent event) throws Exception;
    42
    43      @Override
    44      public final void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception {
    45          filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_CREATED, session, null));
    46      }
    47
    48      @Override
    49      public final void sessionOpened(NextFilter nextFilter, IoSession session) throws Exception {
    50          filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_OPENED, session, null));
    51      }
    52
    53      @Override
    54      public final void sessionClosed(NextFilter nextFilter, IoSession session) throws Exception {
    55          filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_CLOSED, session, null));
    56      }
    57
    58      @Override
    59      public final void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) throws Exception {
    60          filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_IDLE, session, status));
    61      }
    62
    63      @Override
    64      public final void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) throws Exception {
    65          filter(new IoFilterEvent(nextFilter, IoEventType.EXCEPTION_CAUGHT, session, cause));
    66      }
    67
    68      @Override
    69      public final void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
    70          filter(new IoFilterEvent(nextFilter, IoEventType.MESSAGE_RECEIVED, session, message));
    71      }
    72
    73      @Override
    74      public final void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
    75          filter(new IoFilterEvent(nextFilter, IoEventType.MESSAGE_SENT, session, writeRequest));
    76      }
    77
    78      @Override
    79      public final void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
    80          filter(new IoFilterEvent(nextFilter, IoEventType.WRITE, session, writeRequest));
    81      }
    82
    83      @Override
    84      public final void filterClose(NextFilter nextFilter, IoSession session) throws Exception {
    85          filter(new IoFilterEvent(nextFilter, IoEventType.CLOSE, session, null));
    86      }
    87  }