View Javadoc

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