1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.context.notification; |
8 | |
|
9 | |
import org.mule.api.MuleEvent; |
10 | |
import org.mule.api.MuleRuntimeException; |
11 | |
import org.mule.api.NamedObject; |
12 | |
import org.mule.api.construct.FlowConstruct; |
13 | |
import org.mule.api.context.notification.BlockingServerEvent; |
14 | |
import org.mule.api.context.notification.ServerNotification; |
15 | |
import org.mule.api.processor.MessageProcessor; |
16 | |
import org.mule.util.ObjectUtils; |
17 | |
|
18 | |
import java.lang.reflect.Method; |
19 | |
|
20 | 0 | public class MessageProcessorNotification extends ServerNotification implements BlockingServerEvent |
21 | |
{ |
22 | |
|
23 | |
private static final long serialVersionUID = 1L; |
24 | |
|
25 | |
public static final int MESSAGE_PROCESSOR_PRE_INVOKE = MESSAGE_PROCESSOR_EVENT_ACTION_START_RANGE + 1; |
26 | |
public static final int MESSAGE_PROCESSOR_POST_INVOKE = MESSAGE_PROCESSOR_EVENT_ACTION_START_RANGE + 2; |
27 | |
|
28 | |
private final transient MessageProcessor processor; |
29 | |
private String messageProcessorName; |
30 | |
|
31 | |
static |
32 | |
{ |
33 | 0 | registerAction("message processor pre invoke", MESSAGE_PROCESSOR_PRE_INVOKE); |
34 | 0 | registerAction("message processor post invoke", MESSAGE_PROCESSOR_POST_INVOKE); |
35 | 0 | } |
36 | |
|
37 | |
|
38 | |
public MessageProcessorNotification(FlowConstruct flowConstruct, MuleEvent event, MessageProcessor processor, int action) |
39 | |
{ |
40 | 0 | super(event, action, flowConstruct != null ? flowConstruct.getName() : null); |
41 | |
|
42 | 0 | this.processor = processor; |
43 | |
|
44 | |
|
45 | |
try |
46 | |
{ |
47 | |
|
48 | |
|
49 | |
try |
50 | |
{ |
51 | 0 | final Method method = processor.getClass().getMethod("getName"); |
52 | |
|
53 | 0 | messageProcessorName = ObjectUtils.toString(method.invoke(processor), toString(processor)); |
54 | |
} |
55 | 0 | catch (NoSuchMethodException e) |
56 | |
{ |
57 | |
|
58 | 0 | messageProcessorName = toString(processor); |
59 | 0 | } |
60 | |
} |
61 | 0 | catch (Exception e) |
62 | |
{ |
63 | 0 | throw new MuleRuntimeException(e); |
64 | 0 | } |
65 | 0 | } |
66 | |
|
67 | |
@Override |
68 | |
public MuleEvent getSource() |
69 | |
{ |
70 | 0 | if (source instanceof String) |
71 | |
{ |
72 | 0 | return null; |
73 | |
} |
74 | 0 | return (MuleEvent) super.getSource(); |
75 | |
} |
76 | |
|
77 | |
public MessageProcessor getProcessor() |
78 | |
{ |
79 | 0 | return processor; |
80 | |
} |
81 | |
|
82 | |
public String getFriendlyProcessorName() |
83 | |
{ |
84 | 0 | return messageProcessorName; |
85 | |
} |
86 | |
|
87 | |
protected String toString(Object obj) |
88 | |
{ |
89 | 0 | if (obj == null) |
90 | |
{ |
91 | 0 | return ""; |
92 | |
} |
93 | |
|
94 | |
String name; |
95 | 0 | if (obj instanceof NamedObject) |
96 | |
{ |
97 | 0 | name = String.format("%s '%s'", obj.getClass().getName(), ((NamedObject) obj).getName()); |
98 | |
} |
99 | |
else |
100 | |
{ |
101 | 0 | name = ObjectUtils.identityToString(obj); |
102 | |
} |
103 | 0 | return name; |
104 | |
} |
105 | |
} |