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