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.common;
21  
22  /**
23   * An I/O event or an I/O request that MINA provides.
24   * Most users won't need to use this class.  It is usually used by internal
25   * components to store I/O events.
26   *
27   * @author The Apache MINA Project (dev@mina.apache.org)
28   * @version $Rev: 592965 $, $Date: 2007-11-07 17:15:00 -0700 (Wed, 07 Nov 2007) $
29   */
30  public class IoEvent implements Runnable {
31      private final IoEventType type;
32  
33      private final IoSession session;
34  
35      private final Object parameter;
36  
37      public IoEvent(IoEventType type, IoSession session, Object parameter) {
38          if (type == null) {
39              throw new NullPointerException("type");
40          }
41          if (session == null) {
42              throw new NullPointerException("session");
43          }
44          this.type = type;
45          this.session = session;
46          this.parameter = parameter;
47      }
48  
49      public IoEventType getType() {
50          return type;
51      }
52  
53      public IoSession getSession() {
54          return session;
55      }
56  
57      public Object getParameter() {
58          return parameter;
59      }
60      
61      public void run() {
62          fire();
63      }
64  
65      public void fire() {
66          switch (getType()) {
67          case MESSAGE_RECEIVED:
68              getSession().getFilterChain().fireMessageReceived(getParameter());
69              break;
70          case MESSAGE_SENT:
71              getSession().getFilterChain().fireMessageSent((WriteRequest) getParameter());
72              break;
73          case WRITE:
74              getSession().getFilterChain().fireFilterWrite((WriteRequest) getParameter());
75              break;
76          case SET_TRAFFIC_MASK:
77              getSession().getFilterChain().fireFilterSetTrafficMask((TrafficMask) getParameter());
78              break;
79          case CLOSE:
80              getSession().getFilterChain().fireFilterClose();
81              break;
82          case EXCEPTION_CAUGHT:
83              getSession().getFilterChain().fireExceptionCaught((Throwable) getParameter());
84              break;
85          case SESSION_IDLE:
86              getSession().getFilterChain().fireSessionIdle((IdleStatus) getParameter());
87              break;
88          case SESSION_OPENED:
89              getSession().getFilterChain().fireSessionOpened();
90              break;
91          case SESSION_CREATED:
92              getSession().getFilterChain().fireSessionCreated();
93              break;
94          case SESSION_CLOSED:
95              getSession().getFilterChain().fireSessionClosed();
96              break;
97          default:
98              throw new IllegalArgumentException("Unknown event type: " + getType());
99          }
100     }
101 
102     @Override
103     public String toString() {
104         if (getParameter() == null) {
105             return "[" + getSession() + "] " + getType().name();
106         } else {
107             return "[" + getSession() + "] " + getType().name() + ": "
108                     + getParameter();
109         }
110     }
111 }