001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.tomcat.util.modeler;
019:
020: import java.util.HashSet;
021:
022: import javax.management.AttributeChangeNotification;
023: import javax.management.Notification;
024: import javax.management.NotificationFilter;
025:
026: /**
027: * <p>Implementation of <code>NotificationFilter</code> for attribute change
028: * notifications. This class is used by <code>BaseModelMBean</code> to
029: * construct attribute change notification event filters when a filter is not
030: * supplied by the application.</p>
031: *
032: * @author Craig R. McClanahan
033: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
034: */
035:
036: public class BaseAttributeFilter implements NotificationFilter {
037:
038: // ----------------------------------------------------------- Constructors
039:
040: /**
041: * Construct a new filter that accepts only the specified attribute
042: * name.
043: *
044: * @param name Name of the attribute to be accepted by this filter, or
045: * <code>null</code> to accept all attribute names
046: */
047: public BaseAttributeFilter(String name) {
048:
049: super ();
050: if (name != null)
051: addAttribute(name);
052:
053: }
054:
055: // ----------------------------------------------------- Instance Variables
056:
057: /**
058: * The set of attribute names that are accepted by this filter. If this
059: * list is empty, all attribute names are accepted.
060: */
061: private HashSet names = new HashSet();
062:
063: // --------------------------------------------------------- Public Methods
064:
065: /**
066: * Add a new attribute name to the set of names accepted by this filter.
067: *
068: * @param name Name of the attribute to be accepted
069: */
070: public void addAttribute(String name) {
071:
072: synchronized (names) {
073: names.add(name);
074: }
075:
076: }
077:
078: /**
079: * Clear all accepted names from this filter, so that it will accept
080: * all attribute names.
081: */
082: public void clear() {
083:
084: synchronized (names) {
085: names.clear();
086: }
087:
088: }
089:
090: /**
091: * Return the set of names that are accepted by this filter. If this
092: * filter accepts all attribute names, a zero length array will be
093: * returned.
094: */
095: public String[] getNames() {
096:
097: synchronized (names) {
098: return ((String[]) names.toArray(new String[names.size()]));
099: }
100:
101: }
102:
103: /**
104: * <p>Test whether notification enabled for this event.
105: * Return true if:</p>
106: * <ul>
107: * <li>This is an attribute change notification</li>
108: * <li>Either the set of accepted names is empty (implying that all
109: * attribute names are of interest) or the set of accepted names
110: * includes the name of the attribute in this notification</li>
111: * </ul>
112: */
113: public boolean isNotificationEnabled(Notification notification) {
114:
115: if (notification == null)
116: return (false);
117: if (!(notification instanceof AttributeChangeNotification))
118: return (false);
119: AttributeChangeNotification acn = (AttributeChangeNotification) notification;
120: if (!AttributeChangeNotification.ATTRIBUTE_CHANGE.equals(acn
121: .getType()))
122: return (false);
123: synchronized (names) {
124: if (names.size() < 1)
125: return (true);
126: else
127: return (names.contains(acn.getAttributeName()));
128: }
129:
130: }
131:
132: /**
133: * Remove an attribute name from the set of names accepted by this
134: * filter.
135: *
136: * @param name Name of the attribute to be removed
137: */
138: public void removeAttribute(String name) {
139:
140: synchronized (names) {
141: names.remove(name);
142: }
143:
144: }
145:
146: }
|