1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.modeler;
19
20
21 import java.util.HashSet;
22
23 import javax.management.AttributeChangeNotification;
24 import javax.management.Notification;
25 import javax.management.NotificationFilter;
26
27
28 /***
29 * <p>Implementation of <code>NotificationFilter</code> for attribute change
30 * notifications. This class is used by <code>BaseModelMBean</code> to
31 * construct attribute change notification event filters when a filter is not
32 * supplied by the application.</p>
33 *
34 * @author Craig R. McClanahan
35 * @version $Revision: 155428 $ $Date: 2005-02-26 08:12:25 -0500 (Sat, 26 Feb 2005) $
36 */
37
38 public class BaseAttributeFilter implements NotificationFilter {
39
40
41
42
43
44 /***
45 * Construct a new filter that accepts only the specified attribute
46 * name.
47 *
48 * @param name Name of the attribute to be accepted by this filter, or
49 * <code>null</code> to accept all attribute names
50 */
51 public BaseAttributeFilter(String name) {
52
53 super();
54 if (name != null)
55 addAttribute(name);
56
57 }
58
59
60
61
62
63 /***
64 * The set of attribute names that are accepted by this filter. If this
65 * list is empty, all attribute names are accepted.
66 */
67 private HashSet names = new HashSet();
68
69
70
71
72
73 /***
74 * Add a new attribute name to the set of names accepted by this filter.
75 *
76 * @param name Name of the attribute to be accepted
77 */
78 public void addAttribute(String name) {
79
80 synchronized (names) {
81 names.add(name);
82 }
83
84 }
85
86
87 /***
88 * Clear all accepted names from this filter, so that it will accept
89 * all attribute names.
90 */
91 public void clear() {
92
93 synchronized (names) {
94 names.clear();
95 }
96
97 }
98
99
100 /***
101 * Return the set of names that are accepted by this filter. If this
102 * filter accepts all attribute names, a zero length array will be
103 * returned.
104 */
105 public String[] getNames() {
106
107 synchronized (names) {
108 return ((String[]) names.toArray(new String[names.size()]));
109 }
110
111 }
112
113
114 /***
115 * <p>Test whether notification enabled for this event.
116 * Return true if:</p>
117 * <ul>
118 * <li>This is an attribute change notification</li>
119 * <li>Either the set of accepted names is empty (implying that all
120 * attribute names are of interest) or the set of accepted names
121 * includes the name of the attribute in this notification</li>
122 * </ul>
123 */
124 public boolean isNotificationEnabled(Notification notification) {
125
126 if (notification == null)
127 return (false);
128 if (!(notification instanceof AttributeChangeNotification))
129 return (false);
130 AttributeChangeNotification acn =
131 (AttributeChangeNotification) notification;
132 if (!AttributeChangeNotification.ATTRIBUTE_CHANGE.equals(acn.getType()))
133 return (false);
134 synchronized (names) {
135 if (names.size() < 1)
136 return (true);
137 else
138 return (names.contains(acn.getAttributeName()));
139 }
140
141 }
142
143
144 /***
145 * Remove an attribute name from the set of names accepted by this
146 * filter.
147 *
148 * @param name Name of the attribute to be removed
149 */
150 public void removeAttribute(String name) {
151
152 synchronized (names) {
153 names.remove(name);
154 }
155
156 }
157
158
159 }