1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.agent; |
12 | |
|
13 | |
import org.mule.DefaultMuleEvent; |
14 | |
import org.mule.DefaultMuleMessage; |
15 | |
import org.mule.api.MuleEvent; |
16 | |
import org.mule.api.MuleMessage; |
17 | |
import org.mule.api.MuleSession; |
18 | |
import org.mule.api.context.notification.ServerNotification; |
19 | |
import org.mule.api.endpoint.OutboundEndpoint; |
20 | |
import org.mule.api.lifecycle.InitialisationException; |
21 | |
import org.mule.config.i18n.CoreMessages; |
22 | |
import org.mule.context.notification.ConnectionNotification; |
23 | |
import org.mule.context.notification.ModelNotification; |
24 | |
import org.mule.context.notification.MuleContextNotification; |
25 | |
import org.mule.session.DefaultMuleSession; |
26 | |
|
27 | |
import java.util.ArrayList; |
28 | |
import java.util.List; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class EndpointNotificationLoggerAgent extends AbstractNotificationLoggerAgent |
35 | |
{ |
36 | |
|
37 | 0 | private OutboundEndpoint endpoint = null; |
38 | |
private MuleSession session; |
39 | 0 | private List<Integer> ignoredNotifications = new ArrayList<Integer>(); |
40 | |
|
41 | |
|
42 | |
public EndpointNotificationLoggerAgent() |
43 | |
{ |
44 | 0 | super("Endpoint Logger Agent"); |
45 | |
|
46 | |
|
47 | 0 | ignoredNotifications.add(MuleContextNotification.CONTEXT_STOPPED); |
48 | 0 | ignoredNotifications.add(MuleContextNotification.CONTEXT_DISPOSING); |
49 | 0 | ignoredNotifications.add(MuleContextNotification.CONTEXT_DISPOSED); |
50 | 0 | ignoredNotifications.add(ModelNotification.MODEL_STOPPED); |
51 | 0 | ignoredNotifications.add(ModelNotification.MODEL_DISPOSED); |
52 | 0 | } |
53 | |
|
54 | |
@Override |
55 | |
protected void doInitialise() throws InitialisationException |
56 | |
{ |
57 | |
|
58 | |
try |
59 | |
{ |
60 | 0 | if (endpoint == null) |
61 | |
{ |
62 | 0 | throw new InitialisationException(CoreMessages.propertiesNotSet("endpoint"), this); |
63 | |
} |
64 | |
|
65 | 0 | session = new DefaultMuleSession(muleContext); |
66 | |
} |
67 | 0 | catch (Exception e) |
68 | |
{ |
69 | 0 | throw new InitialisationException(e, this); |
70 | 0 | } |
71 | 0 | } |
72 | |
|
73 | |
@Override |
74 | |
protected void logEvent(ServerNotification e) |
75 | |
{ |
76 | 0 | if (endpoint != null && !ignoredNotifications.contains(new Integer(e.getAction()))) |
77 | |
{ |
78 | 0 | if (!endpoint.getConnector().isStarted()) |
79 | |
{ |
80 | 0 | logger.warn("Endpoint not started: " + endpoint.getEndpointURI() + ". Cannot dispatch notification: " + e); |
81 | 0 | return; |
82 | |
} |
83 | 0 | if ((e.getAction() == ConnectionNotification.CONNECTION_FAILED || e.getAction() == ConnectionNotification.CONNECTION_DISCONNECTED) |
84 | |
&& (e.getSource()).equals(endpoint.getConnector())) |
85 | |
{ |
86 | |
|
87 | |
|
88 | |
|
89 | 0 | return; |
90 | |
} |
91 | 0 | MuleMessage msg = new DefaultMuleMessage(e, muleContext); |
92 | |
try |
93 | |
{ |
94 | |
|
95 | 0 | if (endpoint.getFilter() != null && !endpoint.getFilter().accept(msg)) |
96 | |
{ |
97 | 0 | if (logger.isInfoEnabled()) |
98 | |
{ |
99 | 0 | logger.info("Message not accepted with filter: " + endpoint.getFilter()); |
100 | |
} |
101 | 0 | return; |
102 | |
} |
103 | |
|
104 | 0 | MuleEvent event = new DefaultMuleEvent(msg, endpoint, session); |
105 | 0 | endpoint.process(event); |
106 | |
} |
107 | 0 | catch (Exception e1) |
108 | |
{ |
109 | |
|
110 | 0 | logger.error("Failed to dispatch event: " + e.toString() + " over endpoint: " + endpoint |
111 | |
+ ". Error is: " + e1.getMessage(), e1); |
112 | 0 | } |
113 | |
} |
114 | 0 | } |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
@Override |
120 | |
public String getDescription() |
121 | |
{ |
122 | 0 | StringBuffer buf = new StringBuffer(); |
123 | 0 | buf.append(getName()).append(": "); |
124 | 0 | if (endpoint != null) |
125 | |
{ |
126 | 0 | buf.append("Forwarding notifications to: ").append(endpoint.getEndpointURI().getAddress()); |
127 | |
} |
128 | 0 | return buf.toString(); |
129 | |
} |
130 | |
|
131 | |
public OutboundEndpoint getEndpoint() |
132 | |
{ |
133 | 0 | return endpoint; |
134 | |
} |
135 | |
|
136 | |
public void setEndpoint(OutboundEndpoint endpoint) |
137 | |
{ |
138 | 0 | this.endpoint = endpoint; |
139 | 0 | } |
140 | |
} |