1 /* 2 * Copyright (c) 1999, 2019, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * 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 @SuppressWarnings("serial") // Not statically typed as Serializable 68 private List<String> enabledTypes = new Vector<String>(); 69 70 71 /** 72 * Invoked before sending the specified notification to the listener. 73 * <BR>This filter compares the type of the specified notification with each enabled type. 74 * If the notification type matches one of the enabled types, 75 * the notification should be sent to the listener and this method returns <CODE>true</CODE>. 76 * 77 * @param notification The notification to be sent. 78 * @return <CODE>true</CODE> if the notification should be sent to the listener, <CODE>false</CODE> otherwise. 79 */ 80 public synchronized boolean isNotificationEnabled(Notification notification) { 81 82 String type = notification.getType(); 83 84 if (type == null) { 85 return false; 86 } 87 try { 88 for (String prefix : enabledTypes) { 89 if (type.startsWith(prefix)) { 90 return true; 91 } 92 } 93 } catch (java.lang.NullPointerException e) { 94 // Should never occurs... 95 return false; 96 } 97 return false; 98 } 99 100 /** 101 * Enables all the notifications the type of which starts with the specified prefix 102 * to be sent to the listener. 103 * <BR>If the specified prefix is already in the list of enabled notification types, 104 * this method has no effect. 105 * <P> 106 * Example: 107 * <BLOCKQUOTE> 108 * <PRE> 109 * // Enables all notifications the type of which starts with "my_example" to be sent. 110 * myFilter.enableType("my_example"); 111 * // Enables all notifications the type of which is "my_example.my_type" to be sent. 112 * myFilter.enableType("my_example.my_type"); 113 * </PRE> 114 * </BLOCKQUOTE> 115 * 116 * Note that: 117 * <BLOCKQUOTE><CODE> 118 * myFilter.enableType("my_example.*"); 119 * </CODE></BLOCKQUOTE> 120 * will no match any notification type. 121 * 122 * @param prefix The prefix. 123 * @exception java.lang.IllegalArgumentException The prefix parameter is null. 124 */ 125 public synchronized void enableType(String prefix) 126 throws IllegalArgumentException { 127 128 if (prefix == null) { 129 throw new IllegalArgumentException("The prefix cannot be null."); 130 } 131 if (!enabledTypes.contains(prefix)) { 132 enabledTypes.add(prefix); 133 } 134 } 135 136 /** 137 * Removes the given prefix from the prefix list. 138 * <BR>If the specified prefix is not in the list of enabled notification types, 139 * this method has no effect. 140 * 141 * @param prefix The prefix. 142 */ 143 public synchronized void disableType(String prefix) { 144 enabledTypes.remove(prefix); 145 } 146 147 /** 148 * Disables all notification types. 149 */ 150 public synchronized void disableAllTypes() { 151 enabledTypes.clear(); 152 } 153 154 155 /** 156 * Gets all the enabled notification types for this filter. 157 * 158 * @return The list containing all the enabled notification types. 159 */ 160 public synchronized Vector<String> getEnabledTypes() { 161 return (Vector<String>)enabledTypes; 162 } 163 164 }