AttributeChangeNotificationFilter.java revision 10444:f08705540498
1/*
2 * Copyright (c) 1999, 2006, 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
26package javax.management;
27
28
29import java.util.Vector;
30
31
32/**
33 * This class implements of the {@link javax.management.NotificationFilter NotificationFilter}
34 * interface for the {@link javax.management.AttributeChangeNotification attribute change notification}.
35 * The filtering is performed on the name of the observed attribute.
36 * <P>
37 * It manages a list of enabled attribute names.
38 * A method allows users to enable/disable as many attribute names as required.
39 *
40 * @since 1.5
41 */
42public class AttributeChangeNotificationFilter implements NotificationFilter {
43
44    /* Serial version */
45    private static final long serialVersionUID = -6347317584796410029L;
46
47    /**
48     * @serial {@link Vector} that contains the enabled attribute names.
49     *         The default value is an empty vector.
50     */
51    private Vector<String> enabledAttributes = new Vector<String>();
52
53
54    /**
55     * Invoked before sending the specified notification to the listener.
56     * <BR>This filter compares the attribute name of the specified attribute change notification
57     * with each enabled attribute name.
58     * If the attribute name equals one of the enabled attribute names,
59     * the notification must be sent to the listener and this method returns <CODE>true</CODE>.
60     *
61     * @param notification The attribute change notification to be sent.
62     * @return <CODE>true</CODE> if the notification has to be sent to the listener, <CODE>false</CODE> otherwise.
63     */
64    public synchronized boolean isNotificationEnabled(Notification notification) {
65
66        String type = notification.getType();
67
68        if ((type == null) ||
69            (type.equals(AttributeChangeNotification.ATTRIBUTE_CHANGE) == false) ||
70            (!(notification instanceof AttributeChangeNotification))) {
71            return false;
72        }
73
74        String attributeName =
75          ((AttributeChangeNotification)notification).getAttributeName();
76        return enabledAttributes.contains(attributeName);
77    }
78
79    /**
80     * Enables all the attribute change notifications the attribute name of which equals
81     * the specified name to be sent to the listener.
82     * <BR>If the specified name is already in the list of enabled attribute names,
83     * this method has no effect.
84     *
85     * @param name The attribute name.
86     * @exception java.lang.IllegalArgumentException The attribute name parameter is null.
87     */
88    public synchronized void enableAttribute(String name) throws java.lang.IllegalArgumentException {
89
90        if (name == null) {
91            throw new java.lang.IllegalArgumentException("The name cannot be null.");
92        }
93        if (!enabledAttributes.contains(name)) {
94            enabledAttributes.addElement(name);
95        }
96    }
97
98    /**
99     * Disables all the attribute change notifications the attribute name of which equals
100     * the specified attribute name to be sent to the listener.
101     * <BR>If the specified name is not in the list of enabled attribute names,
102     * this method has no effect.
103     *
104     * @param name The attribute name.
105     */
106    public synchronized void disableAttribute(String name) {
107        enabledAttributes.removeElement(name);
108    }
109
110    /**
111     * Disables all the attribute names.
112     */
113    public synchronized void disableAllAttributes() {
114        enabledAttributes.removeAllElements();
115    }
116
117    /**
118     * Gets all the enabled attribute names for this filter.
119     *
120     * @return The list containing all the enabled attribute names.
121     */
122    public synchronized Vector<String> getEnabledAttributes() {
123        return enabledAttributes;
124    }
125
126}
127