src/java.management/share/classes/javax/management/NotificationFilterSupport.java
author darcy
Wed, 23 Oct 2019 13:01:40 -0700
changeset 58766 54ffb15c4839
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.* Reviewed-by: rriggs, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58766
54ffb15c4839 8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.*
darcy
parents: 47216
diff changeset
     2
 * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Provides an implementation of the {@link javax.management.NotificationFilter} interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * The filtering is performed on the notification type attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Manages a list of enabled notification types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * A method allows users to enable/disable as many notification types as required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Then, before sending a notification to a listener registered with a filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the notification broadcaster compares this notification type with all notification types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * enabled by the filter. The notification will be sent to the listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * only if its filter enables this notification type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <BLOCKQUOTE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * NotificationFilterSupport myFilter = new NotificationFilterSupport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * myFilter.enableType("my_example.my_type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * myBroadcaster.addListener(myListener, myFilter, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * </PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * </BLOCKQUOTE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The listener <CODE>myListener</CODE> will only receive notifications the type of which equals/starts with "my_example.my_type".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @see javax.management.NotificationBroadcaster#addNotificationListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class NotificationFilterSupport implements NotificationFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /* Serial version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final long serialVersionUID = 6579080007561786969L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @serial {@link Vector} that contains the enabled notification types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *         The default value is an empty vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
58766
54ffb15c4839 8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.*
darcy
parents: 47216
diff changeset
    67
    @SuppressWarnings("serial") // Not statically typed as Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private List<String> enabledTypes = new Vector<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Invoked before sending the specified notification to the listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <BR>This filter compares the type of the specified notification with each enabled type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * If the notification type matches one of the enabled types,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * the notification should be sent to the listener and this method returns <CODE>true</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param notification The notification to be sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @return <CODE>true</CODE> if the notification should be sent to the listener, <CODE>false</CODE> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public synchronized boolean isNotificationEnabled(Notification notification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        String type = notification.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            for (String prefix : enabledTypes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                if (type.startsWith(prefix)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } catch (java.lang.NullPointerException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            // Should never occurs...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Enables all the notifications the type of which starts with the specified prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * to be sent to the listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <BR>If the specified prefix is already in the list of enabled notification types,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * this method has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <BLOCKQUOTE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * // Enables all notifications the type of which starts with "my_example" to be sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * myFilter.enableType("my_example");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * // Enables all notifications the type of which is "my_example.my_type" to be sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * myFilter.enableType("my_example.my_type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * </PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * </BLOCKQUOTE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Note that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <BLOCKQUOTE><CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * myFilter.enableType("my_example.*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * </CODE></BLOCKQUOTE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * will no match any notification type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param prefix The prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @exception java.lang.IllegalArgumentException The prefix parameter is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public synchronized void enableType(String prefix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (prefix == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new IllegalArgumentException("The prefix cannot be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (!enabledTypes.contains(prefix)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            enabledTypes.add(prefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Removes the given prefix from the prefix list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <BR>If the specified prefix is not in the list of enabled notification types,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * this method has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param prefix The prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public synchronized void disableType(String prefix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        enabledTypes.remove(prefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Disables all notification types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public synchronized void disableAllTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        enabledTypes.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Gets all the enabled notification types for this filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @return The list containing all the enabled notification types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public synchronized Vector<String> getEnabledTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return (Vector<String>)enabledTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
}