001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package javax.management;
023:
024: import java.io.IOException;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027: import java.io.ObjectStreamField;
028:
029: import java.util.Set;
030: import java.util.HashSet;
031: import java.util.Vector;
032:
033: /**
034: * Notification filter support for attribute change notifications.
035: *
036: * @see javax.management.AttributeChangeNotification
037: * @see javax.management.NotificationFilter
038: *
039: * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
040: * @author a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
041: * @version $Revision: 57200 $
042: *
043: * <p><b>Revisions:</b>
044: * <p><b>20020710 Adrian Brock:</b>
045: * <ul>
046: * <li> Serialization </li>
047: * </ul>
048: */
049: public class AttributeChangeNotificationFilter implements
050: NotificationFilter, java.io.Serializable {
051:
052: // Constants -----------------------------------------------------
053:
054: private static final long serialVersionUID = -6347317584796410029L;
055: private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[] { new ObjectStreamField(
056: "enabledAttributes", Vector.class) };
057:
058: // Attributes ----------------------------------------------------
059: private Set attributes = new HashSet();
060:
061: // Constructors --------------------------------------------------
062:
063: /**
064: * Constructs an attribute change notification filter. All attribute
065: * notifications are filtered by default. Use {@link #enableAttribute}
066: * to enable notifications of a given attribute to pass this filter.
067: */
068: public AttributeChangeNotificationFilter() {
069: }
070:
071: // NotificationFilter implementation -----------------------------
072:
073: public synchronized boolean isNotificationEnabled(
074: Notification notification) {
075: if (notification != null
076: && notification instanceof AttributeChangeNotification) {
077: AttributeChangeNotification notif = (AttributeChangeNotification) notification;
078: if (attributes.contains(notif.getAttributeName()))
079: return true;
080: }
081:
082: return false;
083: }
084:
085: // Public --------------------------------------------------------
086:
087: /**
088: * Enables the attribute change notifications of the given attribute to be
089: * sent to the listener.
090: *
091: * @param name name of the management attribute
092: */
093: public synchronized void enableAttribute(String name)
094: throws IllegalArgumentException {
095: if (name == null)
096: throw new IllegalArgumentException("Null attribute name");
097:
098: attributes.add(name);
099: }
100:
101: /**
102: * Disable the attribute change notifications of the given attribute.
103: * Attribute change notifications for this attribute will not be sent to
104: * the listener.
105: *
106: * @param name name of the management attribute
107: */
108: public synchronized void disableAttribute(String name) {
109: attributes.remove(name);
110: }
111:
112: /**
113: * Disables all attribute change notifications.
114: */
115: public synchronized void disableAllAttributes() {
116: attributes.clear();
117: }
118:
119: /**
120: * Returns the names of the attributes whose notifications are allowed to
121: * pass this filter.
122: *
123: * @return a vector containing the name strings of the enabled attributes
124: */
125: public synchronized Vector getEnabledAttributes() {
126: return new Vector(attributes);
127: }
128:
129: /**
130: * @return human readable string.
131: */
132: public String toString() {
133: StringBuffer buffer = new StringBuffer(100);
134: buffer.append(getClass().getName()).append(":");
135: buffer.append(" enabledAttributes=").append(
136: getEnabledAttributes());
137: return buffer.toString();
138: }
139:
140: // Private -------------------------------------------------------
141:
142: private void readObject(ObjectInputStream ois) throws IOException,
143: ClassNotFoundException {
144: ObjectInputStream.GetField getField = ois.readFields();
145: Vector enabled = (Vector) getField.get("enabledAttributes",
146: null);
147: attributes = new HashSet(enabled);
148: }
149:
150: private void writeObject(ObjectOutputStream oos) throws IOException {
151: ObjectOutputStream.PutField putField = oos.putFields();
152: putField.put("enabledAttributes", new Vector(attributes));
153: oos.writeFields();
154: }
155: }
|