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