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      /** The IoEvent type */
33      private final IoEventType type;
34  
35      /** The associated IoSession */
36      private final IoSession session;
37  
38      /** The stored parameter */
39      private final Object parameter;
40  
41      /**
42       * Creates a new IoEvent
43       * 
44       * @param type The type of event to create
45       * @param session The associated IoSession
46       * @param parameter The parameter to add to the event
47       */
48      public IoEvent(IoEventType type, IoSession session, Object parameter) {
49          if (type == null) {
50              throw new IllegalArgumentException("type");
51          }
52          
53          if (session == null) {
54              throw new IllegalArgumentException("session");
55          }
56          
57          this.type = type;
58          this.session = session;
59          this.parameter = parameter;
60      }
61  
62      /**
63       * @return The IoEvent type
64       */
65      public IoEventType getType() {
66          return type;
67      }
68  
69      /**
70       * @return The associated IoSession
71       */
72      public IoSession getSession() {
73          return session;
74      }
75  
76      /**
77       * @return The stored parameter
78       */
79      public Object getParameter() {
80          return parameter;
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public void run() {
88          fire();
89      }
90  
91      /**
92       * Fire an event
93       */
94      public void fire() {
95          switch ( type ) {
96              case MESSAGE_RECEIVED:
97                  session.getFilterChain().fireMessageReceived(getParameter());
98                  break;
99                  
100             case MESSAGE_SENT:
101                 session.getFilterChain().fireMessageSent((WriteRequest) getParameter());
102                 break;
103                 
104             case WRITE:
105                 session.getFilterChain().fireFilterWrite((WriteRequest) getParameter());
106                 break;
107                 
108             case CLOSE:
109                 session.getFilterChain().fireFilterClose();
110                 break;
111                 
112             case EXCEPTION_CAUGHT:
113                 session.getFilterChain().fireExceptionCaught((Throwable) getParameter());
114                 break;
115                 
116             case SESSION_IDLE:
117                 session.getFilterChain().fireSessionIdle((IdleStatus) getParameter());
118                 break;
119                 
120             case SESSION_OPENED:
121                 session.getFilterChain().fireSessionOpened();
122                 break;
123                 
124             case SESSION_CREATED:
125                 session.getFilterChain().fireSessionCreated();
126                 break;
127                 
128             case SESSION_CLOSED:
129                 session.getFilterChain().fireSessionClosed();
130                 break;
131                 
132             default:
133                 throw new IllegalArgumentException("Unknown event type: " + getType());
134         }
135     }
136 
137     /**
138      * @see Object#toString()
139      */
140     @Override
141     public String toString() {
142         StringBuilder sb = new StringBuilder();
143         
144         sb.append('[');
145         sb.append(session);
146         sb.append(']');
147         sb.append(type.name());
148         
149         if (parameter != null) {
150             sb.append(':');
151             sb.append(parameter);
152         }
153 
154         return sb.toString();
155     }
156 }