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 final static 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(final NextFilter nextFilter, final IoSession session, final 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(final NextFilter nextFilter, final IoSession session, final 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(final NextFilter nextFilter, final IoSession session,
104             final IoSessionEventType type, final IdleStatus status) {
105         switch (type) {
106         case CREATED:
107             nextFilter.sessionCreated(session);
108             break;
109         case OPENED:
110             nextFilter.sessionOpened(session);
111             break;
112         case IDLE:
113             nextFilter.sessionIdle(session, status);
114             break;
115         case CLOSED:
116             nextFilter.sessionClosed(session);
117             break;
118         }
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     public String toString() {
126         StringBuilder sb = new StringBuilder(IoSessionEvent.class.getSimpleName());
127         sb.append('@');
128         sb.append(Integer.toHexString(hashCode()));
129         sb.append(" - [ ").append(session);
130         sb.append(", ").append(type);
131         sb.append(']');
132         return sb.toString();
133     }
134 
135     /**
136      * Returns the idle status of the event.
137      * 
138      * @return the idle status of the event
139      */
140     public IdleStatus getStatus() {
141         return status;
142     }
143 
144     /**
145      * Returns the next filter to which the event should be sent.
146      * 
147      * @return the next filter
148      */
149     public NextFilter getNextFilter() {
150         return nextFilter;
151     }
152 
153     /**
154      * Returns the session on which the event occured.
155      * 
156      * @return the session
157      */
158     public IoSession getSession() {
159         return session;
160     }
161 
162     /**
163      * Returns the event type that occured.
164      * 
165      * @return the event type
166      */
167     public IoSessionEventType getType() {
168         return type;
169     }
170 }