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      private static final Logger LOGGER = LoggerFactory.getLogger(IoFilterEvent.class);
41  
42      /** A speedup for logs */
43      private static final boolean DEBUG = LOGGER.isDebugEnabled();
44  
45      private final NextFilter nextFilter;
46  
47      /**
48       * Creates a new IoFilterEvent instance
49       * 
50       * @param nextFilter The next Filter
51       * @param type The type of event
52       * @param session The current session
53       * @param parameter Any parameter
54       */
55      public IoFilterEvent(NextFilter nextFilter, IoEventType type, IoSession session, Object parameter) {
56          super(type, session, parameter);
57  
58          if (nextFilter == null) {
59              throw new IllegalArgumentException("nextFilter must not be null");
60          }
61  
62          this.nextFilter = nextFilter;
63      }
64  
65      /**
66       * @return The next filter
67       */
68      public NextFilter getNextFilter() {
69          return nextFilter;
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public void fire() {
77          IoSession session = getSession();
78          IoEventType type = getType();
79  
80          if (DEBUG) {
81              LOGGER.debug("Firing a {} event for session {}", type, session.getId());
82          }
83  
84          switch (type) {
85              case MESSAGE_RECEIVED:
86                  Object parameter = getParameter();
87                  nextFilter.messageReceived(session, parameter);
88                  break;
89      
90              case MESSAGE_SENT:
91                  WriteRequest writeRequest = (WriteRequest) getParameter();
92                  nextFilter.messageSent(session, writeRequest);
93                  break;
94      
95              case WRITE:
96                  writeRequest = (WriteRequest) getParameter();
97                  nextFilter.filterWrite(session, writeRequest);
98                  break;
99      
100             case CLOSE:
101                 nextFilter.filterClose(session);
102                 break;
103     
104             case EXCEPTION_CAUGHT:
105                 Throwable throwable = (Throwable) getParameter();
106                 nextFilter.exceptionCaught(session, throwable);
107                 break;
108     
109             case SESSION_IDLE:
110                 nextFilter.sessionIdle(session, (IdleStatus) getParameter());
111                 break;
112     
113             case SESSION_OPENED:
114                 nextFilter.sessionOpened(session);
115                 break;
116     
117             case SESSION_CREATED:
118                 nextFilter.sessionCreated(session);
119                 break;
120     
121             case SESSION_CLOSED:
122                 nextFilter.sessionClosed(session);
123                 break;
124     
125             default:
126                 throw new IllegalArgumentException("Unknown event type: " + type);
127         }
128 
129         if (DEBUG) {
130             LOGGER.debug("Event {} has been fired for session {}", type, session.getId());
131         }
132     }
133 }