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