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          if (LOGGER.isDebugEnabled()) {
90              LOGGER.debug("Delivering event {}", this);
91          }
92          
93          deliverEvent(this.nextFilter, this.session, this.type, this.status);
94      }
95  
96      /**
97       * Static method which effectively delivers the specified event to the next filter
98       * <code>nextFilter</code> on the <code>session</code>.
99       * 
100      * @param nextFilter the next filter
101      * @param session the session on which the event occured
102      * @param type the event type
103      * @param status the idle status should only be non null only if the event type is 
104      * {@link IoSessionEventType#IDLE} 
105      */
106     private static void deliverEvent(NextFilter nextFilter, IoSession session,
107             IoSessionEventType type, IdleStatus status) {
108         switch (type) {
109             case CREATED:
110                 nextFilter.sessionCreated(session);
111                 break;
112 
113             case OPENED:
114                 nextFilter.sessionOpened(session);
115                 break;
116             
117             case IDLE:
118                 nextFilter.sessionIdle(session, status);
119                 break;
120             
121             case CLOSED:
122                 nextFilter.sessionClosed(session);
123                 break;
124         }
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public String toString() {
132         StringBuilder sb = new StringBuilder(IoSessionEvent.class.getSimpleName());
133         sb.append('@');
134         sb.append(Integer.toHexString(hashCode()));
135         sb.append(" - [ ").append(session);
136         sb.append(", ").append(type);
137         sb.append(']');
138         
139         return sb.toString();
140     }
141 
142     /**
143      * @return the idle status of the event
144      */
145     public IdleStatus getStatus() {
146         return status;
147     }
148 
149     /**
150      * @return the next filter to which the event should be sent.
151      */
152     public NextFilter getNextFilter() {
153         return nextFilter;
154     }
155 
156     /**
157      * @return the session on which the event occurred.
158      */
159     public IoSession getSession() {
160         return session;
161     }
162 
163     /**
164      * @return the event type that occurred.
165      */
166     public IoSessionEventType getType() {
167         return type;
168     }
169 }