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