KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > internal > admin > EndpointNotificationLoggerAgent


1 /*
2  * $Id: EndpointNotificationLoggerAgent.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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.Message;
14 import org.mule.config.i18n.Messages;
15 import org.mule.impl.MuleEvent;
16 import org.mule.impl.MuleMessage;
17 import org.mule.impl.MuleSession;
18 import org.mule.impl.NullSessionHandler;
19 import org.mule.impl.endpoint.MuleEndpoint;
20 import org.mule.providers.NullPayload;
21 import org.mule.umo.UMOEvent;
22 import org.mule.umo.UMOMessage;
23 import org.mule.umo.UMOSession;
24 import org.mule.umo.endpoint.UMOEndpoint;
25 import org.mule.umo.lifecycle.InitialisationException;
26 import org.mule.umo.manager.UMOServerNotification;
27 import org.mule.umo.provider.UMOMessageDispatcher;
28
29 import java.util.Map JavaDoc;
30
31 /**
32  * <code>EndpointAbstractEventLoggerAgent</code> will forward server notifications
33  * to a configurered endpoint uri.
34  *
35  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
36  * @version $Revision: 3798 $
37  */

38 public class EndpointNotificationLoggerAgent extends AbstractNotificationLoggerAgent
39 {
40
41     private String JavaDoc endpointAddress;
42     private UMOEndpoint logEndpoint = null;
43     private UMOSession session;
44
45     protected void doInitialise() throws InitialisationException
46     {
47         // first see if we're logging notifications to an endpoint
48
try
49         {
50             if (endpointAddress != null)
51             {
52                 logEndpoint = MuleEndpoint.getOrCreateEndpointForUri(endpointAddress,
53                     UMOEndpoint.ENDPOINT_TYPE_SENDER);
54             }
55             else
56             {
57                 throw new InitialisationException(new Message(Messages.PROPERTIES_X_NOT_SET,
58                     "endpointAddress"), this);
59             }
60             // Create a session for sending notifications
61
session = new MuleSession(new MuleMessage(new NullPayload(), (Map JavaDoc)null), new NullSessionHandler());
62         }
63         catch (Exception JavaDoc e)
64         {
65             throw new InitialisationException(e, this);
66         }
67     }
68
69     protected void logEvent(UMOServerNotification e)
70     {
71         if (logEndpoint != null)
72         {
73             try
74             {
75                 UMOMessageDispatcher dispatcher = logEndpoint.getConnector().getDispatcher(logEndpoint);
76                 UMOMessage msg = new MuleMessage(e.toString(), (Map JavaDoc)null);
77                 UMOEvent event = new MuleEvent(msg, logEndpoint, session, false);
78                 dispatcher.dispatch(event);
79             }
80             catch (Exception JavaDoc e1)
81             {
82                 logger.error("Failed to dispatch event: " + e.toString() + " over endpoint: " + logEndpoint
83                              + ". Error is: " + e1.getMessage(), e1);
84             }
85         }
86     }
87
88     /**
89      * Should be a 1 line description of the agent
90      *
91      * @return
92      */

93     public String JavaDoc getDescription()
94     {
95         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
96         buf.append(getName()).append(": ");
97         if (endpointAddress != null)
98         {
99             buf.append("Forwarding notifications to: " + endpointAddress);
100         }
101         return buf.toString();
102     }
103
104     public String JavaDoc getEndpointAddress()
105     {
106         return endpointAddress;
107     }
108
109     public void setEndpointAddress(String JavaDoc endpointAddress)
110     {
111         this.endpointAddress = endpointAddress;
112     }
113 }
114
Popular Tags