| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
package org.apache.qpid.proton.engine.impl; |
| 22 |
|
| 23 |
import java.util.Iterator; |
| 24 |
|
| 25 |
import org.apache.qpid.proton.engine.Connection; |
| 26 |
import org.apache.qpid.proton.engine.Delivery; |
| 27 |
import org.apache.qpid.proton.engine.Event; |
| 28 |
import org.apache.qpid.proton.engine.EventType; |
| 29 |
import org.apache.qpid.proton.engine.Handler; |
| 30 |
import org.apache.qpid.proton.engine.HandlerException; |
| 31 |
import org.apache.qpid.proton.engine.Link; |
| 32 |
import org.apache.qpid.proton.engine.Receiver; |
| 33 |
import org.apache.qpid.proton.engine.Record; |
| 34 |
import org.apache.qpid.proton.engine.Sender; |
| 35 |
import org.apache.qpid.proton.engine.Session; |
| 36 |
import org.apache.qpid.proton.engine.Transport; |
| 37 |
import org.apache.qpid.proton.reactor.Reactor; |
| 38 |
import org.apache.qpid.proton.reactor.Selectable; |
| 39 |
import org.apache.qpid.proton.reactor.Task; |
| 40 |
import org.apache.qpid.proton.reactor.impl.ReactorImpl; |
| 41 |
|
| 42 |
/** |
| 43 |
* EventImpl |
| 44 |
* |
| 45 |
*/ |
| 46 |
|
| 47 |
class EventImpl implements Event |
| 48 |
{ |
| 49 |
|
| 50 |
EventType type; |
| 51 |
Object context; |
| 52 |
EventImpl next; |
| 53 |
RecordImpl attachments = new RecordImpl(); |
| 54 |
|
| 55 |
EventImpl() |
| 56 |
{ |
| 57 |
this.type = null; |
| 58 |
} |
| 59 |
|
| 60 |
void init(EventType type, Object context) |
| 61 |
{ |
| 62 |
this.type = type; |
| 63 |
this.context = context; |
| 64 |
this.attachments.clear(); |
| 65 |
} |
| 66 |
|
| 67 |
void clear() |
| 68 |
{ |
| 69 |
type = null; |
| 70 |
context = null; |
| 71 |
attachments.clear(); |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public EventType getEventType() |
| 76 |
{ |
| 77 |
return type; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public Type getType() { |
| 82 |
if (type instanceof Type) { |
| 83 |
return (Type)type; |
| 84 |
} |
| 85 |
return Type.NON_CORE_EVENT; |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public Object getContext() |
| 90 |
{ |
| 91 |
return context; |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public Handler getRootHandler() { |
| 96 |
return ReactorImpl.ROOT.get(this); |
| 97 |
} |
| 98 |
|
| 99 |
private Handler delegated = null; |
| 100 |
|
| 101 |
@Override |
| 102 |
public void dispatch(Handler handler) throws HandlerException |
| 103 |
{ |
| 104 |
Handler old_delegated = delegated; |
| 105 |
try { |
| 106 |
delegated = handler; |
| 107 |
try { |
| 108 |
handler.handle(this); |
| 109 |
} catch(HandlerException handlerException) { |
| 110 |
throw handlerException; |
| 111 |
} catch(RuntimeException runtimeException) { |
| 112 |
throw new HandlerException(handler, runtimeException); |
| 113 |
} |
| 114 |
delegate(); |
| 115 |
} finally { |
| 116 |
delegated = old_delegated; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public void delegate() throws HandlerException |
| 122 |
{ |
| 123 |
if (delegated == null) { |
| 124 |
return; // short circuit |
| 125 |
} |
| 126 |
Iterator<Handler> children = delegated.children(); |
| 127 |
delegated = null; |
| 128 |
while(children.hasNext()) { |
| 129 |
dispatch(children.next()); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public void redispatch(EventType as_type, Handler handler) throws HandlerException |
| 135 |
{ |
| 136 |
if (!as_type.isValid()) { |
| 137 |
throw new IllegalArgumentException("Can only redispatch valid event types"); |
| 138 |
} |
| 139 |
EventType old = type; |
| 140 |
try { |
| 141 |
type = as_type; |
| 142 |
dispatch(handler); |
| 143 |
} |
| 144 |
finally { |
| 145 |
type = old; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public Connection getConnection() |
| 151 |
{ |
| 152 |
if (context instanceof Connection) { |
| 153 |
return (Connection) context; |
| 154 |
} else if (context instanceof Transport) { |
| 155 |
Transport transport = getTransport(); |
| 156 |
if (transport == null) { |
| 157 |
return null; |
| 158 |
} |
| 159 |
return ((TransportImpl) transport).getConnectionImpl(); |
| 160 |
} else { |
| 161 |
Session ssn = getSession(); |
| 162 |
if (ssn == null) { |
| 163 |
return null; |
| 164 |
} |
| 165 |
return ssn.getConnection(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
public Session getSession() |
| 171 |
{ |
| 172 |
if (context instanceof Session) { |
| 173 |
return (Session) context; |
| 174 |
} else { |
| 175 |
Link link = getLink(); |
| 176 |
if (link == null) { |
| 177 |
return null; |
| 178 |
} |
| 179 |
return link.getSession(); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public Link getLink() |
| 185 |
{ |
| 186 |
if (context instanceof Link) { |
| 187 |
return (Link) context; |
| 188 |
} else { |
| 189 |
Delivery dlv = getDelivery(); |
| 190 |
if (dlv == null) { |
| 191 |
return null; |
| 192 |
} |
| 193 |
return dlv.getLink(); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public Sender getSender() |
| 199 |
{ |
| 200 |
if (context instanceof Sender) { |
| 201 |
return (Sender) context; |
| 202 |
} else { |
| 203 |
Link link = getLink(); |
| 204 |
if (link instanceof Sender) { |
| 205 |
return (Sender) link; |
| 206 |
} |
| 207 |
return null; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
@Override |
| 212 |
public Receiver getReceiver() |
| 213 |
{ |
| 214 |
if (context instanceof Receiver) { |
| 215 |
return (Receiver) context; |
| 216 |
} else { |
| 217 |
Link link = getLink(); |
| 218 |
if (link instanceof Receiver) { |
| 219 |
return (Receiver) link; |
| 220 |
} |
| 221 |
return null; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public Delivery getDelivery() |
| 227 |
{ |
| 228 |
if (context instanceof Delivery) { |
| 229 |
return (Delivery) context; |
| 230 |
} else { |
| 231 |
return null; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
@Override |
| 236 |
public Transport getTransport() |
| 237 |
{ |
| 238 |
if (context instanceof Transport) { |
| 239 |
return (Transport) context; |
| 240 |
} else if (context instanceof Connection) { |
| 241 |
return ((Connection)context).getTransport(); |
| 242 |
} else { |
| 243 |
Session session = getSession(); |
| 244 |
if (session == null) { |
| 245 |
return null; |
| 246 |
} |
| 247 |
|
| 248 |
Connection connection = session.getConnection(); |
| 249 |
if (connection == null) { |
| 250 |
return null; |
| 251 |
} |
| 252 |
|
| 253 |
return connection.getTransport(); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
@Override |
| 258 |
public Selectable getSelectable() { |
| 259 |
if (context instanceof Selectable) { |
| 260 |
return (Selectable) context; |
| 261 |
} else { |
| 262 |
return null; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
@Override |
| 267 |
public Reactor getReactor() { |
| 268 |
if (context instanceof Reactor) { |
| 269 |
return (Reactor) context; |
| 270 |
} else if (context instanceof Task) { |
| 271 |
return ((Task)context).getReactor(); |
| 272 |
} else if (context instanceof Transport) { |
| 273 |
return ((TransportImpl)context).getReactor(); |
| 274 |
} else if (context instanceof Delivery) { |
| 275 |
return ((Delivery)context).getLink().getSession().getConnection().getReactor(); |
| 276 |
} else if (context instanceof Link) { |
| 277 |
return ((Link)context).getSession().getConnection().getReactor(); |
| 278 |
} else if (context instanceof Session) { |
| 279 |
return ((Session)context).getConnection().getReactor(); |
| 280 |
} else if (context instanceof Connection) { |
| 281 |
return ((Connection)context).getReactor(); |
| 282 |
} else if (context instanceof Selectable) { |
| 283 |
return ((Selectable)context).getReactor(); |
| 284 |
} |
| 285 |
return null; |
| 286 |
} |
| 287 |
|
| 288 |
@Override |
| 289 |
public Task getTask() { |
| 290 |
if (context instanceof Task) { |
| 291 |
return (Task) context; |
| 292 |
} else { |
| 293 |
return null; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public Record attachments() { |
| 299 |
return attachments; |
| 300 |
} |
| 301 |
|
| 302 |
@Override |
| 303 |
public Event copy() |
| 304 |
{ |
| 305 |
EventImpl newEvent = new EventImpl(); |
| 306 |
newEvent.init(type, context); |
| 307 |
newEvent.attachments.copy(attachments); |
| 308 |
return newEvent; |
| 309 |
} |
| 310 |
|
| 311 |
@Override |
| 312 |
public String toString() |
| 313 |
{ |
| 314 |
return "EventImpl{" + "type=" + type + ", context=" + context + '}'; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
} |