0N/A/*
2362N/A * Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.management;
0N/A
0N/A
0N/Aimport java.util.List;
0N/Aimport java.util.Vector;
0N/A
0N/A/**
0N/A * Provides an implementation of the {@link javax.management.NotificationFilter} interface.
0N/A * The filtering is performed on the notification type attribute.
0N/A * <P>
0N/A * Manages a list of enabled notification types.
0N/A * A method allows users to enable/disable as many notification types as required.
0N/A * <P>
0N/A * Then, before sending a notification to a listener registered with a filter,
0N/A * the notification broadcaster compares this notification type with all notification types
0N/A * enabled by the filter. The notification will be sent to the listener
0N/A * only if its filter enables this notification type.
0N/A * <P>
0N/A * Example:
0N/A * <BLOCKQUOTE>
0N/A * <PRE>
0N/A * NotificationFilterSupport myFilter = new NotificationFilterSupport();
0N/A * myFilter.enableType("my_example.my_type");
0N/A * myBroadcaster.addListener(myListener, myFilter, null);
0N/A * </PRE>
0N/A * </BLOCKQUOTE>
0N/A * The listener <CODE>myListener</CODE> will only receive notifications the type of which equals/starts with "my_example.my_type".
0N/A *
0N/A * @see javax.management.NotificationBroadcaster#addNotificationListener
0N/A *
0N/A * @since 1.5
0N/A */
0N/Apublic class NotificationFilterSupport implements NotificationFilter {
0N/A
0N/A /* Serial version */
0N/A private static final long serialVersionUID = 6579080007561786969L;
0N/A
0N/A /**
0N/A * @serial {@link Vector} that contains the enabled notification types.
0N/A * The default value is an empty vector.
0N/A */
0N/A private List<String> enabledTypes = new Vector<String>();
0N/A
0N/A
0N/A /**
0N/A * Invoked before sending the specified notification to the listener.
0N/A * <BR>This filter compares the type of the specified notification with each enabled type.
0N/A * If the notification type matches one of the enabled types,
0N/A * the notification should be sent to the listener and this method returns <CODE>true</CODE>.
0N/A *
0N/A * @param notification The notification to be sent.
0N/A * @return <CODE>true</CODE> if the notification should be sent to the listener, <CODE>false</CODE> otherwise.
0N/A */
0N/A public synchronized boolean isNotificationEnabled(Notification notification) {
0N/A
0N/A String type = notification.getType();
0N/A
0N/A if (type == null) {
0N/A return false;
0N/A }
0N/A try {
0N/A for (String prefix : enabledTypes) {
0N/A if (type.startsWith(prefix)) {
0N/A return true;
0N/A }
0N/A }
0N/A } catch (java.lang.NullPointerException e) {
0N/A // Should never occurs...
0N/A return false;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Enables all the notifications the type of which starts with the specified prefix
0N/A * to be sent to the listener.
0N/A * <BR>If the specified prefix is already in the list of enabled notification types,
0N/A * this method has no effect.
0N/A * <P>
0N/A * Example:
0N/A * <BLOCKQUOTE>
0N/A * <PRE>
0N/A * // Enables all notifications the type of which starts with "my_example" to be sent.
0N/A * myFilter.enableType("my_example");
0N/A * // Enables all notifications the type of which is "my_example.my_type" to be sent.
0N/A * myFilter.enableType("my_example.my_type");
0N/A * </PRE>
0N/A * </BLOCKQUOTE>
0N/A *
0N/A * Note that:
0N/A * <BLOCKQUOTE><CODE>
0N/A * myFilter.enableType("my_example.*");
0N/A * </CODE></BLOCKQUOTE>
0N/A * will no match any notification type.
0N/A *
0N/A * @param prefix The prefix.
0N/A * @exception java.lang.IllegalArgumentException The prefix parameter is null.
0N/A */
0N/A public synchronized void enableType(String prefix)
0N/A throws IllegalArgumentException {
0N/A
0N/A if (prefix == null) {
0N/A throw new IllegalArgumentException("The prefix cannot be null.");
0N/A }
0N/A if (!enabledTypes.contains(prefix)) {
0N/A enabledTypes.add(prefix);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Removes the given prefix from the prefix list.
0N/A * <BR>If the specified prefix is not in the list of enabled notification types,
0N/A * this method has no effect.
0N/A *
0N/A * @param prefix The prefix.
0N/A */
0N/A public synchronized void disableType(String prefix) {
0N/A enabledTypes.remove(prefix);
0N/A }
0N/A
0N/A /**
0N/A * Disables all notification types.
0N/A */
0N/A public synchronized void disableAllTypes() {
0N/A enabledTypes.clear();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Gets all the enabled notification types for this filter.
0N/A *
0N/A * @return The list containing all the enabled notification types.
0N/A */
0N/A public synchronized Vector<String> getEnabledTypes() {
0N/A return (Vector<String>)enabledTypes;
0N/A }
0N/A
0N/A}