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 <a href="http://mina.apache.org">Apache MINA Project</a>
30   */
31  public class IoEvent implements Runnable {
32      private final IoEventType type;
33  
34      private final IoSession session;
35  
36      private final Object parameter;
37  
38      public IoEvent(IoEventType type, IoSession session, Object parameter) {
39          if (type == null) {
40              throw new IllegalArgumentException("type");
41          }
42          if (session == null) {
43              throw new IllegalArgumentException("session");
44          }
45          this.type = type;
46          this.session = session;
47          this.parameter = parameter;
48      }
49  
50      public IoEventType getType() {
51          return type;
52      }
53  
54      public IoSession getSession() {
55          return session;
56      }
57  
58      public Object getParameter() {
59          return parameter;
60      }
61  
62      public void run() {
63          fire();
64      }
65  
66      public void fire() {
67          switch (getType()) {
68          case MESSAGE_RECEIVED:
69              getSession().getFilterChain().fireMessageReceived(getParameter());
70              break;
71          case MESSAGE_SENT:
72              getSession().getFilterChain().fireMessageSent((WriteRequest) getParameter());
73              break;
74          case WRITE:
75              getSession().getFilterChain().fireFilterWrite((WriteRequest) getParameter());
76              break;
77          case CLOSE:
78              getSession().getFilterChain().fireFilterClose();
79              break;
80          case EXCEPTION_CAUGHT:
81              getSession().getFilterChain().fireExceptionCaught((Throwable) getParameter());
82              break;
83          case SESSION_IDLE:
84              getSession().getFilterChain().fireSessionIdle((IdleStatus) getParameter());
85              break;
86          case SESSION_OPENED:
87              getSession().getFilterChain().fireSessionOpened();
88              break;
89          case SESSION_CREATED:
90              getSession().getFilterChain().fireSessionCreated();
91              break;
92          case SESSION_CLOSED:
93              getSession().getFilterChain().fireSessionClosed();
94              break;
95          default:
96              throw new IllegalArgumentException("Unknown event type: " + getType());
97          }
98      }
99  
100     @Override
101     public String toString() {
102         if (getParameter() == null) {
103             return "[" + getSession() + "] " + getType().name();
104         }
105 
106         return "[" + getSession() + "] " + getType().name() + ": " + getParameter();
107     }
108 }