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      private final NextFilter nextFilter;
43  
44      public IoFilterEvent(NextFilter nextFilter, IoEventType type,
45              IoSession session, Object parameter) {
46          super(type, session, parameter);
47  
48          if (nextFilter == null) {
49              throw new NullPointerException("nextFilter");
50          }
51          this.nextFilter = nextFilter;
52      }
53  
54      public NextFilter getNextFilter() {
55          return nextFilter;
56      }
57  
58      @Override
59      public void fire() {
60          IoSession session = getSession();
61          NextFilter nextFilter = getNextFilter();
62          IoEventType type = getType();
63  
64          if ( LOGGER.isDebugEnabled()) {
65              LOGGER.debug( "Firing a {} event for session {}",type, session.getId() );
66          }
67  
68          switch (type) {
69          case MESSAGE_RECEIVED:
70              Object parameter = getParameter();
71              nextFilter.messageReceived(session, parameter);
72              break;
73          case MESSAGE_SENT:
74              WriteRequest writeRequest = (WriteRequest)getParameter();
75              nextFilter.messageSent(session, writeRequest);
76              break;
77          case WRITE:
78              writeRequest = (WriteRequest)getParameter();
79              nextFilter.filterWrite(session, writeRequest);
80              break;
81          case CLOSE:
82              nextFilter.filterClose(session);
83              break;
84          case EXCEPTION_CAUGHT:
85              Throwable throwable = (Throwable)getParameter();
86              nextFilter.exceptionCaught(session, throwable);
87              break;
88          case SESSION_IDLE:
89              nextFilter.sessionIdle(session, (IdleStatus) getParameter());
90              break;
91          case SESSION_OPENED:
92              nextFilter.sessionOpened(session);
93              break;
94          case SESSION_CREATED:
95              nextFilter.sessionCreated(session);
96              break;
97          case SESSION_CLOSED:
98              nextFilter.sessionClosed(session);
99              break;
100         default:
101             throw new IllegalArgumentException("Unknown event type: " + type);
102         }
103         
104         if ( LOGGER.isDebugEnabled()) {
105             LOGGER.debug( "Event {} has been fired for session {}", type, session.getId() );
106         }
107     }
108 }