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.core.filterchain;
21  
22  import org.apache.mina.core.filterchain.IoFilter.NextFilter;
23  import org.apache.mina.core.session.IdleStatus;
24  import org.apache.mina.core.session.IoEvent;
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  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * An I/O event or an I/O request that MINA provides for {@link IoFilter}s.
33   * Most users won't need to use this class.  It is usually used by internal
34   * components to store I/O events.
35   *
36   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
37   */
38  public class IoFilterEvent extends IoEvent {
39      /** A logger for this class */
40      static Logger LOGGER = LoggerFactory.getLogger(IoFilterEvent.class);
41  
42      /** A speedup for logs */
43      static boolean DEBUG = LOGGER.isDebugEnabled();
44  
45      private final NextFilter nextFilter;
46  
47      public IoFilterEvent(NextFilter nextFilter, IoEventType type, IoSession session, Object parameter) {
48          super(type, session, parameter);
49  
50          if (nextFilter == null) {
51              throw new IllegalArgumentException("nextFilter must not be null");
52          }
53  
54          this.nextFilter = nextFilter;
55      }
56  
57      public NextFilter getNextFilter() {
58          return nextFilter;
59      }
60  
61      @Override
62      public void fire() {
63          IoSession session = getSession();
64          NextFilter nextFilter = getNextFilter();
65          IoEventType type = getType();
66  
67          if (DEBUG) {
68              LOGGER.debug("Firing a {} event for session {}", type, session.getId());
69          }
70  
71          switch (type) {
72          case MESSAGE_RECEIVED:
73              Object parameter = getParameter();
74              nextFilter.messageReceived(session, parameter);
75              break;
76  
77          case MESSAGE_SENT:
78              WriteRequest writeRequest = (WriteRequest) getParameter();
79              nextFilter.messageSent(session, writeRequest);
80              break;
81  
82          case WRITE:
83              writeRequest = (WriteRequest) getParameter();
84              nextFilter.filterWrite(session, writeRequest);
85              break;
86  
87          case CLOSE:
88              nextFilter.filterClose(session);
89              break;
90  
91          case EXCEPTION_CAUGHT:
92              Throwable throwable = (Throwable) getParameter();
93              nextFilter.exceptionCaught(session, throwable);
94              break;
95  
96          case SESSION_IDLE:
97              nextFilter.sessionIdle(session, (IdleStatus) getParameter());
98              break;
99  
100         case SESSION_OPENED:
101             nextFilter.sessionOpened(session);
102             break;
103 
104         case SESSION_CREATED:
105             nextFilter.sessionCreated(session);
106             break;
107 
108         case SESSION_CLOSED:
109             nextFilter.sessionClosed(session);
110             break;
111 
112         default:
113             throw new IllegalArgumentException("Unknown event type: " + type);
114         }
115 
116         if (DEBUG) {
117             LOGGER.debug("Event {} has been fired for session {}", type, session.getId());
118         }
119     }
120 }