View Javadoc

1   /*
2    * $Id: EndpointNotificationLoggerAgent.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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.impl.internal.admin;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.impl.MuleEvent;
15  import org.mule.impl.MuleMessage;
16  import org.mule.impl.MuleSession;
17  import org.mule.impl.NullSessionHandler;
18  import org.mule.impl.endpoint.MuleEndpoint;
19  import org.mule.providers.NullPayload;
20  import org.mule.umo.UMOEvent;
21  import org.mule.umo.UMOMessage;
22  import org.mule.umo.UMOSession;
23  import org.mule.umo.endpoint.UMOEndpoint;
24  import org.mule.umo.lifecycle.InitialisationException;
25  import org.mule.umo.manager.UMOServerNotification;
26  
27  import java.util.Map;
28  
29  /**
30   * <code>EndpointAbstractEventLoggerAgent</code> will forward server notifications
31   * to a configurered endpoint uri.
32   */
33  public class EndpointNotificationLoggerAgent extends AbstractNotificationLoggerAgent
34  {
35  
36      private String endpointAddress;
37      private UMOEndpoint logEndpoint = null;
38      private UMOSession session;
39  
40      protected void doInitialise() throws InitialisationException
41      {
42          // first see if we're logging notifications to an endpoint
43          try
44          {
45              if (endpointAddress != null)
46              {
47                  logEndpoint = MuleEndpoint.getOrCreateEndpointForUri(endpointAddress,
48                      UMOEndpoint.ENDPOINT_TYPE_SENDER);
49              }
50              else
51              {
52                  throw new InitialisationException(
53                      CoreMessages.propertiesNotSet("endpointAddress"), this);
54              }
55              // Create a session for sending notifications
56              session = new MuleSession(new MuleMessage(NullPayload.getInstance(), (Map) null), new NullSessionHandler());
57          }
58          catch (Exception e)
59          {
60              throw new InitialisationException(e, this);
61          }
62      }
63  
64      protected void logEvent(UMOServerNotification e)
65      {
66          if (logEndpoint != null)
67          {
68              try
69              {
70                  UMOMessage msg = new MuleMessage(e.toString(), (Map) null);
71                  UMOEvent event = new MuleEvent(msg, logEndpoint, session, false);
72                  logEndpoint.dispatch(event);
73              }
74              catch (Exception e1)
75              {
76                  // TODO MULE-863: If this is an error, do something better than this
77                  logger.error("Failed to dispatch event: " + e.toString() + " over endpoint: " + logEndpoint
78                               + ". Error is: " + e1.getMessage(), e1);
79              }
80          }
81      }
82  
83      /**
84       * Should be a 1 line description of the agent
85       * 
86       * @return
87       */
88      public String getDescription()
89      {
90          StringBuffer buf = new StringBuffer();
91          buf.append(getName()).append(": ");
92          if (endpointAddress != null)
93          {
94              buf.append("Forwarding notifications to: " + endpointAddress);
95          }
96          return buf.toString();
97      }
98  
99      public String getEndpointAddress()
100     {
101         return endpointAddress;
102     }
103 
104     public void setEndpointAddress(String endpointAddress)
105     {
106         this.endpointAddress = endpointAddress;
107     }
108 }