View Javadoc

1   /*
2    * $Id: AbstractNotificationLoggerAgent.java 12247 2008-07-07 21:25:01Z dfeist $
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.AbstractAgent;
14  import org.mule.api.MuleException;
15  import org.mule.api.context.MuleContextBuilder;
16  import org.mule.api.context.notification.ComponentMessageNotificationListener;
17  import org.mule.api.context.notification.ConnectionNotificationListener;
18  import org.mule.api.context.notification.CustomNotificationListener;
19  import org.mule.api.context.notification.EndpointMessageNotificationListener;
20  import org.mule.api.context.notification.ManagementNotificationListener;
21  import org.mule.api.context.notification.ModelNotificationListener;
22  import org.mule.api.context.notification.MuleContextNotificationListener;
23  import org.mule.api.context.notification.SecurityNotificationListener;
24  import org.mule.api.context.notification.ServerNotification;
25  import org.mule.api.context.notification.ServerNotificationListener;
26  import org.mule.api.context.notification.ServiceNotificationListener;
27  import org.mule.api.lifecycle.InitialisationException;
28  import org.mule.context.notification.NotificationException;
29  
30  import java.util.HashSet;
31  import java.util.Iterator;
32  import java.util.Set;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  /**
38   * <code>AbstractNotificationLoggerAgent</code> Receives Mule server notifications
39   * and logs them and can optionally route them to an endpoint. This agent will only
40   * receive notifications for notification events that are enabled. The notifications
41   * that are enabled are determined by the {@link MuleContextBuilder} that is used or
42   * configuration mechanisms that may override these values.
43   */
44  public abstract class AbstractNotificationLoggerAgent extends AbstractAgent
45  {
46      /**
47       * The logger used for this class
48       */
49      protected transient Log logger = LogFactory.getLog(getClass());
50  
51      private boolean ignoreManagerNotifications = false;
52      private boolean ignoreModelNotifications = false;
53      private boolean ignoreComponentNotifications = false;
54      private boolean ignoreConnectionNotifications = false;
55      private boolean ignoreSecurityNotifications = false;
56      private boolean ignoreManagementNotifications = false;
57      private boolean ignoreCustomNotifications = false;
58      private boolean ignoreAdminNotifications = false;
59      private boolean ignoreMessageNotifications = false;
60      private boolean ignoreEndpointMessageNotifications = false;
61      private boolean ignoreComponentMessageNotifications = false;
62  
63      private Set listeners = new HashSet();
64  
65  
66      protected AbstractNotificationLoggerAgent(String name)
67      {
68          super(name);
69      }
70  
71      public void start() throws MuleException
72      {
73          // nothing to do
74      }
75  
76      public void stop() throws MuleException
77      {
78          // nothing to do
79      }
80  
81      public void dispose()
82      {
83          // nothing to do
84      }
85  
86      public void registered()
87      {
88          // nothing to do
89      }
90  
91      public void unregistered()
92      {
93          for (Iterator iterator = listeners.iterator(); iterator.hasNext();)
94          {
95              ServerNotificationListener listener = (ServerNotificationListener) iterator.next();
96              muleContext.unregisterListener(listener);
97          }
98      }
99  
100     public boolean isIgnoreManagerNotifications()
101     {
102         return ignoreManagerNotifications;
103     }
104 
105     public void setIgnoreManagerNotifications(boolean ignoreManagerNotifications)
106     {
107         this.ignoreManagerNotifications = ignoreManagerNotifications;
108     }
109 
110     public boolean isIgnoreMessageNotifications()
111     {
112         return ignoreMessageNotifications;
113     }
114 
115     public void setIgnoreMessageNotifications(boolean ignoreMessageNotifications)
116     {
117         this.ignoreMessageNotifications = ignoreMessageNotifications;
118     }
119 
120     public boolean isIgnoreModelNotifications()
121     {
122         return ignoreModelNotifications;
123     }
124 
125     public void setIgnoreModelNotifications(boolean ignoreModelNotifications)
126     {
127         this.ignoreModelNotifications = ignoreModelNotifications;
128     }
129 
130     public boolean isIgnoreComponentNotifications()
131     {
132         return ignoreComponentNotifications;
133     }
134 
135     public void setIgnoreComponentNotifications(boolean ignoreComponentNotifications)
136     {
137         this.ignoreComponentNotifications = ignoreComponentNotifications;
138     }
139 
140     public boolean isIgnoreSecurityNotifications()
141     {
142         return ignoreSecurityNotifications;
143     }
144 
145     public void setIgnoreSecurityNotifications(boolean ignoreSecurityNotifications)
146     {
147         this.ignoreSecurityNotifications = ignoreSecurityNotifications;
148     }
149 
150     public boolean isIgnoreManagementNotifications()
151     {
152         return ignoreManagementNotifications;
153     }
154 
155     public void setIgnoreManagementNotifications(boolean ignoreManagementNotifications)
156     {
157         this.ignoreManagementNotifications = ignoreManagementNotifications;
158     }
159 
160     public boolean isIgnoreCustomNotifications()
161     {
162         return ignoreCustomNotifications;
163     }
164 
165     public void setIgnoreCustomNotifications(boolean ignoreCustomNotifications)
166     {
167         this.ignoreCustomNotifications = ignoreCustomNotifications;
168     }
169 
170     public boolean isIgnoreAdminNotifications()
171     {
172         return ignoreAdminNotifications;
173     }
174 
175     public void setIgnoreAdminNotifications(boolean ignoreAdminNotifications)
176     {
177         this.ignoreAdminNotifications = ignoreAdminNotifications;
178     }
179 
180     public boolean isIgnoreConnectionNotifications()
181     {
182         return ignoreConnectionNotifications;
183     }
184 
185     public void setIgnoreConnectionNotifications(boolean ignoreConnectionNotifications)
186     {
187         this.ignoreConnectionNotifications = ignoreConnectionNotifications;
188     }
189     
190     public boolean isIgnoreComponentMessageNotifications()
191     {
192         return ignoreComponentMessageNotifications;
193     }
194 
195     public void setIgnoreComponentMessageNotifications(boolean ignoreComponentMessageNotifications)
196     {
197         this.ignoreComponentMessageNotifications = ignoreComponentMessageNotifications;
198     }
199 
200     public boolean isIgnoreEndpointMessageNotifications()
201     {
202         return ignoreEndpointMessageNotifications;
203     }
204 
205     public void setIgnoreEndpointMessageNotifications(boolean ignoreEndpointMessageNotifications)
206     {
207         this.ignoreEndpointMessageNotifications = ignoreEndpointMessageNotifications;
208     }
209 
210     public final void initialise() throws InitialisationException
211     {
212         doInitialise();
213         if (!ignoreManagerNotifications)
214         {
215             ServerNotificationListener l = new MuleContextNotificationListener()
216             {
217                 public void onNotification(ServerNotification notification)
218                 {
219                     logEvent(notification);
220                 }
221             };
222             try
223             {
224                muleContext.registerListener(l);
225             }
226             catch (NotificationException e)
227             {
228                 throw new InitialisationException(e, this);
229             }
230             listeners.add(l);
231         }
232         if (!ignoreModelNotifications)
233         {
234             ServerNotificationListener l = new ModelNotificationListener()
235             {
236                 public void onNotification(ServerNotification notification)
237                 {
238                     logEvent(notification);
239                 }
240             };
241             try
242             {
243                muleContext.registerListener(l);
244             }
245             catch (NotificationException e)
246             {
247                 throw new InitialisationException(e, this);
248             }
249             listeners.add(l);
250         }
251         if (!ignoreComponentNotifications)
252         {
253             ServerNotificationListener l = new ServiceNotificationListener()
254             {
255                 public void onNotification(ServerNotification notification)
256                 {
257                     logEvent(notification);
258                 }
259             };
260             try
261             {
262                muleContext.registerListener(l);
263             }
264             catch (NotificationException e)
265             {
266                 throw new InitialisationException(e, this);
267             }
268             listeners.add(l);
269         }
270         if (!ignoreSecurityNotifications)
271         {
272             ServerNotificationListener l = new SecurityNotificationListener()
273             {
274                 public void onNotification(ServerNotification notification)
275                 {
276                     logEvent(notification);
277                 }
278             };
279             try
280             {
281                muleContext.registerListener(l);
282             }
283             catch (NotificationException e)
284             {
285                 throw new InitialisationException(e, this);
286             }
287             listeners.add(l);
288         }
289 
290         if (!ignoreManagementNotifications)
291         {
292             ServerNotificationListener l = new ManagementNotificationListener()
293             {
294                 public void onNotification(ServerNotification notification)
295                 {
296                     logEvent(notification);
297                 }
298             };
299             try
300             {
301                muleContext.registerListener(l);
302             }
303             catch (NotificationException e)
304             {
305                 throw new InitialisationException(e, this);
306             }
307             listeners.add(l);
308         }
309 
310         if (!ignoreCustomNotifications)
311         {
312             ServerNotificationListener l = new CustomNotificationListener()
313             {
314                 public void onNotification(ServerNotification notification)
315                 {
316                     logEvent(notification);
317                 }
318             };
319             try
320             {
321                muleContext.registerListener(l);
322             }
323             catch (NotificationException e)
324             {
325                 throw new InitialisationException(e, this);
326             }
327             listeners.add(l);
328         }
329 
330         if (!ignoreConnectionNotifications)
331         {
332             ServerNotificationListener l = new ConnectionNotificationListener()
333             {
334                 public void onNotification(ServerNotification notification)
335                 {
336                     logEvent(notification);
337                 }
338             };
339             try
340             {
341                muleContext.registerListener(l);
342             }
343             catch (NotificationException e)
344             {
345                 throw new InitialisationException(e, this);
346             }
347             listeners.add(l);
348         }
349 
350         if (!ignoreMessageNotifications && !ignoreEndpointMessageNotifications)
351         {
352             ServerNotificationListener l = new EndpointMessageNotificationListener()
353             {
354                 public void onNotification(ServerNotification notification)
355                 {
356                     logEvent(notification);
357                 }
358             };
359             try
360             {
361                muleContext.registerListener(l);
362             }
363             catch (NotificationException e)
364             {
365                 throw new InitialisationException(e, this);
366             }
367             listeners.add(l);
368         }
369         
370         if (!ignoreMessageNotifications && !ignoreComponentMessageNotifications)
371         {
372             ServerNotificationListener l = new ComponentMessageNotificationListener()
373             {
374                 public void onNotification(ServerNotification notification)
375                 {
376                     logEvent(notification);
377                 }
378             };
379             try
380             {
381                muleContext.registerListener(l);
382             }
383             catch (NotificationException e)
384             {
385                 throw new InitialisationException(e, this);
386             }
387             listeners.add(l);
388         }
389 
390     }
391 
392     protected abstract void doInitialise() throws InitialisationException;
393 
394     protected abstract void logEvent(ServerNotification e);
395 }