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  import org.apache.mina.filter.FilterEvent;
24  
25  /**
26   * An I/O event or an I/O request that MINA provides.
27   * Most users won't need to use this class.  It is usually used by internal
28   * components to store I/O events.
29   *
30   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
31   */
32  public class IoEvent implements Runnable {
33      /** The IoEvent type */
34      private final IoEventType type;
35  
36      /** The associated IoSession */
37      private final IoSession session;
38  
39      /** The stored parameter */
40      private final Object parameter;
41  
42      /**
43       * Creates a new IoEvent
44       * 
45       * @param type The type of event to create
46       * @param session The associated IoSession
47       * @param parameter The parameter to add to the event
48       */
49      public IoEvent(IoEventType type, IoSession session, Object parameter) {
50          if (type == null) {
51              throw new IllegalArgumentException("type");
52          }
53          
54          if (session == null) {
55              throw new IllegalArgumentException("session");
56          }
57          
58          this.type = type;
59          this.session = session;
60          this.parameter = parameter;
61      }
62  
63      /**
64       * @return The IoEvent type
65       */
66      public IoEventType getType() {
67          return type;
68      }
69  
70      /**
71       * @return The associated IoSession
72       */
73      public IoSession getSession() {
74          return session;
75      }
76  
77      /**
78       * @return The stored parameter
79       */
80      public Object getParameter() {
81          return parameter;
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public void run() {
89          fire();
90      }
91  
92      /**
93       * Fire an event
94       */
95      public void fire() {
96          switch ( type ) {
97              case CLOSE:
98                  session.getFilterChain().fireFilterClose();
99                  break;
100 
101             case EVENT:
102                 session.getFilterChain().fireEvent((FilterEvent)getParameter());
103                 break;
104                 
105             case EXCEPTION_CAUGHT:
106                 session.getFilterChain().fireExceptionCaught((Throwable) getParameter());
107                 break;
108                 
109             case INPUT_CLOSED:
110                 session.getFilterChain().fireInputClosed();
111                 break;
112                 
113             case MESSAGE_RECEIVED:
114                 session.getFilterChain().fireMessageReceived(getParameter());
115                 break;
116                 
117             case MESSAGE_SENT:
118                 session.getFilterChain().fireMessageSent((WriteRequest) getParameter());
119                 break;
120                 
121             case SESSION_CLOSED:
122                 session.getFilterChain().fireSessionClosed();
123                 break;
124                 
125             case SESSION_CREATED:
126                 session.getFilterChain().fireSessionCreated();
127                 break;
128                 
129             case SESSION_IDLE:
130                 session.getFilterChain().fireSessionIdle((IdleStatus) getParameter());
131                 break;
132                 
133             case SESSION_OPENED:
134                 session.getFilterChain().fireSessionOpened();
135                 break;
136                 
137             case WRITE:
138                 session.getFilterChain().fireFilterWrite((WriteRequest) getParameter());
139                 break;
140                 
141             default:
142                 throw new IllegalArgumentException("Unknown event type: " + getType());
143         }
144     }
145 
146     /**
147      * @see Object#toString()
148      */
149     @Override
150     public String toString() {
151         StringBuilder sb = new StringBuilder();
152         
153         sb.append('[');
154         sb.append(session);
155         sb.append(']');
156         sb.append(type.name());
157         
158         if (parameter != null) {
159             sb.append(':');
160             sb.append(parameter);
161         }
162 
163         return sb.toString();
164     }
165 }