001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package javax.management;
010:
011: import java.io.IOException;
012: import java.io.ObjectInputStream;
013: import java.io.ObjectOutputStream;
014: import java.io.ObjectStreamField;
015: import java.io.Serializable;
016: import java.io.StreamCorruptedException;
017: import java.util.HashSet;
018: import java.util.Vector;
019:
020: /**
021: * @version $Revision: 1.14 $
022: */
023: // Change not needed, workaround to a TCK bug only to achieve TCK compliance
024: // public class AttributeChangeNotificationFilter implements NotificationFilter
025: public class AttributeChangeNotificationFilter implements
026: NotificationFilter, Serializable {
027: private static final long serialVersionUID = -6347317584796410029L;
028: private static final String serialName = "enabledAttributes";
029:
030: /**
031: * @serialField enabledAttributes Vector The names of the attributes for which
032: * this filter will enable notification dispatching
033: */
034: private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(
035: serialName, Vector.class) };
036:
037: private HashSet enabledAttributes = new HashSet();
038:
039: public int hashCode() {
040: return enabledAttributes.hashCode();
041: }
042:
043: public boolean equals(Object obj) {
044: if (obj == null)
045: return false;
046: if (obj == this )
047: return true;
048:
049: try {
050: AttributeChangeNotificationFilter other = (AttributeChangeNotificationFilter) obj;
051: return getEnabledAttributes().equals(
052: other.getEnabledAttributes());
053: } catch (ClassCastException x) {
054: }
055: return false;
056: }
057:
058: public void enableAttribute(String name)
059: throws IllegalArgumentException {
060: if (name == null)
061: throw new IllegalArgumentException("Name cannot be null");
062: synchronized (enabledAttributes) {
063: enabledAttributes.add(name);
064: }
065: }
066:
067: public void disableAttribute(String name) {
068: if (name != null) {
069: synchronized (enabledAttributes) {
070: enabledAttributes.remove(name);
071: }
072: }
073: }
074:
075: public void disableAllAttributes() {
076: synchronized (enabledAttributes) {
077: enabledAttributes.clear();
078: }
079: }
080:
081: public Vector getEnabledAttributes() {
082: synchronized (enabledAttributes) {
083: return new Vector(enabledAttributes);
084: }
085: }
086:
087: public boolean isNotificationEnabled(Notification notification) {
088: if (!(notification instanceof AttributeChangeNotification))
089: return false;
090: AttributeChangeNotification n = (AttributeChangeNotification) notification;
091:
092: if (!AttributeChangeNotification.ATTRIBUTE_CHANGE.equals(n
093: .getType()))
094: return false;
095: String attributeName = n.getAttributeName();
096:
097: if (attributeName != null) {
098: synchronized (enabledAttributes) {
099: if (enabledAttributes.contains(attributeName))
100: return true;
101: }
102: }
103: return false;
104: }
105:
106: private void readObject(ObjectInputStream in) throws IOException,
107: ClassNotFoundException {
108: ObjectInputStream.GetField fields = in.readFields();
109:
110: Vector vector = (Vector) fields.get(serialName, null);
111: if (fields.defaulted(serialName)) {
112: throw new StreamCorruptedException(
113: "Serialized stream corrupted: expecting a non-null Vector");
114: }
115:
116: if (enabledAttributes == null)
117: enabledAttributes = new HashSet();
118: enabledAttributes.clear();
119: enabledAttributes.addAll(vector);
120: }
121:
122: private void writeObject(ObjectOutputStream out) throws IOException {
123: ObjectOutputStream.PutField fields = out.putFields();
124:
125: Vector vector = getEnabledAttributes();
126: fields.put(serialName, vector);
127: out.writeFields();
128: }
129: }
|