J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package javax.management; |
| 27 | |
| 28 | |
| 29 | import java.util.List; |
| 30 | import java.util.Vector; |
| 31 | |
| 32 | /** |
| 33 | * Provides an implementation of the {@link javax.management.NotificationFilter} interface. |
| 34 | * The filtering is performed on the notification type attribute. |
| 35 | * <P> |
| 36 | * Manages a list of enabled notification types. |
| 37 | * A method allows users to enable/disable as many notification types as required. |
| 38 | * <P> |
| 39 | * Then, before sending a notification to a listener registered with a filter, |
| 40 | * the notification broadcaster compares this notification type with all notification types |
| 41 | * enabled by the filter. The notification will be sent to the listener |
| 42 | * only if its filter enables this notification type. |
| 43 | * <P> |
| 44 | * Example: |
| 45 | * <BLOCKQUOTE> |
| 46 | * <PRE> |
| 47 | * NotificationFilterSupport myFilter = new NotificationFilterSupport(); |
| 48 | * myFilter.enableType("my_example.my_type"); |
| 49 | * myBroadcaster.addListener(myListener, myFilter, null); |
| 50 | * </PRE> |
| 51 | * </BLOCKQUOTE> |
| 52 | * The listener <CODE>myListener</CODE> will only receive notifications the type of which equals/starts with "my_example.my_type". |
| 53 | * |
| 54 | * @see javax.management.NotificationBroadcaster#addNotificationListener |
| 55 | * |
| 56 | * @since 1.5 |
| 57 | */ |
| 58 | public class NotificationFilterSupport implements NotificationFilter { |
| 59 | |
| 60 | /* Serial version */ |
| 61 | private static final long serialVersionUID = 6579080007561786969L; |
| 62 | |
| 63 | /** |
| 64 | * @serial {@link Vector} that contains the enabled notification types. |
| 65 | * The default value is an empty vector. |
| 66 | */ |
| 67 | private List<String> enabledTypes = new Vector<String>(); |
| 68 | |
| 69 | |
| 70 | /** |
| 71 | * Invoked before sending the specified notification to the listener. |
| 72 | * <BR>This filter compares the type of the specified notification with each enabled type. |
| 73 | * If the notification type matches one of the enabled types, |
| 74 | * the notification should be sent to the listener and this method returns <CODE>true</CODE>. |
| 75 | * |
| 76 | * @param notification The notification to be sent. |
| 77 | * @return <CODE>true</CODE> if the notification should be sent to the listener, <CODE>false</CODE> otherwise. |
| 78 | */ |
| 79 | public synchronized boolean isNotificationEnabled(Notification notification) { |
| 80 | |
| 81 | String type = notification.getType(); |
| 82 | |
| 83 | if (type == null) { |
| 84 | return false; |
| 85 | } |
| 86 | try { |
| 87 | for (String prefix : enabledTypes) { |
| 88 | if (type.startsWith(prefix)) { |
| 89 | return true; |
| 90 | } |
| 91 | } |
| 92 | } catch (java.lang.NullPointerException e) { |
| 93 | // Should never occurs... |
| 94 | return false; |
| 95 | } |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Enables all the notifications the type of which starts with the specified prefix |
| 101 | * to be sent to the listener. |
| 102 | * <BR>If the specified prefix is already in the list of enabled notification types, |
| 103 | * this method has no effect. |
| 104 | * <P> |
| 105 | * Example: |
| 106 | * <BLOCKQUOTE> |
| 107 | * <PRE> |
| 108 | * // Enables all notifications the type of which starts with "my_example" to be sent. |
| 109 | * myFilter.enableType("my_example"); |
| 110 | * // Enables all notifications the type of which is "my_example.my_type" to be sent. |
| 111 | * myFilter.enableType("my_example.my_type"); |
| 112 | * </PRE> |
| 113 | * </BLOCKQUOTE> |
| 114 | * |
| 115 | * Note that: |
| 116 | * <BLOCKQUOTE><CODE> |
| 117 | * myFilter.enableType("my_example.*"); |
| 118 | * </CODE></BLOCKQUOTE> |
| 119 | * will no match any notification type. |
| 120 | * |
| 121 | * @param prefix The prefix. |
| 122 | * @exception java.lang.IllegalArgumentException The prefix parameter is null. |
| 123 | */ |
| 124 | public synchronized void enableType(String prefix) |
| 125 | throws IllegalArgumentException { |
| 126 | |
| 127 | if (prefix == null) { |
| 128 | throw new IllegalArgumentException("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 | /** |
| 155 | * Gets all the enabled notification types for this filter. |
| 156 | * |
| 157 | * @return The list containing all the enabled notification types. |
| 158 | */ |
| 159 | public synchronized Vector<String> getEnabledTypes() { |
| 160 | return (Vector<String>)enabledTypes; |
| 161 | } |
| 162 | |
| 163 | } |