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