KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > NotificationFilterSupport


1 /*
2  * @(#)NotificationFilterSupport.java 4.25 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.management;
9
10
11 // java imports
12
//
13
import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 /**
18  * Provides an implementation of the {@link javax.management.NotificationFilter} interface.
19  * The filtering is performed on the notification type attribute.
20  * <P>
21  * Manages a list of enabled notification types.
22  * A method allows users to enable/disable as many notification types as required.
23  * <P>
24  * Then, before sending a notification to a listener registered with a filter,
25  * the notification broadcaster compares this notification type with all notification types
26  * enabled by the filter. The notification will be sent to the listener
27  * only if its filter enables this notification type.
28  * <P>
29  * Example:
30  * <BLOCKQUOTE>
31  * <PRE>
32  * NotificationFilterSupport myFilter = new NotificationFilterSupport();
33  * myFilter.enableType("my_example.my_type");
34  * myBroadcaster.addListener(myListener, myFilter, null);
35  * </PRE>
36  * </BLOCKQUOTE>
37  * The listener <CODE>myListener</CODE> will only receive notifications the type of which equals/starts with "my_example.my_type".
38  *
39  * @see javax.management.NotificationBroadcaster#addNotificationListener
40  *
41  * @since 1.5
42  */

43 public class NotificationFilterSupport implements NotificationFilter JavaDoc, java.io.Serializable JavaDoc {
44
45     /* Serial version */
46     private static final long serialVersionUID = 6579080007561786969L;
47
48     /**
49      * @serial {@link Vector} that contains the enabled notification types.
50      * The default value is an empty vector.
51      */

52     private List JavaDoc enabledTypes = new Vector JavaDoc();
53
54   
55     /**
56      * Invoked before sending the specified notification to the listener.
57      * <BR>This filter compares the type of the specified notification with each enabled type.
58      * If the notification type matches one of the enabled types,
59      * the notification should be sent to the listener and this method returns <CODE>true</CODE>.
60      *
61      * @param notification The notification to be sent.
62      * @return <CODE>true</CODE> if the notification should be sent to the listener, <CODE>false</CODE> otherwise.
63      */

64     public synchronized boolean isNotificationEnabled(Notification JavaDoc notification) {
65         
66         String JavaDoc type = notification.getType();
67         
68         if (type == null) {
69             return false;
70         }
71         try {
72             for (Iterator JavaDoc i = enabledTypes.iterator(); i.hasNext(); ) {
73                 String JavaDoc prefix = (String JavaDoc)i.next();
74                 if (type.startsWith(prefix)) {
75                     return true;
76                 }
77             }
78         } catch (java.lang.NullPointerException JavaDoc e) {
79             // Should never occurs...
80
return false;
81         }
82         return false;
83     }
84     
85     /**
86      * Enables all the notifications the type of which starts with the specified prefix
87      * to be sent to the listener.
88      * <BR>If the specified prefix is already in the list of enabled notification types,
89      * this method has no effect.
90      * <P>
91      * Example:
92      * <BLOCKQUOTE>
93      * <PRE>
94      * // Enables all notifications the type of which starts with "my_example" to be sent.
95      * myFilter.enableType("my_example");
96      * // Enables all notifications the type of which is "my_example.my_type" to be sent.
97      * myFilter.enableType("my_example.my_type");
98      * </PRE>
99      * </BLOCKQUOTE>
100      *
101      * Note that:
102      * <BLOCKQUOTE><CODE>
103      * myFilter.enableType("my_example.*");
104      * </CODE></BLOCKQUOTE>
105      * will no match any notification type.
106      *
107      * @param prefix The prefix.
108      * @exception java.lang.IllegalArgumentException The prefix parameter is null.
109      */

110     public synchronized void enableType(String JavaDoc prefix) throws java.lang.IllegalArgumentException JavaDoc {
111         
112         if (prefix == null) {
113             throw new java.lang.IllegalArgumentException JavaDoc("The prefix cannot be null.");
114         }
115         if (!enabledTypes.contains(prefix)) {
116             enabledTypes.add(prefix);
117         }
118     }
119     
120     /**
121      * Removes the given prefix from the prefix list.
122      * <BR>If the specified prefix is not in the list of enabled notification types,
123      * this method has no effect.
124      *
125      * @param prefix The prefix.
126      */

127     public synchronized void disableType(String JavaDoc prefix) {
128         enabledTypes.remove(prefix);
129     }
130     
131     /**
132      * Disables all notification types.
133      */

134     public synchronized void disableAllTypes() {
135         enabledTypes.clear();
136     }
137   
138
139     /**
140      * Gets all the enabled notification types for this filter.
141      *
142      * @return The list containing all the enabled notification types.
143      */

144     public synchronized Vector JavaDoc getEnabledTypes() {
145         return (Vector JavaDoc)enabledTypes;
146     }
147       
148 }
149
Popular Tags