001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package javax.management;
009:
010: import java.io.Serializable;
011: import java.util.Vector;
012: import java.util.ArrayList;
013: import java.util.List;
014: import java.util.Iterator;
015:
016: /**
017: * This class implements of the {@link javax.management.NotificationFilter NotificationFilter}
018: * interface for the {@link javax.management.AttributeChangeNotification attribute change notification}.
019: * The filtering is performed on the name of the observed attribute.
020: * <P>
021: * It manages a list of enabled attribute names.
022: * A method allows users to enable/disable as many attribute names as required.
023: *
024: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
025: */
026:
027: public class AttributeChangeNotificationFilter implements
028: NotificationFilter, Serializable {
029:
030: /**
031: * List that contains the enabled attribute names.
032: * The default value is an empty vector.
033: */
034: private List enabledAttributes = new ArrayList();
035:
036: /**
037: * Invoked before sending the specified notification to the listener.
038: * <BR>This filter compares the attribute name of the specified attribute change notification
039: * with each enabled attribute name.
040: * If the attribute name equals one of the enabled attribute names,
041: * the notification must be sent to the listener and this method returns <CODE>true</CODE>.
042: *
043: * @param notification The attribute change notification to be sent.
044: * @return <CODE>true</CODE> if the notification has to be sent to the listener, <CODE>false</CODE> otherwise.
045: */
046: public synchronized boolean isNotificationEnabled(
047: Notification notification) {
048:
049: String type = notification.getType();
050:
051: if ((type == null)
052: || (type
053: .equals(AttributeChangeNotification.ATTRIBUTE_CHANGE) == false)
054: || (!(notification instanceof AttributeChangeNotification))) {
055: return false;
056: }
057:
058: String attributeName = ((AttributeChangeNotification) notification)
059: .getAttributeName();
060: for (Iterator iter = enabledAttributes.iterator(); iter
061: .hasNext();) {
062: String name = (String) iter.next();
063: if (attributeName.equals(name)) {
064: return true;
065: }
066: }
067:
068: return false;
069: }
070:
071: /**
072: * Enables all the attribute change notifications the attribute name of which equals
073: * the specified name to be sent to the listener.
074: * <BR>If the specified name is already in the list of enabled attribute names,
075: * this method has no effect.
076: *
077: * @param name The attribute name.
078: * @exception java.lang.IllegalArgumentException The attribute name parameter is null.
079: */
080: public synchronized void enableAttribute(String name)
081: throws java.lang.IllegalArgumentException {
082:
083: if (name == null) {
084: throw new java.lang.IllegalArgumentException(
085: "The name cannot be null.");
086: }
087: if (!enabledAttributes.contains(name)) {
088: enabledAttributes.add(name);
089: }
090: }
091:
092: /**
093: * Disables all the attribute change notifications the attribute name of which equals
094: * the specified attribute name to be sent to the listener.
095: * <BR>If the specified name is not in the list of enabled attribute names,
096: * this method has no effect.
097: *
098: * @param name The attribute name.
099: */
100: public synchronized void disableAttribute(String name) {
101: enabledAttributes.remove(name);
102: }
103:
104: /**
105: * Disables all the attribute names.
106: */
107: public synchronized void disableAllAttributes() {
108: enabledAttributes.clear();
109: }
110:
111: /**
112: * Gets all the enabled attribute names for this filter.
113: *
114: * @return The list containing all the enabled attribute names.
115: */
116: public synchronized Vector getEnabledAttributes() {
117: return new Vector(enabledAttributes);
118: }
119:
120: }
|