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