View Javadoc

1   /*
2    * $Id: OptimisedNotificationHandler.java 20321 2010-11-24 15:21:24Z dfeist $
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.context.notification;
12  
13  import org.mule.api.context.notification.ServerNotification;
14  import org.mule.api.context.notification.ServerNotificationHandler;
15  import org.mule.api.context.notification.ServerNotificationListener;
16  
17  /**
18   * Optimized to make a quick decision on a particular class of messages.
19   */
20  public class OptimisedNotificationHandler implements ServerNotificationHandler
21  {
22  
23      private ServerNotificationHandler delegate;
24      private Class type;
25      private boolean dynamic = false;
26      private boolean enabled = false;
27  
28      public OptimisedNotificationHandler(ServerNotificationHandler delegate, Class type)
29      {
30          this.delegate = delegate;
31          this.type = type;
32          dynamic = delegate.isNotificationDynamic();
33          enabled = delegate.isNotificationEnabled(type);
34      }
35  
36      public boolean isNotificationDynamic()
37      {
38          return dynamic;
39      }
40  
41      public boolean isListenerRegistered(ServerNotificationListener listener)
42      {
43          return delegate.isListenerRegistered(listener);
44      }
45  
46      /**
47       * This returns a very "conservative" value - it is true if the notification or any subclass would be
48       * accepted.  So if it returns false then you can be sure that there is no need to send the
49       * notification.  On the other hand, if it returns true there is no guarantee that the notification
50       * "really" will be dispatched to any listener.
51       *
52       * @param notfnClass Either the notification class being generated or some superclass
53       * @return false if there is no need to dispatch the notification
54       */
55      public boolean isNotificationEnabled(Class notfnClass)
56      {
57          if ((!dynamic) && type.isAssignableFrom(notfnClass))
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  }