001 /*
002 * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.management;
027
028 import java.util.List;
029 import java.util.Vector;
030
031 /**
032 * Provides an implementation of the {@link javax.management.NotificationFilter} interface.
033 * The filtering is performed on the notification type attribute.
034 * <P>
035 * Manages a list of enabled notification types.
036 * A method allows users to enable/disable as many notification types as required.
037 * <P>
038 * Then, before sending a notification to a listener registered with a filter,
039 * the notification broadcaster compares this notification type with all notification types
040 * enabled by the filter. The notification will be sent to the listener
041 * only if its filter enables this notification type.
042 * <P>
043 * Example:
044 * <BLOCKQUOTE>
045 * <PRE>
046 * NotificationFilterSupport myFilter = new NotificationFilterSupport();
047 * myFilter.enableType("my_example.my_type");
048 * myBroadcaster.addListener(myListener, myFilter, null);
049 * </PRE>
050 * </BLOCKQUOTE>
051 * The listener <CODE>myListener</CODE> will only receive notifications the type of which equals/starts with "my_example.my_type".
052 *
053 * @see javax.management.NotificationBroadcaster#addNotificationListener
054 *
055 * @since 1.5
056 */
057 public class NotificationFilterSupport implements NotificationFilter {
058
059 /* Serial version */
060 private static final long serialVersionUID = 6579080007561786969L;
061
062 /**
063 * @serial {@link Vector} that contains the enabled notification types.
064 * The default value is an empty vector.
065 */
066 private List<String> enabledTypes = new Vector<String>();
067
068 /**
069 * Invoked before sending the specified notification to the listener.
070 * <BR>This filter compares the type of the specified notification with each enabled type.
071 * If the notification type matches one of the enabled types,
072 * the notification should be sent to the listener and this method returns <CODE>true</CODE>.
073 *
074 * @param notification The notification to be sent.
075 * @return <CODE>true</CODE> if the notification should be sent to the listener, <CODE>false</CODE> otherwise.
076 */
077 public synchronized boolean isNotificationEnabled(
078 Notification notification) {
079
080 String type = notification.getType();
081
082 if (type == null) {
083 return false;
084 }
085 try {
086 for (String prefix : enabledTypes) {
087 if (type.startsWith(prefix)) {
088 return true;
089 }
090 }
091 } catch (java.lang.NullPointerException e) {
092 // Should never occurs...
093 return false;
094 }
095 return false;
096 }
097
098 /**
099 * Enables all the notifications the type of which starts with the specified prefix
100 * to be sent to the listener.
101 * <BR>If the specified prefix is already in the list of enabled notification types,
102 * this method has no effect.
103 * <P>
104 * Example:
105 * <BLOCKQUOTE>
106 * <PRE>
107 * // Enables all notifications the type of which starts with "my_example" to be sent.
108 * myFilter.enableType("my_example");
109 * // Enables all notifications the type of which is "my_example.my_type" to be sent.
110 * myFilter.enableType("my_example.my_type");
111 * </PRE>
112 * </BLOCKQUOTE>
113 *
114 * Note that:
115 * <BLOCKQUOTE><CODE>
116 * myFilter.enableType("my_example.*");
117 * </CODE></BLOCKQUOTE>
118 * will no match any notification type.
119 *
120 * @param prefix The prefix.
121 * @exception java.lang.IllegalArgumentException The prefix parameter is null.
122 */
123 public synchronized void enableType(String prefix)
124 throws IllegalArgumentException {
125
126 if (prefix == null) {
127 throw new IllegalArgumentException(
128 "The prefix cannot be null.");
129 }
130 if (!enabledTypes.contains(prefix)) {
131 enabledTypes.add(prefix);
132 }
133 }
134
135 /**
136 * Removes the given prefix from the prefix list.
137 * <BR>If the specified prefix is not in the list of enabled notification types,
138 * this method has no effect.
139 *
140 * @param prefix The prefix.
141 */
142 public synchronized void disableType(String prefix) {
143 enabledTypes.remove(prefix);
144 }
145
146 /**
147 * Disables all notification types.
148 */
149 public synchronized void disableAllTypes() {
150 enabledTypes.clear();
151 }
152
153 /**
154 * Gets all the enabled notification types for this filter.
155 *
156 * @return The list containing all the enabled notification types.
157 */
158 public synchronized Vector<String> getEnabledTypes() {
159 return (Vector<String>) enabledTypes;
160 }
161
162 }
|