1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule; |
12 | |
|
13 | |
import org.mule.api.DefaultMuleException; |
14 | |
import org.mule.api.MuleContext; |
15 | |
import org.mule.api.MuleEvent; |
16 | |
import org.mule.api.MuleException; |
17 | |
import org.mule.api.MuleMessage; |
18 | |
import org.mule.api.MuleSession; |
19 | |
import org.mule.api.ThreadSafeAccess; |
20 | |
import org.mule.api.config.MuleProperties; |
21 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
22 | |
import org.mule.api.security.Credentials; |
23 | |
import org.mule.api.service.Service; |
24 | |
import org.mule.api.transformer.TransformerException; |
25 | |
import org.mule.api.transport.PropertyScope; |
26 | |
import org.mule.config.i18n.CoreMessages; |
27 | |
import org.mule.endpoint.DefaultEndpointFactory; |
28 | |
import org.mule.security.MuleCredentials; |
29 | |
import org.mule.util.MapUtils; |
30 | |
import org.mule.util.UUID; |
31 | |
|
32 | |
import java.io.IOException; |
33 | |
import java.io.ObjectInputStream; |
34 | |
import java.io.ObjectOutputStream; |
35 | |
import java.io.OutputStream; |
36 | |
import java.io.UnsupportedEncodingException; |
37 | |
import java.util.EventObject; |
38 | |
import java.util.Iterator; |
39 | |
|
40 | |
import org.apache.commons.beanutils.PropertyUtils; |
41 | |
import org.apache.commons.logging.Log; |
42 | |
import org.apache.commons.logging.LogFactory; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public class DefaultMuleEvent extends EventObject implements MuleEvent, ThreadSafeAccess |
54 | |
{ |
55 | |
|
56 | |
|
57 | |
|
58 | |
private static final long serialVersionUID = 1L; |
59 | |
|
60 | |
|
61 | |
|
62 | 618 | protected Log logger = LogFactory.getLog(getClass()); |
63 | |
|
64 | |
|
65 | |
|
66 | 618 | private transient ImmutableEndpoint endpoint = null; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | 618 | private String id = null; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | 618 | private MuleMessage message = null; |
77 | |
|
78 | |
private MuleSession session; |
79 | |
|
80 | 618 | private boolean stopFurtherProcessing = false; |
81 | |
|
82 | 618 | private boolean synchronous = false; |
83 | |
|
84 | 618 | private int timeout = TIMEOUT_NOT_SET_VALUE; |
85 | |
|
86 | 618 | private transient ResponseOutputStream outputStream = null; |
87 | |
|
88 | 618 | private transient Object transformedMessage = null; |
89 | |
|
90 | 618 | private Credentials credentials = null; |
91 | |
|
92 | 618 | protected String[] ignoredPropertyOverrides = new String[]{MuleProperties.MULE_METHOD_PROPERTY}; |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public DefaultMuleEvent(MuleMessage message, |
100 | |
ImmutableEndpoint endpoint, |
101 | |
Service service, |
102 | |
MuleEvent previousEvent) |
103 | |
{ |
104 | 0 | super(message.getPayload()); |
105 | 0 | this.message = message; |
106 | 0 | this.id = generateEventId(); |
107 | 0 | this.session = previousEvent.getSession(); |
108 | 0 | ((DefaultMuleSession) session).setService(service); |
109 | 0 | this.endpoint = endpoint; |
110 | 0 | this.synchronous = previousEvent.isSynchronous(); |
111 | 0 | this.timeout = previousEvent.getTimeout(); |
112 | 0 | this.outputStream = (ResponseOutputStream) previousEvent.getOutputStream(); |
113 | 0 | fillProperties(previousEvent); |
114 | 0 | } |
115 | |
|
116 | |
public DefaultMuleEvent(MuleMessage message, |
117 | |
ImmutableEndpoint endpoint, |
118 | |
MuleSession session, |
119 | |
boolean synchronous) |
120 | |
{ |
121 | 330 | this(message, endpoint, session, synchronous, null); |
122 | 330 | } |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public DefaultMuleEvent(MuleMessage message, |
133 | |
ImmutableEndpoint endpoint, |
134 | |
MuleSession session, |
135 | |
boolean synchronous, |
136 | |
ResponseOutputStream outputStream) |
137 | |
{ |
138 | 338 | super(message.getPayload()); |
139 | 338 | this.message = message; |
140 | 338 | this.endpoint = endpoint; |
141 | 338 | this.session = session; |
142 | 338 | this.id = generateEventId(); |
143 | 338 | this.synchronous = synchronous; |
144 | 338 | this.outputStream = outputStream; |
145 | 338 | fillProperties(null); |
146 | 338 | } |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public DefaultMuleEvent(MuleMessage message, |
157 | |
ImmutableEndpoint endpoint, |
158 | |
MuleSession session, |
159 | |
String eventId, |
160 | |
boolean synchronous) |
161 | |
{ |
162 | 0 | super(message.getPayload()); |
163 | 0 | this.message = message; |
164 | 0 | this.endpoint = endpoint; |
165 | 0 | this.session = session; |
166 | 0 | this.id = eventId; |
167 | 0 | this.synchronous = synchronous; |
168 | 0 | fillProperties(null); |
169 | 0 | } |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public DefaultMuleEvent(MuleMessage message, MuleEvent rewriteEvent) |
178 | |
{ |
179 | 280 | super(message.getPayload()); |
180 | 280 | this.message = message; |
181 | 280 | this.id = rewriteEvent.getId(); |
182 | 280 | this.session = rewriteEvent.getSession(); |
183 | 280 | ((DefaultMuleSession) session).setService(rewriteEvent.getService()); |
184 | 280 | this.endpoint = rewriteEvent.getEndpoint(); |
185 | 280 | this.synchronous = rewriteEvent.isSynchronous(); |
186 | 280 | this.timeout = rewriteEvent.getTimeout(); |
187 | 280 | this.outputStream = (ResponseOutputStream) rewriteEvent.getOutputStream(); |
188 | 280 | if (rewriteEvent instanceof DefaultMuleEvent) |
189 | |
{ |
190 | 280 | this.transformedMessage = ((DefaultMuleEvent) rewriteEvent).getCachedMessage(); |
191 | |
} |
192 | 280 | fillProperties(rewriteEvent); |
193 | 280 | } |
194 | |
|
195 | |
protected void fillProperties(MuleEvent previousEvent) |
196 | |
{ |
197 | 618 | if (previousEvent != null) |
198 | |
{ |
199 | 280 | MuleMessage msg = previousEvent.getMessage(); |
200 | 280 | synchronized (msg) |
201 | |
{ |
202 | 280 | for (Iterator iterator = msg.getPropertyNames().iterator(); iterator.hasNext();) |
203 | |
{ |
204 | 72 | String prop = (String) iterator.next(); |
205 | 72 | Object value = msg.getProperty(prop); |
206 | |
|
207 | 72 | if (!ignoreProperty(prop)) |
208 | |
{ |
209 | 2 | message.setProperty(prop, value); |
210 | |
} |
211 | 72 | } |
212 | 280 | } |
213 | |
} |
214 | |
|
215 | 618 | if (endpoint != null && endpoint.getProperties() != null) |
216 | |
{ |
217 | 618 | for (Iterator iterator = endpoint.getProperties().keySet().iterator(); iterator.hasNext();) |
218 | |
{ |
219 | 0 | String prop = (String) iterator.next(); |
220 | 0 | Object value = endpoint.getProperties().get(prop); |
221 | |
|
222 | 0 | if (!ignoreProperty(prop)) |
223 | |
{ |
224 | 0 | message.setProperty(prop, value, PropertyScope.INVOCATION); |
225 | |
} |
226 | 0 | } |
227 | |
} |
228 | |
|
229 | 618 | setCredentials(); |
230 | 618 | } |
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
protected boolean ignoreProperty(String key) |
245 | |
{ |
246 | 72 | if (key == null) |
247 | |
{ |
248 | 0 | return true; |
249 | |
} |
250 | |
|
251 | 144 | for (int i = 0; i < ignoredPropertyOverrides.length; i++) |
252 | |
{ |
253 | 72 | if (key.equals(ignoredPropertyOverrides[i])) |
254 | |
{ |
255 | 0 | return false; |
256 | |
} |
257 | |
} |
258 | |
|
259 | 72 | return null != message.getProperty(key); |
260 | |
} |
261 | |
|
262 | |
protected void setCredentials() |
263 | |
{ |
264 | 618 | if (null != endpoint && null != endpoint.getEndpointURI() && null != endpoint.getEndpointURI().getUserInfo()) |
265 | |
{ |
266 | 0 | final String userName = endpoint.getEndpointURI().getUser(); |
267 | 0 | final String password = endpoint.getEndpointURI().getPassword(); |
268 | 0 | if (password != null && userName != null) |
269 | |
{ |
270 | 0 | credentials = new MuleCredentials(userName, password.toCharArray()); |
271 | |
} |
272 | |
} |
273 | 618 | } |
274 | |
|
275 | |
public Credentials getCredentials() |
276 | |
{ |
277 | 0 | return credentials; |
278 | |
} |
279 | |
|
280 | |
Object getCachedMessage() |
281 | |
{ |
282 | 280 | return transformedMessage; |
283 | |
} |
284 | |
|
285 | |
public MuleMessage getMessage() |
286 | |
{ |
287 | 536 | return message; |
288 | |
} |
289 | |
|
290 | |
public byte[] getMessageAsBytes() throws DefaultMuleException |
291 | |
{ |
292 | |
try |
293 | |
{ |
294 | 0 | return message.getPayloadAsBytes(); |
295 | |
} |
296 | 0 | catch (Exception e) |
297 | |
{ |
298 | 0 | throw new DefaultMuleException( |
299 | |
CoreMessages.cannotReadPayloadAsBytes(message.getPayload().getClass().getName()), e); |
300 | |
} |
301 | |
} |
302 | |
|
303 | |
public Object transformMessage() throws TransformerException |
304 | |
{ |
305 | 104 | return transformMessage(null); |
306 | |
} |
307 | |
|
308 | |
public Object transformMessage(Class outputType) throws TransformerException |
309 | |
{ |
310 | 104 | message.applyTransformers(endpoint.getTransformers()); |
311 | 104 | if(outputType==null) |
312 | |
{ |
313 | 104 | return message.getPayload(); |
314 | |
} |
315 | |
else |
316 | |
{ |
317 | 0 | return message.getPayload(outputType); |
318 | |
} |
319 | |
} |
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
public byte[] transformMessageToBytes() throws TransformerException |
332 | |
{ |
333 | 0 | return (byte[]) transformMessage(byte[].class); |
334 | |
} |
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
public String transformMessageToString() throws TransformerException |
348 | |
{ |
349 | |
try |
350 | |
{ |
351 | 0 | return new String(transformMessageToBytes(), getEncoding()); |
352 | |
} |
353 | 0 | catch (UnsupportedEncodingException e) |
354 | |
{ |
355 | 0 | throw new TransformerException(endpoint.getTransformers(), e); |
356 | |
} |
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
} |
373 | |
|
374 | |
public String getMessageAsString() throws MuleException |
375 | |
{ |
376 | 36 | return getMessageAsString(getEncoding()); |
377 | |
} |
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
public String getMessageAsString(String encoding) throws MuleException |
388 | |
{ |
389 | |
try |
390 | |
{ |
391 | 36 | return message.getPayloadAsString(encoding); |
392 | |
} |
393 | 0 | catch (Exception e) |
394 | |
{ |
395 | 0 | throw new DefaultMuleException( |
396 | |
CoreMessages.cannotReadPayloadAsString(message.getClass().getName()), e); |
397 | |
} |
398 | |
} |
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
public String getId() |
406 | |
{ |
407 | 292 | return id; |
408 | |
} |
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
public Object getProperty(String name, boolean exhaustiveSearch) |
414 | |
{ |
415 | 0 | return getProperty(name, null, exhaustiveSearch); |
416 | |
} |
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | |
public Object getProperty(String name, Object defaultValue, boolean exhaustiveSearch) |
425 | |
{ |
426 | 0 | Object property = message.getProperty(name); |
427 | |
|
428 | 0 | if (exhaustiveSearch) |
429 | |
{ |
430 | |
|
431 | 0 | if (property == null) |
432 | |
{ |
433 | 0 | property = MapUtils.getObject(getEndpoint().getEndpointURI().getParams(), name, null); |
434 | |
} |
435 | |
|
436 | |
|
437 | 0 | if (property == null) |
438 | |
{ |
439 | |
try |
440 | |
{ |
441 | 0 | property = PropertyUtils.getProperty(getEndpoint().getConnector(), name); |
442 | |
} |
443 | 0 | catch (Exception e) |
444 | |
{ |
445 | |
|
446 | |
|
447 | 0 | } |
448 | |
} |
449 | |
} |
450 | 0 | return (property == null ? defaultValue : property); |
451 | |
} |
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
public ImmutableEndpoint getEndpoint() |
459 | |
{ |
460 | 376 | return endpoint; |
461 | |
} |
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
public String toString() |
469 | |
{ |
470 | 0 | StringBuffer buf = new StringBuffer(64); |
471 | 0 | buf.append("MuleEvent: ").append(getId()); |
472 | 0 | buf.append(", sync=").append(isSynchronous()); |
473 | 0 | buf.append(", stop processing=").append(isStopFurtherProcessing()); |
474 | 0 | buf.append(", ").append(endpoint); |
475 | |
|
476 | 0 | return buf.toString(); |
477 | |
} |
478 | |
|
479 | |
protected String generateEventId() |
480 | |
{ |
481 | 338 | return UUID.getUUID(); |
482 | |
} |
483 | |
|
484 | |
public MuleSession getSession() |
485 | |
{ |
486 | 450 | return session; |
487 | |
} |
488 | |
|
489 | |
void setSession(MuleSession session) |
490 | |
{ |
491 | 0 | this.session = session; |
492 | 0 | } |
493 | |
|
494 | |
|
495 | |
|
496 | |
|
497 | |
public Service getService() |
498 | |
{ |
499 | 346 | return session.getService(); |
500 | |
} |
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
public boolean isStopFurtherProcessing() |
508 | |
{ |
509 | 18 | return stopFurtherProcessing; |
510 | |
} |
511 | |
|
512 | |
|
513 | |
|
514 | |
|
515 | |
|
516 | |
|
517 | |
|
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
|
523 | |
public void setStopFurtherProcessing(boolean stopFurtherProcessing) |
524 | |
{ |
525 | 0 | this.stopFurtherProcessing = stopFurtherProcessing; |
526 | 0 | } |
527 | |
|
528 | |
public boolean equals(Object o) |
529 | |
{ |
530 | 16 | if (this == o) |
531 | |
{ |
532 | 16 | return true; |
533 | |
} |
534 | 0 | if (!(o instanceof DefaultMuleEvent)) |
535 | |
{ |
536 | 0 | return false; |
537 | |
} |
538 | |
|
539 | 0 | final DefaultMuleEvent event = (DefaultMuleEvent) o; |
540 | |
|
541 | 0 | if (message != null ? !message.equals(event.message) : event.message != null) |
542 | |
{ |
543 | 0 | return false; |
544 | |
} |
545 | 0 | return id.equals(event.id); |
546 | |
} |
547 | |
|
548 | |
public int hashCode() |
549 | |
{ |
550 | 0 | return 29 * id.hashCode() + (message != null ? message.hashCode() : 0); |
551 | |
} |
552 | |
|
553 | |
public boolean isSynchronous() |
554 | |
{ |
555 | 298 | return synchronous; |
556 | |
} |
557 | |
|
558 | |
public void setSynchronous(boolean value) |
559 | |
{ |
560 | 8 | synchronous = value; |
561 | 8 | } |
562 | |
|
563 | |
public int getTimeout() |
564 | |
{ |
565 | 280 | if (timeout == TIMEOUT_NOT_SET_VALUE) |
566 | |
{ |
567 | |
|
568 | 280 | timeout = endpoint.getRemoteSyncTimeout(); |
569 | |
} |
570 | 280 | return timeout; |
571 | |
} |
572 | |
|
573 | |
public void setTimeout(int timeout) |
574 | |
{ |
575 | 0 | this.timeout = timeout; |
576 | 0 | } |
577 | |
|
578 | |
|
579 | |
|
580 | |
|
581 | |
|
582 | |
|
583 | |
|
584 | |
|
585 | |
public OutputStream getOutputStream() |
586 | |
{ |
587 | 280 | return outputStream; |
588 | |
} |
589 | |
|
590 | |
private void writeObject(ObjectOutputStream out) throws IOException |
591 | |
{ |
592 | 2 | out.defaultWriteObject(); |
593 | 2 | out.writeInt(endpoint.hashCode()); |
594 | 2 | } |
595 | |
|
596 | |
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException |
597 | |
{ |
598 | 2 | in.defaultReadObject(); |
599 | 2 | int endpointHashCode = in.readInt(); |
600 | 2 | endpoint = (ImmutableEndpoint) RegistryContext.getRegistry().lookupObject( |
601 | |
DefaultEndpointFactory.ENDPOINT_REGISTRY_PREFIX + endpointHashCode); |
602 | 2 | } |
603 | |
|
604 | |
|
605 | |
|
606 | |
|
607 | |
|
608 | |
|
609 | |
|
610 | |
|
611 | |
|
612 | |
public String getEncoding() |
613 | |
{ |
614 | 36 | String encoding = message.getEncoding(); |
615 | 36 | if (encoding == null) |
616 | |
{ |
617 | 0 | encoding = endpoint.getEncoding(); |
618 | |
} |
619 | |
|
620 | 36 | return encoding; |
621 | |
} |
622 | |
|
623 | |
public MuleContext getMuleContext() |
624 | |
{ |
625 | 2 | return endpoint.getMuleContext(); |
626 | |
} |
627 | |
|
628 | |
public ThreadSafeAccess newThreadCopy() |
629 | |
{ |
630 | 268 | if (message instanceof ThreadSafeAccess) |
631 | |
{ |
632 | 268 | DefaultMuleEvent copy = new DefaultMuleEvent((MuleMessage) ((ThreadSafeAccess) message).newThreadCopy(), this); |
633 | 268 | copy.resetAccessControl(); |
634 | 268 | return copy; |
635 | |
} |
636 | |
else |
637 | |
{ |
638 | 0 | return this; |
639 | |
} |
640 | |
} |
641 | |
|
642 | |
public void resetAccessControl() |
643 | |
{ |
644 | 270 | if (message instanceof ThreadSafeAccess) |
645 | |
{ |
646 | 270 | ((ThreadSafeAccess) message).resetAccessControl(); |
647 | |
} |
648 | 270 | } |
649 | |
|
650 | |
public void assertAccess(boolean write) |
651 | |
{ |
652 | 42 | if (message instanceof ThreadSafeAccess) |
653 | |
{ |
654 | 42 | ((ThreadSafeAccess) message).assertAccess(write); |
655 | |
} |
656 | 34 | } |
657 | |
|
658 | |
} |