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