View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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   * <code>EndpointAbstractEventLoggerAgent</code> will forward server notifications
28   * to a configurered endpoint uri.
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          // List of notifications to ignore, because when these notifications are
42          // received the notification endpoint is no longer available
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          // first see if we're logging notifications to an endpoint
54          try
55          {
56              if (endpoint == null)
57              {
58                  throw new InitialisationException(CoreMessages.propertiesNotSet("endpoint"), this);
59              }
60              // Create a session for sending notifications
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                  // If this is a CONNECTION_FAILED or
83                  // CONNECTION_DISCONNECTED notification for the same connector that
84                  // is being used for notifications then ignore.
85                  return;
86              }
87              MuleMessage msg = new DefaultMuleMessage(e, muleContext);
88              try
89              {
90                  //TODO: Filters should really be applied by the endpoint
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                 // TODO MULE-863: If this is an error, do something better than this
106                 logger.error("Failed to dispatch event: " + e.toString() + " over endpoint: " + endpoint
107                         + ". Error is: " + e1.getMessage(), e1);
108             }
109         }
110     }
111 
112     /**
113      * Should be a 1 line description of the agent
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 }