View Javadoc

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