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.context.notification;
8   
9   import org.mule.api.context.notification.ServerNotification;
10  import org.mule.api.context.notification.ServerNotificationHandler;
11  import org.mule.api.context.notification.ServerNotificationListener;
12  
13  /**
14   * Optimized to make a quick decision on a particular class of messages.
15   */
16  public class OptimisedNotificationHandler implements ServerNotificationHandler
17  {
18  
19      private ServerNotificationHandler delegate;
20      private Class type;
21      private boolean dynamic = false;
22      private volatile Boolean enabled = null;
23  
24      public OptimisedNotificationHandler(ServerNotificationHandler delegate, Class type)
25      {
26          this.delegate = delegate;
27          this.type = type;
28          dynamic = delegate.isNotificationDynamic();
29      }
30  
31      public boolean isNotificationDynamic()
32      {
33          return dynamic;
34      }
35  
36      public boolean isListenerRegistered(ServerNotificationListener listener)
37      {
38          return delegate.isListenerRegistered(listener);
39      }
40  
41      /**
42       * This returns a very "conservative" value - it is true if the notification or any subclass would be
43       * accepted.  So if it returns false then you can be sure that there is no need to send the
44       * notification.  On the other hand, if it returns true there is no guarantee that the notification
45       * "really" will be dispatched to any listener.
46       *
47       * @param notfnClass Either the notification class being generated or some superclass
48       * @return false if there is no need to dispatch the notification
49       */
50      public boolean isNotificationEnabled(Class notfnClass)
51      {
52          if ((!dynamic) && type.isAssignableFrom(notfnClass))
53          {
54              if (enabled == null)
55              {
56                  enabled = delegate.isNotificationEnabled(notfnClass);
57              }
58  
59              return enabled;
60          }
61          else
62          {
63              return delegate.isNotificationEnabled(notfnClass);
64          }
65      }
66  
67      public void fireNotification(ServerNotification notification)
68      {
69          if (isNotificationEnabled(notification.getClass()))
70          {
71              delegate.fireNotification(notification);
72          }
73      }
74  
75  }