01: /*
02: * $Id: OptimisedNotificationHandler.java 10489 2008-01-23 17:53:38Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
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
20: ServerNotificationHandler {
21:
22: private ServerNotificationHandler delegate;
23: private Class type;
24: private boolean dynamic = false;
25: private boolean enabled = false;
26:
27: public OptimisedNotificationHandler(
28: ServerNotificationHandler delegate, Class type) {
29: this .delegate = delegate;
30: this .type = type;
31: dynamic = delegate.isNotificationDynamic();
32: enabled = delegate.isNotificationEnabled(type);
33: }
34:
35: public boolean isNotificationDynamic() {
36: return dynamic;
37: }
38:
39: /**
40: * This returns a very "conservative" value - it is true if the notification or any subclass would be
41: * accepted. So if it returns false then you can be sure that there is no need to send the
42: * notification. On the other hand, if it returns true there is no guarantee that the notification
43: * "really" will be dispatched to any listener.
44: *
45: * @param notfnClass Either the notification class being generated or some superclass
46: * @return false if there is no need to dispatch the notification
47: */
48: public boolean isNotificationEnabled(Class notfnClass) {
49: if ((!dynamic) && type.isAssignableFrom(notfnClass)) {
50: return enabled;
51: } else {
52: return delegate.isNotificationEnabled(notfnClass);
53: }
54: }
55:
56: public void fireNotification(ServerNotification notification) {
57: if (isNotificationEnabled(notification.getClass())) {
58: delegate.fireNotification(notification);
59: }
60: }
61:
62: }
|