Coverage Report - org.mule.agent.EndpointNotificationLoggerAgent
 
Classes in this File Line Coverage Branch Coverage Complexity
EndpointNotificationLoggerAgent
0%
0/41
0%
0/22
0
 
 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  0
     private OutboundEndpoint endpoint = null;
 34  
     private MuleSession session;
 35  0
     private List<Integer> ignoredNotifications = new ArrayList<Integer>();
 36  
 
 37  
 
 38  
     public EndpointNotificationLoggerAgent()
 39  
     {
 40  0
         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  0
         ignoredNotifications.add(MuleContextNotification.CONTEXT_STOPPED);
 44  0
         ignoredNotifications.add(MuleContextNotification.CONTEXT_DISPOSING);
 45  0
         ignoredNotifications.add(MuleContextNotification.CONTEXT_DISPOSED);
 46  0
         ignoredNotifications.add(ModelNotification.MODEL_STOPPED);
 47  0
         ignoredNotifications.add(ModelNotification.MODEL_DISPOSED);
 48  0
     }
 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  0
             if (endpoint == null)
 57  
             {
 58  0
                 throw new InitialisationException(CoreMessages.propertiesNotSet("endpoint"), this);
 59  
             }
 60  
             // Create a session for sending notifications
 61  0
             session = new DefaultMuleSession(muleContext);
 62  
         }
 63  0
         catch (Exception e)
 64  
         {
 65  0
             throw new InitialisationException(e, this);
 66  0
         }
 67  0
     }
 68  
 
 69  
     @Override
 70  
     protected void logEvent(ServerNotification e)
 71  
     {
 72  0
         if (endpoint != null && !ignoredNotifications.contains(new Integer(e.getAction())))
 73  
         {
 74  0
             if (!endpoint.getConnector().isStarted())
 75  
             {
 76  0
                 logger.warn("Endpoint not started: " + endpoint.getEndpointURI() + ". Cannot dispatch notification: " + e);
 77  0
                 return;
 78  
             }
 79  0
             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  0
                 return;
 86  
             }
 87  0
             MuleMessage msg = new DefaultMuleMessage(e, muleContext);
 88  
             try
 89  
             {
 90  
                 //TODO: Filters should really be applied by the endpoint
 91  0
                 if (endpoint.getFilter() != null && !endpoint.getFilter().accept(msg))
 92  
                 {
 93  0
                     if (logger.isInfoEnabled())
 94  
                     {
 95  0
                         logger.info("Message not accepted with filter: " + endpoint.getFilter());
 96  
                     }
 97  0
                     return;
 98  
                 }
 99  
 
 100  0
                 MuleEvent event = new DefaultMuleEvent(msg, endpoint, session);
 101  0
                 endpoint.process(event);
 102  
             }
 103  0
             catch (Exception e1)
 104  
             {
 105  
                 // TODO MULE-863: If this is an error, do something better than this
 106  0
                 logger.error("Failed to dispatch event: " + e.toString() + " over endpoint: " + endpoint
 107  
                         + ". Error is: " + e1.getMessage(), e1);
 108  0
             }
 109  
         }
 110  0
     }
 111  
 
 112  
     /**
 113  
      * Should be a 1 line description of the agent
 114  
      */
 115  
     @Override
 116  
     public String getDescription()
 117  
     {
 118  0
         StringBuffer buf = new StringBuffer();
 119  0
         buf.append(getName()).append(": ");
 120  0
         if (endpoint != null)
 121  
         {
 122  0
             buf.append("Forwarding notifications to: ").append(endpoint.getEndpointURI().getAddress());
 123  
         }
 124  0
         return buf.toString();
 125  
     }
 126  
 
 127  
     public OutboundEndpoint getEndpoint()
 128  
     {
 129  0
         return endpoint;
 130  
     }
 131  
 
 132  
     public void setEndpoint(OutboundEndpoint endpoint)
 133  
     {
 134  0
         this.endpoint = endpoint;
 135  0
     }
 136  
 }