View Javadoc

1   /*
2    * $Id: EndpointNotificationLoggerAgent.java 22162 2011-06-09 18:23:00Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * <code>EndpointAbstractEventLoggerAgent</code> will forward server notifications
32   * to a configurered endpoint uri.
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          // List of notifications to ignore, because when these notifications are
46          // received the notification endpoint is no longer available
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          // first see if we're logging notifications to an endpoint
58          try
59          {
60              if (endpoint == null)
61              {
62                  throw new InitialisationException(CoreMessages.propertiesNotSet("endpoint"), this);
63              }
64              // Create a session for sending notifications
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                  // If this is a CONNECTION_FAILED or
87                  // CONNECTION_DISCONNECTED notification for the same connector that
88                  // is being used for notifications then ignore.
89                  return;
90              }
91              MuleMessage msg = new DefaultMuleMessage(e, muleContext);
92              try
93              {
94                  //TODO: Filters should really be applied by the endpoint
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                 // TODO MULE-863: If this is an error, do something better than this
110                 logger.error("Failed to dispatch event: " + e.toString() + " over endpoint: " + endpoint
111                         + ". Error is: " + e1.getMessage(), e1);
112             }
113         }
114     }
115 
116     /**
117      * Should be a 1 line description of the agent
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 }