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.DefaultMuleSession; |
16 | |
import org.mule.NullSessionHandler; |
17 | |
import org.mule.api.MuleEvent; |
18 | |
import org.mule.api.MuleMessage; |
19 | |
import org.mule.api.MuleSession; |
20 | |
import org.mule.api.context.notification.ServerNotification; |
21 | |
import org.mule.api.endpoint.OutboundEndpoint; |
22 | |
import org.mule.api.lifecycle.InitialisationException; |
23 | |
import org.mule.api.transport.Connector; |
24 | |
import org.mule.config.i18n.CoreMessages; |
25 | |
import org.mule.context.notification.ConnectionNotification; |
26 | |
import org.mule.context.notification.ModelNotification; |
27 | |
import org.mule.context.notification.MuleContextNotification; |
28 | |
import org.mule.transport.NullPayload; |
29 | |
|
30 | |
import java.util.ArrayList; |
31 | |
import java.util.List; |
32 | |
import java.util.Map; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class EndpointNotificationLoggerAgent extends AbstractNotificationLoggerAgent |
39 | |
{ |
40 | |
|
41 | |
private String endpointAddress; |
42 | 0 | private OutboundEndpoint logEndpoint = null; |
43 | |
private MuleSession session; |
44 | 0 | private List ignoredNotifications = new ArrayList(); |
45 | |
|
46 | |
|
47 | |
public EndpointNotificationLoggerAgent() |
48 | |
{ |
49 | 0 | super("Endpoint Logger Agent"); |
50 | |
|
51 | |
|
52 | 0 | ignoredNotifications.add(new Integer(MuleContextNotification.CONTEXT_DISPOSING_CONNECTORS)); |
53 | 0 | ignoredNotifications.add(new Integer(MuleContextNotification.CONTEXT_DISPOSED_CONNECTORS)); |
54 | 0 | ignoredNotifications.add(new Integer(MuleContextNotification.CONTEXT_STOPPED)); |
55 | 0 | ignoredNotifications.add(new Integer(MuleContextNotification.CONTEXT_DISPOSING)); |
56 | 0 | ignoredNotifications.add(new Integer(MuleContextNotification.CONTEXT_DISPOSED)); |
57 | 0 | ignoredNotifications.add(new Integer(ModelNotification.MODEL_STOPPED)); |
58 | 0 | ignoredNotifications.add(new Integer(ModelNotification.MODEL_DISPOSING)); |
59 | 0 | ignoredNotifications.add(new Integer(ModelNotification.MODEL_DISPOSED)); |
60 | 0 | } |
61 | |
|
62 | |
protected void doInitialise() throws InitialisationException |
63 | |
{ |
64 | |
|
65 | |
try |
66 | |
{ |
67 | 0 | if (endpointAddress != null) |
68 | |
{ |
69 | 0 | logEndpoint = muleContext.getRegistry().lookupEndpointFactory().getOutboundEndpoint(endpointAddress); |
70 | |
} |
71 | |
else |
72 | |
{ |
73 | 0 | throw new InitialisationException( |
74 | |
CoreMessages.propertiesNotSet("endpointAddress"), this); |
75 | |
} |
76 | |
|
77 | 0 | session = new DefaultMuleSession(new DefaultMuleMessage(NullPayload.getInstance(), (Map) null), new NullSessionHandler(), muleContext); |
78 | |
} |
79 | 0 | catch (Exception e) |
80 | |
{ |
81 | 0 | throw new InitialisationException(e, this); |
82 | 0 | } |
83 | 0 | } |
84 | |
|
85 | |
protected void logEvent(ServerNotification e) |
86 | |
{ |
87 | 0 | if (logEndpoint != null && !ignoredNotifications.contains(new Integer(e.getAction()))) |
88 | |
{ |
89 | 0 | if ((e.getAction() == ConnectionNotification.CONNECTION_FAILED || e.getAction() == ConnectionNotification.CONNECTION_DISCONNECTED) |
90 | |
&& ((Connector) e.getSource()).equals(logEndpoint.getConnector())) |
91 | |
{ |
92 | |
|
93 | |
|
94 | |
|
95 | 0 | return; |
96 | |
} |
97 | |
try |
98 | |
{ |
99 | 0 | MuleMessage msg = new DefaultMuleMessage(e.toString(), (Map) null); |
100 | 0 | MuleEvent event = new DefaultMuleEvent(msg, logEndpoint, session, false); |
101 | 0 | logEndpoint.dispatch(event); |
102 | |
} |
103 | 0 | catch (Exception e1) |
104 | |
{ |
105 | |
|
106 | 0 | logger.error("Failed to dispatch event: " + e.toString() + " over endpoint: " + logEndpoint |
107 | |
+ ". Error is: " + e1.getMessage(), e1); |
108 | 0 | } |
109 | |
} |
110 | 0 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public String getDescription() |
118 | |
{ |
119 | 0 | StringBuffer buf = new StringBuffer(); |
120 | 0 | buf.append(getName()).append(": "); |
121 | 0 | if (endpointAddress != null) |
122 | |
{ |
123 | 0 | buf.append("Forwarding notifications to: " + endpointAddress); |
124 | |
} |
125 | 0 | return buf.toString(); |
126 | |
} |
127 | |
|
128 | |
public String getEndpointAddress() |
129 | |
{ |
130 | 0 | return endpointAddress; |
131 | |
} |
132 | |
|
133 | |
public void setEndpointAddress(String endpointAddress) |
134 | |
{ |
135 | 0 | this.endpointAddress = endpointAddress; |
136 | 0 | } |
137 | |
} |