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.relation;
023:
024: import java.io.IOException;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027: import java.io.ObjectStreamField;
028: import java.io.StreamCorruptedException;
029: import java.util.HashSet;
030: import java.util.List;
031: import java.util.Vector;
032:
033: import javax.management.MBeanServerNotification;
034: import javax.management.Notification;
035: import javax.management.NotificationFilterSupport;
036: import javax.management.ObjectName;
037:
038: import org.jboss.mx.util.Serialization;
039:
040: /**
041: * A helper class, used to filter notifications of registration,
042: * unregistration of selected object names.
043: *
044: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
045: * @version $Revision: 57200 $
046: *
047: * <p><b>Revisions:</b>
048: * <p><b>20020711 Adrian Brock:</b>
049: * <ul>
050: * <li> Serialization </li>
051: * </ul>
052: */
053: public class MBeanServerNotificationFilter extends
054: NotificationFilterSupport {
055: // Constants ---------------------------------------------------
056:
057: // Attributes --------------------------------------------------
058:
059: /**
060: * Enabled Object Names.
061: */
062: private HashSet enabled = new HashSet();
063:
064: /**
065: * Disable Object Names.
066: */
067: private HashSet disabled = null;
068:
069: // Static ------------------------------------------------------
070:
071: private static final long serialVersionUID;
072: private static final ObjectStreamField[] serialPersistentFields;
073:
074: static {
075: switch (Serialization.version) {
076: case Serialization.V1R0:
077: serialVersionUID = 6001782699077323605L;
078: serialPersistentFields = new ObjectStreamField[] {
079: new ObjectStreamField("myDeselectObjNameList",
080: Vector.class),
081: new ObjectStreamField("mySelectObjNameList",
082: Vector.class), };
083: break;
084: default:
085: serialVersionUID = 2605900539589789736L;
086: serialPersistentFields = new ObjectStreamField[] {
087: new ObjectStreamField("deselectedNames", List.class),
088: new ObjectStreamField("selectedNames", List.class) };
089: }
090: }
091:
092: // Constructors ------------------------------------------------
093:
094: /**
095: * Create a filter selecting nothing by default<p>
096: *
097: * WARNING!! WARNING!! The spec says the MBeanServerNotificationFilter
098: * accepts everything by default. The RI does exactly the opposite.
099: * I follow the RI.
100: */
101: public MBeanServerNotificationFilter() {
102: }
103:
104: // Public ------------------------------------------------------
105:
106: /**
107: * Disable all object names. Rejects all notifications.
108: */
109: public synchronized void disableAllObjectNames() {
110: enabled = new HashSet();
111: disabled = null;
112: }
113:
114: /**
115: * Disable an object name.
116: *
117: * @param objectName the object name to disable.
118: * @exception IllegalArgumentException for a null object name
119: */
120: public synchronized void disableObjectName(ObjectName objectName)
121: throws IllegalArgumentException {
122: if (objectName == null)
123: throw new IllegalArgumentException("null object name");
124: if (enabled != null)
125: enabled.remove(objectName);
126: if (disabled != null && disabled.contains(objectName) == false)
127: disabled.add(objectName);
128: }
129:
130: /**
131: * Enable all object names. Accepts all notifications.
132: */
133: public synchronized void enableAllObjectNames() {
134: enabled = null;
135: disabled = new HashSet();
136: }
137:
138: /**
139: * Enable an object name.
140: *
141: * @param objectName the object name to enable.
142: * @exception IllegalArgumentException for a null object name
143: */
144: public synchronized void enableObjectName(ObjectName objectName)
145: throws IllegalArgumentException {
146: if (objectName == null)
147: throw new IllegalArgumentException("null object name");
148: if (disabled != null)
149: disabled.remove(objectName);
150: if (enabled != null && enabled.contains(objectName) == false)
151: enabled.add(objectName);
152: }
153:
154: /**
155: * Get all the disabled object names.<p>
156: *
157: * Returns a vector of disabled object names.<br>
158: * Null for all object names disabled.
159: * An empty vector means all object names enabled.
160: *
161: * @return the vector of disabled object names.
162: */
163: public synchronized Vector getDisabledObjectNames() {
164: if (disabled == null)
165: return null;
166: return new Vector(disabled);
167: }
168:
169: /**
170: * Get all the enabled object names.<p>
171: *
172: * Returns a vector of enabled object names.<br>
173: * Null for all object names enabled.
174: * An empty vector means all object names disabled.
175: *
176: * @return the vector of enabled object names.
177: */
178: public synchronized Vector getEnabledObjectNames() {
179: if (enabled == null)
180: return null;
181: return new Vector(enabled);
182: }
183:
184: /**
185: * @return human readable string.
186: */
187: public String toString() {
188: StringBuffer buffer = new StringBuffer(100);
189: buffer.append(getClass().getName()).append(":");
190: buffer.append(" enabledTypes=").append(getEnabledTypes());
191: buffer.append(" enabledObjectNames=").append(
192: getEnabledObjectNames());
193: buffer.append(" disabledObjectNames=").append(
194: getDisabledObjectNames());
195: return buffer.toString();
196: }
197:
198: // NotificationFilterSupport overrides -------------------------
199:
200: /**
201: * Test to see whether this notification is enabled
202: *
203: * @param notification the notification to filter
204: * @return true when the notification should be sent, false otherwise
205: * @exception IllegalArgumentException for null notification.
206: */
207: public synchronized boolean isNotificationEnabled(
208: Notification notification) throws IllegalArgumentException {
209: if (notification == null)
210: throw new IllegalArgumentException("null notification");
211:
212: // Check the notification type
213: if (super .isNotificationEnabled(notification) == false)
214: return false;
215:
216: // Get the object name
217: MBeanServerNotification mbsNotification = (MBeanServerNotification) notification;
218: ObjectName objectName = mbsNotification.getMBeanName();
219:
220: // Is it enabled?
221: if (enabled != null)
222: return enabled.contains(objectName);
223:
224: // Is it not disabled?
225: if (disabled.contains(objectName) == false)
226: return true;
227:
228: // Disabled
229: return false;
230: }
231:
232: // Private -----------------------------------------------------
233:
234: private void readObject(ObjectInputStream ois) throws IOException,
235: ClassNotFoundException {
236: ObjectInputStream.GetField getField = ois.readFields();
237: List deselectedNames;
238: List selectedNames;
239: switch (Serialization.version) {
240: case Serialization.V1R0:
241: deselectedNames = (List) getField.get(
242: "myDeselectObjNameList", null);
243: selectedNames = (List) getField.get("mySelectObjNameList",
244: null);
245: break;
246: default:
247: deselectedNames = (List) getField.get("deselectedNames",
248: null);
249: selectedNames = (List) getField.get("selectedNames", null);
250: }
251: if (deselectedNames == null && selectedNames == null)
252: throw new StreamCorruptedException(
253: "Nothing enabled or disabled?");
254: if (deselectedNames == null)
255: disabled = null;
256: else
257: disabled = new HashSet(deselectedNames);
258: if (selectedNames == null)
259: enabled = null;
260: else
261: enabled = new HashSet(selectedNames);
262: }
263:
264: private void writeObject(ObjectOutputStream oos) throws IOException {
265: ObjectOutputStream.PutField putField = oos.putFields();
266: switch (Serialization.version) {
267: case Serialization.V1R0:
268: putField.put("myDeselectObjNameList",
269: getDisabledObjectNames());
270: putField
271: .put("mySelectObjNameList", getEnabledObjectNames());
272: break;
273: default:
274: putField.put("deselectedNames", getDisabledObjectNames());
275: putField.put("selectedNames", getEnabledObjectNames());
276: }
277: oos.writeFields();
278: }
279: }
|