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