Coverage Report - org.mule.agent.EndpointNotificationLoggerAgent
 
Classes in this File Line Coverage Branch Coverage Complexity
EndpointNotificationLoggerAgent
0%
0/38
0%
0/14
2.5
 
 1  
 /*
 2  
  * $Id: EndpointNotificationLoggerAgent.java 11343 2008-03-13 10:58:26Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.DefaultMuleSession;
 16  
 import org.mule.NullSessionHandler;
 17  
 import org.mule.api.MuleEvent;
 18  
 import org.mule.api.MuleMessage;
 19  
 import org.mule.api.MuleSession;
 20  
 import org.mule.api.context.notification.ServerNotification;
 21  
 import org.mule.api.endpoint.OutboundEndpoint;
 22  
 import org.mule.api.lifecycle.InitialisationException;
 23  
 import org.mule.api.transport.Connector;
 24  
 import org.mule.config.i18n.CoreMessages;
 25  
 import org.mule.context.notification.ConnectionNotification;
 26  
 import org.mule.context.notification.ModelNotification;
 27  
 import org.mule.context.notification.MuleContextNotification;
 28  
 import org.mule.transport.NullPayload;
 29  
 
 30  
 import java.util.ArrayList;
 31  
 import java.util.List;
 32  
 import java.util.Map;
 33  
 
 34  
 /**
 35  
  * <code>EndpointAbstractEventLoggerAgent</code> will forward server notifications
 36  
  * to a configurered endpoint uri.
 37  
  */
 38  
 public class EndpointNotificationLoggerAgent extends AbstractNotificationLoggerAgent
 39  
 {
 40  
 
 41  
     private String endpointAddress;
 42  0
     private OutboundEndpoint logEndpoint = null;
 43  
     private MuleSession session;
 44  0
     private List ignoredNotifications = new ArrayList();
 45  
 
 46  
 
 47  
     public EndpointNotificationLoggerAgent()
 48  
     {
 49  0
         super("Endpoint Logger Agent");
 50  
         // List of notifications to ignore, because when these notifications are
 51  
         // received the notification endpoint is no longer available
 52  0
         ignoredNotifications.add(new Integer(MuleContextNotification.CONTEXT_DISPOSING_CONNECTORS));
 53  0
         ignoredNotifications.add(new Integer(MuleContextNotification.CONTEXT_DISPOSED_CONNECTORS));
 54  0
         ignoredNotifications.add(new Integer(MuleContextNotification.CONTEXT_STOPPED));
 55  0
         ignoredNotifications.add(new Integer(MuleContextNotification.CONTEXT_DISPOSING));   
 56  0
         ignoredNotifications.add(new Integer(MuleContextNotification.CONTEXT_DISPOSED));
 57  0
         ignoredNotifications.add(new Integer(ModelNotification.MODEL_STOPPED));
 58  0
         ignoredNotifications.add(new Integer(ModelNotification.MODEL_DISPOSING));
 59  0
         ignoredNotifications.add(new Integer(ModelNotification.MODEL_DISPOSED));
 60  0
     }
 61  
 
 62  
     protected void doInitialise() throws InitialisationException
 63  
     {
 64  
         // first see if we're logging notifications to an endpoint
 65  
         try
 66  
         {
 67  0
             if (endpointAddress != null)
 68  
             {
 69  0
                 logEndpoint = muleContext.getRegistry().lookupEndpointFactory().getOutboundEndpoint(endpointAddress);
 70  
             }
 71  
             else
 72  
             {
 73  0
                 throw new InitialisationException(
 74  
                     CoreMessages.propertiesNotSet("endpointAddress"), this);
 75  
             }
 76  
             // Create a session for sending notifications
 77  0
             session = new DefaultMuleSession(new DefaultMuleMessage(NullPayload.getInstance(), (Map) null), new NullSessionHandler(), muleContext);
 78  
         }
 79  0
         catch (Exception e)
 80  
         {
 81  0
             throw new InitialisationException(e, this);
 82  0
         }
 83  0
     }
 84  
 
 85  
     protected void logEvent(ServerNotification e)
 86  
     {
 87  0
         if (logEndpoint != null && !ignoredNotifications.contains(new Integer(e.getAction())))
 88  
         {
 89  0
             if ((e.getAction() == ConnectionNotification.CONNECTION_FAILED || e.getAction() == ConnectionNotification.CONNECTION_DISCONNECTED)
 90  
                 && ((Connector) e.getSource()).equals(logEndpoint.getConnector()))
 91  
             {
 92  
                 // If this is a CONNECTION_FAILED or
 93  
                 // CONNECTION_DISCONNECTED notification for the same connector that
 94  
                 // is being used for notifications then ignore.
 95  0
                 return;
 96  
             }
 97  
             try
 98  
             {
 99  0
                 MuleMessage msg = new DefaultMuleMessage(e.toString(), (Map) null);
 100  0
                 MuleEvent event = new DefaultMuleEvent(msg, logEndpoint, session, false);
 101  0
                 logEndpoint.dispatch(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: " + logEndpoint
 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  
      * @return
 116  
      */
 117  
     public String getDescription()
 118  
     {
 119  0
         StringBuffer buf = new StringBuffer();
 120  0
         buf.append(getName()).append(": ");
 121  0
         if (endpointAddress != null)
 122  
         {
 123  0
             buf.append("Forwarding notifications to: " + endpointAddress);
 124  
         }
 125  0
         return buf.toString();
 126  
     }
 127  
 
 128  
     public String getEndpointAddress()
 129  
     {
 130  0
         return endpointAddress;
 131  
     }
 132  
 
 133  
     public void setEndpointAddress(String endpointAddress)
 134  
     {
 135  0
         this.endpointAddress = endpointAddress;
 136  0
     }
 137  
 }