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