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.proxy.event;
21  
22  import org.apache.mina.core.filterchain.IoFilter.NextFilter;
23  import org.apache.mina.core.session.IdleStatus;
24  import org.apache.mina.core.session.IoSession;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  /**
29   * IoSessionEvent.java - Wrapper Class for enqueued events.
30   * 
31   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
32   * @since MINA 2.0.0-M3
33   */
34  public class IoSessionEvent {
35      private static final Logger logger = LoggerFactory.getLogger(IoSessionEvent.class);
36  
37      /**
38       * The next filter in the chain.
39       */
40      private final NextFilter nextFilter;
41  
42      /**
43       * The session.
44       */
45      private final IoSession session;
46  
47      /**
48       * The event type.
49       */
50      private final IoSessionEventType type;
51  
52      /**
53       * The idle status if type value is {@link IoSessionEventType#IDLE},
54       * null otherwise.
55       */
56      private IdleStatus status;
57  
58      /**
59       * Creates an instance of this class when event type differs from 
60       * {@link IoSessionEventType#IDLE}.
61       * 
62       * @param nextFilter the next filter
63       * @param session the session
64       * @param type the event type
65       */
66      public IoSessionEvent(NextFilter nextFilter, IoSession session, IoSessionEventType type) {
67          this.nextFilter = nextFilter;
68          this.session = session;
69          this.type = type;
70      }
71  
72      /**
73       * Creates an instance of this class when event type is 
74       * {@link IoSessionEventType#IDLE}.
75       * 
76       * @param nextFilter the next filter
77       * @param session the session
78       * @param status the idle status
79       */
80      public IoSessionEvent(NextFilter nextFilter, IoSession session, IdleStatus status) {
81          this(nextFilter, session, IoSessionEventType.IDLE);
82          this.status = status;
83      }
84  
85      /**
86       * Delivers this event to the next filter.
87       */
88      public void deliverEvent() {
89          logger.debug("Delivering event {}", this);
90          deliverEvent(this.nextFilter, this.session, this.type, this.status);
91      }
92  
93      /**
94       * Static method which effectively delivers the specified event to the next filter
95       * <code>nextFilter</code> on the <code>session</code>.
96       * 
97       * @param nextFilter the next filter
98       * @param session the session on which the event occured
99       * @param type the event type
100      * @param status the idle status should only be non null only if the event type is 
101      * {@link IoSessionEventType#IDLE} 
102      */
103     private static void deliverEvent(NextFilter nextFilter, IoSession session,
104             IoSessionEventType type, IdleStatus status) {
105         switch (type) {
106             case CREATED:
107                 nextFilter.sessionCreated(session);
108                 break;
109 
110             case OPENED:
111                 nextFilter.sessionOpened(session);
112                 break;
113             
114             case IDLE:
115                 nextFilter.sessionIdle(session, status);
116                 break;
117             
118             case CLOSED:
119                 nextFilter.sessionClosed(session);
120                 break;
121         }
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public String toString() {
129         StringBuilder sb = new StringBuilder(IoSessionEvent.class.getSimpleName());
130         sb.append('@');
131         sb.append(Integer.toHexString(hashCode()));
132         sb.append(" - [ ").append(session);
133         sb.append(", ").append(type);
134         sb.append(']');
135         
136         return sb.toString();
137     }
138 
139     /**
140      * @return the idle status of the event
141      */
142     public IdleStatus getStatus() {
143         return status;
144     }
145 
146     /**
147      * @return the next filter to which the event should be sent.
148      */
149     public NextFilter getNextFilter() {
150         return nextFilter;
151     }
152 
153     /**
154      * @return the session on which the event occurred.
155      */
156     public IoSession getSession() {
157         return session;
158     }
159 
160     /**
161      * @return the event type that occurred.
162      */
163     public IoSessionEventType getType() {
164         return type;
165     }
166 }