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.common.IdleStatus;
23  import org.apache.mina.common.IoEventType;
24  import org.apache.mina.common.IoFilterAdapter;
25  import org.apache.mina.common.IoFilterEvent;
26  import org.apache.mina.common.IoSession;
27  import org.apache.mina.common.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  public abstract class CommonEventFilter extends IoFilterAdapter {
34  
35      public CommonEventFilter() {
36      }
37  
38      protected abstract void filter(IoFilterEvent event) throws Exception;
39  
40      @Override
41      public final void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception {
42          filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_CREATED, session, null));
43      }
44  
45      @Override
46      public final void sessionOpened(NextFilter nextFilter, IoSession session) throws Exception {
47          filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_OPENED, session, null));
48      }
49  
50      @Override
51      public final void sessionClosed(NextFilter nextFilter, IoSession session) throws Exception {
52          filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_CLOSED, session, null));
53      }
54  
55      @Override
56      public final void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) throws Exception {
57          filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_IDLE, session, status));
58      }
59  
60      @Override
61      public final void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) throws Exception {
62          filter(new IoFilterEvent(nextFilter, IoEventType.EXCEPTION_CAUGHT, session, cause));
63      }
64  
65      @Override
66      public final void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
67          filter(new IoFilterEvent(nextFilter, IoEventType.MESSAGE_RECEIVED, session, message));
68      }
69  
70      @Override
71      public final void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
72          filter(new IoFilterEvent(nextFilter, IoEventType.MESSAGE_SENT, session, writeRequest));
73      }
74  
75      @Override
76      public final void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
77          filter(new IoFilterEvent(nextFilter, IoEventType.WRITE, session, writeRequest));
78      }
79  
80      @Override
81      public final void filterClose(NextFilter nextFilter, IoSession session) throws Exception {
82          filter(new IoFilterEvent(nextFilter, IoEventType.CLOSE, session, null));
83      }
84  }