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: */package javax.management;
008:
009: import java.io.IOException;
010: import java.io.ObjectInputStream;
011: import java.io.ObjectOutputStream;
012: import java.io.ObjectStreamField;
013: import java.io.Serializable;
014: import java.util.HashSet;
015: import java.util.Iterator;
016: import java.util.List;
017: import java.util.Vector;
018:
019: /**
020: * Default implementation of a NotificationListener that filters out Notifications that
021: * does not match the types enabled in this filter.
022: *
023: * @version $Revision: 1.11 $
024: */
025: // Change not needed, workaround to a TCK bug only to achieve TCK compliance
026: // public class NotificationFilterSupport implements NotificationFilter
027: public class NotificationFilterSupport implements NotificationFilter,
028: Serializable {
029: private static final long serialVersionUID = 6579080007561786969L;
030:
031: private static final String serialName = "enabledTypes";
032: /**
033: * @serialField enabledTypes List The list of notification types that this filter will not filter out
034: */
035: private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(
036: serialName, List.class) };
037:
038: private HashSet types = new HashSet();
039:
040: public boolean equals(Object o) {
041: if (this == o)
042: return true;
043: if (!(o instanceof NotificationFilterSupport))
044: return false;
045:
046: final NotificationFilterSupport support = (NotificationFilterSupport) o;
047:
048: if (!types.equals(support.types))
049: return false;
050:
051: return true;
052: }
053:
054: public int hashCode() {
055: return types.hashCode();
056: }
057:
058: /**
059: * Allows the given notification type to be received by listeners
060: *
061: * @param type The notification type to enable
062: * @throws IllegalArgumentException If the notification type is null
063: */
064: public void enableType(String type) throws IllegalArgumentException {
065: if (type == null)
066: throw new IllegalArgumentException("Null notification type");
067: synchronized (types) {
068: types.add(type);
069: }
070: }
071:
072: /**
073: * Forbids all notification types to be received by listeners
074: */
075: public void disableAllTypes() {
076: synchronized (types) {
077: types.clear();
078: }
079: }
080:
081: /**
082: * Forbids the gven notification type to be received by listeners
083: *
084: * @param type The notification type to disable
085: */
086: public void disableType(String type) {
087: synchronized (types) {
088: types.remove(type);
089: }
090: }
091:
092: /**
093: * Returns the notification type that are not filtered out by this filter
094: */
095: public Vector getEnabledTypes() {
096: Vector v = new Vector();
097: synchronized (types) {
098: v.addAll(types);
099: }
100: return v;
101: }
102:
103: /**
104: * Filters out notifications whose type is not enabled in this filter.
105: *
106: * @param notification The notification to filter
107: * @return True if the notification should be delivered to the listener, false otherwise
108: */
109: public boolean isNotificationEnabled(Notification notification) {
110: String type = notification.getType();
111: if (type != null) {
112: for (Iterator i = getEnabledTypes().iterator(); i.hasNext();) {
113: String t = (String) i.next();
114: if (type.startsWith(t))
115: return true;
116: }
117: }
118: return false;
119: }
120:
121: private void readObject(ObjectInputStream in) throws IOException,
122: ClassNotFoundException {
123: ObjectInputStream.GetField fields = in.readFields();
124:
125: Vector vector = (Vector) fields.get(serialName, null);
126: if (fields.defaulted(serialName)) {
127: throw new IOException(
128: "Serialized stream corrupted: expecting a non-null Vector");
129: }
130:
131: if (types == null)
132: types = new HashSet();
133: types.clear();
134: types.addAll(vector);
135: }
136:
137: private void writeObject(ObjectOutputStream out) throws IOException {
138: ObjectOutputStream.PutField fields = out.putFields();
139:
140: Vector vector = getEnabledTypes();
141: fields.put(serialName, vector);
142: out.writeFields();
143: }
144: }
|