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      protected abstract void filter(IoFilterEvent event) throws Exception;
37  
38      /**
39       * {@inheritDoc}
40       */
41      @Override
42      public final void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception {
43          filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_CREATED, session, null));
44      }
45  
46      /**
47       * {@inheritDoc}
48       */
49      @Override
50      public final void sessionOpened(NextFilter nextFilter, IoSession session) throws Exception {
51          filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_OPENED, session, null));
52      }
53  
54      /**
55       * {@inheritDoc}
56       */
57      @Override
58      public final void sessionClosed(NextFilter nextFilter, IoSession session) throws Exception {
59          filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_CLOSED, session, null));
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public final void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) throws Exception {
67          filter(new IoFilterEvent(nextFilter, IoEventType.SESSION_IDLE, session, status));
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public final void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) throws Exception {
75          filter(new IoFilterEvent(nextFilter, IoEventType.EXCEPTION_CAUGHT, session, cause));
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public final void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
83          filter(new IoFilterEvent(nextFilter, IoEventType.MESSAGE_RECEIVED, session, message));
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public final void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
91          filter(new IoFilterEvent(nextFilter, IoEventType.MESSAGE_SENT, session, writeRequest));
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public final void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
99          filter(new IoFilterEvent(nextFilter, IoEventType.WRITE, session, writeRequest));
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public final void filterClose(NextFilter nextFilter, IoSession session) throws Exception {
107         filter(new IoFilterEvent(nextFilter, IoEventType.CLOSE, session, null));
108     }
109 }