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