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