1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.impl; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.config.MuleProperties; |
15 | |
import org.mule.config.i18n.CoreMessages; |
16 | |
import org.mule.impl.endpoint.MuleEndpoint; |
17 | |
import org.mule.providers.AbstractConnector; |
18 | |
import org.mule.umo.UMOComponent; |
19 | |
import org.mule.umo.UMOEvent; |
20 | |
import org.mule.umo.UMOException; |
21 | |
import org.mule.umo.UMOMessage; |
22 | |
import org.mule.umo.UMOSession; |
23 | |
import org.mule.umo.endpoint.EndpointNotFoundException; |
24 | |
import org.mule.umo.endpoint.UMOEndpoint; |
25 | |
import org.mule.umo.endpoint.UMOImmutableEndpoint; |
26 | |
import org.mule.umo.provider.DispatchException; |
27 | |
import org.mule.umo.provider.ReceiveException; |
28 | |
import org.mule.umo.provider.UMOConnector; |
29 | |
import org.mule.umo.provider.UMOSessionHandler; |
30 | |
import org.mule.umo.routing.UMOOutboundRouterCollection; |
31 | |
import org.mule.umo.security.UMOSecurityContext; |
32 | |
import org.mule.util.UUID; |
33 | |
|
34 | |
import java.util.HashMap; |
35 | |
import java.util.Iterator; |
36 | |
import java.util.Map; |
37 | |
|
38 | |
import org.apache.commons.logging.Log; |
39 | |
import org.apache.commons.logging.LogFactory; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public final class MuleSession implements UMOSession |
47 | |
{ |
48 | |
|
49 | |
|
50 | |
|
51 | |
private static final long serialVersionUID = 3380926585676521866L; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | private static Log logger = LogFactory.getLog(MuleSession.class); |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 0 | private UMOComponent component = null; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 0 | private boolean valid = true; |
67 | |
|
68 | |
private String id; |
69 | |
|
70 | |
private UMOSecurityContext securityContext; |
71 | |
|
72 | 0 | private Map properties = null; |
73 | |
|
74 | |
public MuleSession(UMOComponent component) |
75 | 0 | { |
76 | 0 | properties = new HashMap(); |
77 | 0 | id = UUID.getUUID(); |
78 | 0 | this.component = component; |
79 | 0 | } |
80 | |
|
81 | |
public MuleSession(UMOMessage message, UMOSessionHandler requestSessionHandler, UMOComponent component) |
82 | |
throws UMOException |
83 | |
{ |
84 | 0 | this(message, requestSessionHandler); |
85 | 0 | if (component == null) |
86 | |
{ |
87 | 0 | throw new IllegalArgumentException( |
88 | |
CoreMessages.propertiesNotSet("component").toString()); |
89 | |
} |
90 | 0 | this.component = component; |
91 | 0 | } |
92 | |
|
93 | |
public MuleSession(UMOMessage message, UMOSessionHandler requestSessionHandler) throws UMOException |
94 | 0 | { |
95 | |
|
96 | 0 | if (requestSessionHandler == null) |
97 | |
{ |
98 | 0 | throw new IllegalArgumentException( |
99 | |
CoreMessages.propertiesNotSet("requestSessionHandler").toString()); |
100 | |
} |
101 | |
|
102 | 0 | if (message == null) |
103 | |
{ |
104 | 0 | throw new IllegalArgumentException( |
105 | |
CoreMessages.propertiesNotSet("message").toString()); |
106 | |
} |
107 | |
|
108 | 0 | properties = new HashMap(); |
109 | 0 | requestSessionHandler.retrieveSessionInfoFromMessage(message, this); |
110 | 0 | id = (String) getProperty(requestSessionHandler.getSessionIDKey()); |
111 | 0 | if (id == null) |
112 | |
{ |
113 | 0 | id = UUID.getUUID(); |
114 | 0 | if (logger.isDebugEnabled()) |
115 | |
{ |
116 | 0 | logger.debug("There is no session id on the request using key: " |
117 | |
+ requestSessionHandler.getSessionIDKey() + ". Generating new session id: " + id); |
118 | |
} |
119 | |
} |
120 | 0 | else if (logger.isDebugEnabled()) |
121 | |
{ |
122 | 0 | logger.debug("Got session with id: " + id); |
123 | |
} |
124 | 0 | } |
125 | |
|
126 | |
public void dispatchEvent(UMOMessage message) throws UMOException |
127 | |
{ |
128 | 0 | if (component == null) |
129 | |
{ |
130 | 0 | throw new IllegalStateException(CoreMessages.objectIsNull("Component").getMessage()); |
131 | |
} |
132 | |
|
133 | 0 | UMOOutboundRouterCollection router = component.getDescriptor().getOutboundRouter(); |
134 | 0 | if (router == null) |
135 | |
{ |
136 | 0 | throw new EndpointNotFoundException( |
137 | |
CoreMessages.noOutboundRouterSetOn(component.getDescriptor().getName())); |
138 | |
} |
139 | 0 | router.route(message, this, false); |
140 | 0 | } |
141 | |
|
142 | |
public void dispatchEvent(UMOMessage message, String endpointName) throws UMOException |
143 | |
{ |
144 | 0 | dispatchEvent(message, MuleManager.getInstance().lookupEndpoint(endpointName)); |
145 | 0 | } |
146 | |
|
147 | |
public void dispatchEvent(UMOMessage message, UMOImmutableEndpoint endpoint) throws UMOException |
148 | |
{ |
149 | 0 | if (endpoint == null) |
150 | |
{ |
151 | 0 | logger.warn("Endpoint argument is null, using outbound router to determine endpoint."); |
152 | 0 | dispatchEvent(message); |
153 | |
} |
154 | |
|
155 | 0 | if (logger.isDebugEnabled()) |
156 | |
{ |
157 | 0 | logger.debug("Session has received asynchronous event on: " + endpoint); |
158 | |
} |
159 | |
|
160 | 0 | UMOEvent event = createOutboundEvent(message, endpoint, null); |
161 | |
|
162 | 0 | dispatchEvent(event); |
163 | |
|
164 | |
|
165 | 0 | UMOMessage response = OptimizedRequestContext.criticalWriteResponse(event.getMessage()); |
166 | 0 | processResponse(response); |
167 | 0 | } |
168 | |
|
169 | |
public UMOMessage sendEvent(UMOMessage message, String endpointName) throws UMOException |
170 | |
{ |
171 | 0 | return sendEvent(message, MuleManager.getInstance().lookupEndpoint(endpointName)); |
172 | |
} |
173 | |
|
174 | |
public UMOMessage sendEvent(UMOMessage message) throws UMOException |
175 | |
{ |
176 | 0 | if (component == null) |
177 | |
{ |
178 | 0 | throw new IllegalStateException(CoreMessages.objectIsNull("Component").getMessage()); |
179 | |
} |
180 | 0 | UMOOutboundRouterCollection router = component.getDescriptor().getOutboundRouter(); |
181 | 0 | if (router == null) |
182 | |
{ |
183 | 0 | throw new EndpointNotFoundException( |
184 | |
CoreMessages.noOutboundRouterSetOn(component.getDescriptor().getName())); |
185 | |
} |
186 | 0 | UMOMessage result = router.route(message, this, true); |
187 | 0 | if (result != null) |
188 | |
{ |
189 | 0 | RequestContext.writeResponse(result); |
190 | 0 | processResponse(result); |
191 | |
} |
192 | |
|
193 | 0 | return result; |
194 | |
} |
195 | |
|
196 | |
public UMOMessage sendEvent(UMOMessage message, UMOImmutableEndpoint endpoint) throws UMOException |
197 | |
{ |
198 | 0 | if (endpoint == null) |
199 | |
{ |
200 | 0 | logger.warn("Endpoint argument is null, using outbound router to determine endpoint."); |
201 | 0 | return sendEvent(message); |
202 | |
} |
203 | |
|
204 | 0 | if (logger.isDebugEnabled()) |
205 | |
{ |
206 | 0 | logger.debug("Session has received synchronous event on endpoint: " + endpoint); |
207 | |
} |
208 | |
|
209 | 0 | UMOEvent event = createOutboundEvent(message, endpoint, null); |
210 | 0 | UMOMessage result = sendEvent(event); |
211 | |
|
212 | |
|
213 | |
|
214 | 0 | if (endpoint.isRemoteSync() && endpoint.getResponseTransformer() != null && result != null) |
215 | |
{ |
216 | 0 | Object response = endpoint.getResponseTransformer().transform(result.getPayload()); |
217 | 0 | result = new MuleMessage(response, result); |
218 | |
} |
219 | |
|
220 | 0 | if (result != null) |
221 | |
{ |
222 | 0 | OptimizedRequestContext.unsafeWriteResponse(result); |
223 | 0 | processResponse(result); |
224 | |
} |
225 | |
|
226 | 0 | return result; |
227 | |
} |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
public void dispatchEvent(UMOEvent event) throws UMOException |
235 | |
{ |
236 | 0 | if (event.getEndpoint().canSend()) |
237 | |
{ |
238 | |
try |
239 | |
{ |
240 | 0 | if (logger.isDebugEnabled()) |
241 | |
{ |
242 | 0 | logger.debug("dispatching event: " + event); |
243 | |
} |
244 | |
|
245 | 0 | UMOConnector connector = event.getEndpoint().getConnector(); |
246 | |
|
247 | 0 | if (connector instanceof AbstractConnector) |
248 | |
{ |
249 | 0 | ((AbstractConnector) connector).getSessionHandler().storeSessionInfoToMessage(this, |
250 | |
event.getMessage()); |
251 | |
} |
252 | |
else |
253 | |
{ |
254 | |
|
255 | 0 | logger.warn("A session handler could not be obtained, using default"); |
256 | 0 | new MuleSessionHandler().storeSessionInfoToMessage(this, event.getMessage()); |
257 | |
} |
258 | |
|
259 | 0 | event.getEndpoint().dispatch(event); |
260 | |
} |
261 | 0 | catch (Exception e) |
262 | |
{ |
263 | 0 | throw new DispatchException(event.getMessage(), event.getEndpoint(), e); |
264 | 0 | } |
265 | |
} |
266 | 0 | else if (component != null) |
267 | |
{ |
268 | 0 | if (logger.isDebugEnabled()) |
269 | |
{ |
270 | 0 | logger.debug("dispatching event to component: " + component.getDescriptor().getName() |
271 | |
+ ", event is: " + event); |
272 | |
} |
273 | 0 | component.dispatchEvent(event); |
274 | 0 | UMOMessage response = OptimizedRequestContext.criticalWriteResponse(event.getMessage()); |
275 | 0 | processResponse(response); |
276 | |
} |
277 | |
else |
278 | |
{ |
279 | 0 | throw new DispatchException(CoreMessages.noComponentForEndpoint(), event.getMessage(), |
280 | |
event.getEndpoint()); |
281 | |
} |
282 | 0 | } |
283 | |
|
284 | |
public String getId() |
285 | |
{ |
286 | 0 | return id; |
287 | |
} |
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
public UMOMessage sendEvent(UMOEvent event) throws UMOException |
297 | |
{ |
298 | 0 | int timeout = event.getMessage().getIntProperty(MuleProperties.MULE_EVENT_TIMEOUT_PROPERTY, -1); |
299 | 0 | if (timeout >= 0) |
300 | |
{ |
301 | 0 | event.setTimeout(timeout); |
302 | |
} |
303 | |
|
304 | 0 | if (event.getEndpoint().canSend()) |
305 | |
{ |
306 | |
try |
307 | |
{ |
308 | 0 | if (logger.isDebugEnabled()) |
309 | |
{ |
310 | 0 | logger.debug("sending event: " + event); |
311 | |
} |
312 | |
|
313 | 0 | UMOConnector connector = event.getEndpoint().getConnector(); |
314 | |
|
315 | 0 | if (connector instanceof AbstractConnector) |
316 | |
{ |
317 | 0 | ((AbstractConnector) connector).getSessionHandler().storeSessionInfoToMessage(this, |
318 | |
event.getMessage()); |
319 | |
} |
320 | |
else |
321 | |
{ |
322 | |
|
323 | 0 | logger.warn("A session handler could not be obtained, using default."); |
324 | 0 | new MuleSessionHandler().storeSessionInfoToMessage(this, event.getMessage()); |
325 | |
} |
326 | |
|
327 | 0 | UMOMessage response = event.getEndpoint().send(event); |
328 | 0 | OptimizedRequestContext.unsafeWriteResponse(response); |
329 | 0 | processResponse(response); |
330 | 0 | return response; |
331 | |
} |
332 | 0 | catch (UMOException e) |
333 | |
{ |
334 | 0 | throw e; |
335 | |
} |
336 | 0 | catch (Exception e) |
337 | |
{ |
338 | 0 | throw new DispatchException(event.getMessage(), event.getEndpoint(), e); |
339 | |
} |
340 | |
|
341 | |
} |
342 | 0 | else if (component != null) |
343 | |
{ |
344 | 0 | if (logger.isDebugEnabled()) |
345 | |
{ |
346 | 0 | logger.debug("sending event to component: " + component.getDescriptor().getName() |
347 | |
+ " event is: " + event); |
348 | |
} |
349 | 0 | return component.sendEvent(event); |
350 | |
|
351 | |
} |
352 | |
else |
353 | |
{ |
354 | 0 | throw new DispatchException(CoreMessages.noComponentForEndpoint(), event.getMessage(), |
355 | |
event.getEndpoint()); |
356 | |
} |
357 | |
} |
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
protected void processResponse(UMOMessage response) |
366 | |
{ |
367 | 0 | if (response == null) |
368 | |
{ |
369 | 0 | return; |
370 | |
} |
371 | 0 | response.removeProperty(MuleProperties.MULE_METHOD_PROPERTY); |
372 | 0 | response.removeProperty(MuleProperties.MULE_REMOTE_SYNC_PROPERTY); |
373 | 0 | } |
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
public boolean isValid() |
381 | |
{ |
382 | 0 | return valid; |
383 | |
} |
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
public void setValid(boolean value) |
391 | |
{ |
392 | 0 | valid = value; |
393 | 0 | } |
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
public UMOMessage receiveEvent(String endpointName, long timeout) throws UMOException |
402 | |
{ |
403 | 0 | UMOEndpoint endpoint = MuleEndpoint.getOrCreateEndpointForUri(endpointName, |
404 | |
UMOEndpoint.ENDPOINT_TYPE_RECEIVER); |
405 | 0 | return receiveEvent(endpoint, timeout); |
406 | |
} |
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
public UMOMessage receiveEvent(UMOImmutableEndpoint endpoint, long timeout) throws UMOException |
415 | |
{ |
416 | |
try |
417 | |
{ |
418 | 0 | return endpoint.receive(timeout); |
419 | |
} |
420 | 0 | catch (Exception e) |
421 | |
{ |
422 | 0 | throw new ReceiveException(endpoint, timeout, e); |
423 | |
} |
424 | |
} |
425 | |
|
426 | |
public UMOEvent createOutboundEvent(UMOMessage message, |
427 | |
UMOImmutableEndpoint endpoint, |
428 | |
UMOEvent previousEvent) throws UMOException |
429 | |
{ |
430 | 0 | if (endpoint == null) |
431 | |
{ |
432 | 0 | throw new DispatchException(CoreMessages.objectIsNull("Outbound Endpoint"), message, |
433 | |
endpoint); |
434 | |
} |
435 | |
|
436 | 0 | if (logger.isDebugEnabled()) |
437 | |
{ |
438 | 0 | logger.debug("Creating event with data: " + message.getPayload().getClass().getName() |
439 | |
+ ", for endpoint: " + endpoint); |
440 | |
} |
441 | |
|
442 | |
|
443 | 0 | if (endpoint.getTransformer() == null && endpoint instanceof UMOEndpoint) |
444 | |
{ |
445 | 0 | if (endpoint.getConnector() instanceof AbstractConnector) |
446 | |
{ |
447 | 0 | ((UMOEndpoint)endpoint).setTransformer(((AbstractConnector) endpoint.getConnector()).getDefaultOutboundTransformer()); |
448 | 0 | logger.debug("Using default connector outbound transformer: " + endpoint.getTransformer()); |
449 | |
} |
450 | |
} |
451 | |
try |
452 | |
{ |
453 | |
UMOEvent event; |
454 | 0 | if (previousEvent != null) |
455 | |
{ |
456 | 0 | event = new MuleEvent(message, endpoint, component, previousEvent); |
457 | |
} |
458 | |
else |
459 | |
{ |
460 | 0 | event = new MuleEvent(message, endpoint, this, false, null); |
461 | |
} |
462 | 0 | return event; |
463 | |
} |
464 | 0 | catch (Exception e) |
465 | |
{ |
466 | 0 | throw new DispatchException( |
467 | |
CoreMessages.failedToCreate("Event"), message, endpoint, e); |
468 | |
} |
469 | |
} |
470 | |
|
471 | |
|
472 | |
|
473 | |
|
474 | |
public UMOComponent getComponent() |
475 | |
{ |
476 | 0 | return component; |
477 | |
} |
478 | |
|
479 | |
void setComponent(UMOComponent component) |
480 | |
{ |
481 | 0 | this.component = component; |
482 | 0 | } |
483 | |
|
484 | |
|
485 | |
|
486 | |
|
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
public void setSecurityContext(UMOSecurityContext context) |
492 | |
{ |
493 | 0 | securityContext = context; |
494 | 0 | } |
495 | |
|
496 | |
|
497 | |
|
498 | |
|
499 | |
|
500 | |
|
501 | |
|
502 | |
public UMOSecurityContext getSecurityContext() |
503 | |
{ |
504 | 0 | return securityContext; |
505 | |
} |
506 | |
|
507 | |
|
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
|
513 | |
|
514 | |
public void setProperty(Object key, Object value) |
515 | |
{ |
516 | 0 | properties.put(key, value); |
517 | 0 | } |
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
|
523 | |
|
524 | |
|
525 | |
public Object getProperty(Object key) |
526 | |
{ |
527 | 0 | return properties.get(key); |
528 | |
} |
529 | |
|
530 | |
|
531 | |
|
532 | |
|
533 | |
|
534 | |
|
535 | |
|
536 | |
public Object removeProperty(Object key) |
537 | |
{ |
538 | 0 | return properties.remove(key); |
539 | |
} |
540 | |
|
541 | |
|
542 | |
|
543 | |
|
544 | |
|
545 | |
|
546 | |
|
547 | |
|
548 | |
public Iterator getPropertyNames() |
549 | |
{ |
550 | 0 | return properties.keySet().iterator(); |
551 | |
} |
552 | |
|
553 | |
} |