View Javadoc

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      private OutboundEndpoint logEndpoint = null;
43      private MuleSession session;
44      private List ignoredNotifications = new ArrayList();
45  
46  
47      public EndpointNotificationLoggerAgent()
48      {
49          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          ignoredNotifications.add(new Integer(MuleContextNotification.CONTEXT_DISPOSING_CONNECTORS));
53          ignoredNotifications.add(new Integer(MuleContextNotification.CONTEXT_DISPOSED_CONNECTORS));
54          ignoredNotifications.add(new Integer(MuleContextNotification.CONTEXT_STOPPED));
55          ignoredNotifications.add(new Integer(MuleContextNotification.CONTEXT_DISPOSING));   
56          ignoredNotifications.add(new Integer(MuleContextNotification.CONTEXT_DISPOSED));
57          ignoredNotifications.add(new Integer(ModelNotification.MODEL_STOPPED));
58          ignoredNotifications.add(new Integer(ModelNotification.MODEL_DISPOSING));
59          ignoredNotifications.add(new Integer(ModelNotification.MODEL_DISPOSED));
60      }
61  
62      protected void doInitialise() throws InitialisationException
63      {
64          // first see if we're logging notifications to an endpoint
65          try
66          {
67              if (endpointAddress != null)
68              {
69                  logEndpoint = muleContext.getRegistry().lookupEndpointFactory().getOutboundEndpoint(endpointAddress);
70              }
71              else
72              {
73                  throw new InitialisationException(
74                      CoreMessages.propertiesNotSet("endpointAddress"), this);
75              }
76              // Create a session for sending notifications
77              session = new DefaultMuleSession(new DefaultMuleMessage(NullPayload.getInstance(), (Map) null), new NullSessionHandler(), muleContext);
78          }
79          catch (Exception e)
80          {
81              throw new InitialisationException(e, this);
82          }
83      }
84  
85      protected void logEvent(ServerNotification e)
86      {
87          if (logEndpoint != null && !ignoredNotifications.contains(new Integer(e.getAction())))
88          {
89              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                  return;
96              }
97              try
98              {
99                  MuleMessage msg = new DefaultMuleMessage(e.toString(), (Map) null);
100                 MuleEvent event = new DefaultMuleEvent(msg, logEndpoint, session, false);
101                 logEndpoint.dispatch(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: " + logEndpoint
107                              + ". Error is: " + e1.getMessage(), e1);
108             }
109         }
110     }
111 
112     /**
113      * Should be a 1 line description of the agent
114      * 
115      * @return
116      */
117     public String getDescription()
118     {
119         StringBuffer buf = new StringBuffer();
120         buf.append(getName()).append(": ");
121         if (endpointAddress != null)
122         {
123             buf.append("Forwarding notifications to: " + endpointAddress);
124         }
125         return buf.toString();
126     }
127 
128     public String getEndpointAddress()
129     {
130         return endpointAddress;
131     }
132 
133     public void setEndpointAddress(String endpointAddress)
134     {
135         this.endpointAddress = endpointAddress;
136     }
137 }