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