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.relation;
008:
009: import java.io.IOException;
010: import java.io.ObjectInputStream;
011: import java.io.ObjectOutputStream;
012: import java.io.ObjectStreamField;
013: import java.util.HashSet;
014: import java.util.List;
015: import java.util.Vector;
016: import javax.management.MBeanServerNotification;
017: import javax.management.Notification;
018: import javax.management.NotificationFilterSupport;
019: import javax.management.ObjectName;
020:
021: /**
022: * @version $Revision: 1.7 $
023: */
024: public class MBeanServerNotificationFilter extends
025: NotificationFilterSupport {
026: private static final long serialVersionUID = 2605900539589789736L;
027: private static final String[] serialNames = { "selectedNames",
028: "deselectedNames" };
029: private static final ObjectStreamField[] serialPersistentFields = {
030: new ObjectStreamField(serialNames[0], List.class),
031: new ObjectStreamField(serialNames[1], List.class) };
032:
033: private HashSet m_disabledObjectNames;
034: private HashSet m_enabledObjectNames;
035:
036: public MBeanServerNotificationFilter() {
037: enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION);
038: enableType(MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
039:
040: // By default all disabled
041: disableAllObjectNames();
042: }
043:
044: public int hashCode() {
045: int result = (m_disabledObjectNames != null ? m_disabledObjectNames
046: .hashCode()
047: : 0);
048: result = 29
049: * result
050: + (m_enabledObjectNames != null ? m_enabledObjectNames
051: .hashCode() : 0);
052: return result;
053: }
054:
055: public boolean equals(Object obj) {
056: if (this == obj)
057: return true;
058: if (!(obj instanceof MBeanServerNotificationFilter))
059: return false;
060:
061: MBeanServerNotificationFilter other = (MBeanServerNotificationFilter) obj;
062:
063: if (m_disabledObjectNames != null ? !m_disabledObjectNames
064: .equals(other.m_disabledObjectNames)
065: : other.m_disabledObjectNames != null)
066: return false;
067: if (m_enabledObjectNames != null ? !m_enabledObjectNames
068: .equals(other.m_enabledObjectNames)
069: : other.m_enabledObjectNames != null)
070: return false;
071:
072: return true;
073: }
074:
075: public void disableAllObjectNames() {
076: // Clear the enabled ones, and...
077: if (m_enabledObjectNames == null) {
078: m_enabledObjectNames = new HashSet();
079: } else {
080: m_enabledObjectNames.clear();
081: }
082: // ...reset the disabled ones
083: m_disabledObjectNames = null;
084: }
085:
086: public void enableAllObjectNames() {
087: // Clear the disabled ones, and...
088: if (m_disabledObjectNames == null) {
089: m_disabledObjectNames = new HashSet();
090: } else {
091: m_disabledObjectNames.clear();
092: }
093: // ...reset the enabled ones
094: m_enabledObjectNames = null;
095: }
096:
097: public void enableObjectName(ObjectName name)
098: throws IllegalArgumentException {
099: if (name == null) {
100: throw new IllegalArgumentException(
101: "ObjectName cannot be null");
102: }
103:
104: // Remove from disabled if present
105: if (m_disabledObjectNames != null
106: && m_disabledObjectNames.size() > 0) {
107: m_disabledObjectNames.remove(name);
108: }
109:
110: // If not enableAll, add it to the list
111: if (m_enabledObjectNames != null) {
112: m_enabledObjectNames.add(name);
113: }
114: }
115:
116: public void disableObjectName(ObjectName name)
117: throws IllegalArgumentException {
118: if (name == null) {
119: throw new IllegalArgumentException(
120: "ObjectName cannot be null");
121: }
122:
123: // Remove from enabled if present
124: if (m_enabledObjectNames != null
125: && m_enabledObjectNames.size() > 0) {
126: m_enabledObjectNames.remove(name);
127: }
128:
129: // If not disableAll, add it to the list
130: if (m_disabledObjectNames != null) {
131: m_disabledObjectNames.add(name);
132: }
133: }
134:
135: public Vector getEnabledObjectNames() {
136: if (m_enabledObjectNames == null) {
137: return null;
138: }
139: Vector v = new Vector();
140: v.addAll(m_enabledObjectNames);
141: return v;
142: }
143:
144: public Vector getDisabledObjectNames() {
145: if (m_disabledObjectNames == null) {
146: return null;
147: }
148: Vector v = new Vector();
149: v.addAll(m_disabledObjectNames);
150: return v;
151: }
152:
153: public boolean isNotificationEnabled(Notification notification) {
154: boolean goOn = super .isNotificationEnabled(notification);
155:
156: if (goOn) {
157: if (notification instanceof MBeanServerNotification) {
158: MBeanServerNotification n = (MBeanServerNotification) notification;
159: ObjectName name = n.getMBeanName();
160:
161: if (m_enabledObjectNames == null) {
162: // All enabled, check the disabled ones
163: if (m_disabledObjectNames != null
164: && m_disabledObjectNames.contains(name)) {
165: // All enabled apart this one
166: return false;
167: } else {
168: return true;
169: }
170: } else {
171: // Only some is enabled
172: if (m_enabledObjectNames.contains(name)) {
173: // This one is enabled
174: return true;
175: } else {
176: // This one is not enabled
177: return false;
178: }
179: }
180: }
181: }
182:
183: return false;
184: }
185:
186: private void readObject(ObjectInputStream in) throws IOException,
187: ClassNotFoundException {
188: ObjectInputStream.GetField fields = in.readFields();
189:
190: Vector vector = (Vector) fields.get(serialNames[0], null);
191: if (fields.defaulted(serialNames[0])) {
192: throw new IOException(
193: "Serialized stream corrupted: expecting a non-null Vector");
194: }
195: if (vector != null) {
196: if (m_enabledObjectNames == null) {
197: m_enabledObjectNames = new HashSet();
198: }
199: m_enabledObjectNames.clear();
200: m_enabledObjectNames.addAll(vector);
201: }
202:
203: vector = (Vector) fields.get(serialNames[1], null);
204: if (fields.defaulted(serialNames[1])) {
205: throw new IOException(
206: "Serialized stream corrupted: expecting a non-null Vector");
207: }
208: if (vector != null) {
209: if (m_disabledObjectNames == null) {
210: m_disabledObjectNames = new HashSet();
211: }
212: m_disabledObjectNames.clear();
213: m_disabledObjectNames.addAll(vector);
214: }
215: }
216:
217: private void writeObject(ObjectOutputStream out) throws IOException {
218: ObjectOutputStream.PutField fields = out.putFields();
219:
220: Vector vector = getEnabledObjectNames();
221: fields.put(serialNames[0], vector);
222:
223: vector = getDisabledObjectNames();
224: fields.put(serialNames[1], vector);
225:
226: out.writeFields();
227: }
228: }
|