001: /**
002: * The XMOJO Project 5
003: * Copyright © 2003 XMOJO.org. All rights reserved.
004:
005: * NO WARRANTY
006:
007: * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
008: * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
009: * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
010: * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
011: * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
012: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
013: * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
014: * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
015: * REPAIR OR CORRECTION.
016:
017: * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
018: * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
019: * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
020: * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
021: * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
022: * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
023: * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
024: * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
025: * SUCH DAMAGES.
026: **/package javax.management;
027:
028: import java.io.Serializable;
029: import java.io.ObjectInputStream;
030: import java.io.ObjectInputStream.GetField;
031: import java.io.ObjectStreamField;
032: import java.io.ObjectOutputStream;
033: import java.io.ObjectOutputStream.PutField;
034: import java.io.IOException;
035: import java.util.Vector;
036:
037: /**
038: * This class implements of the {@link javax.management.NotificationFilter NotificationFilter}
039: * interface for the {@link javax.management.AttributeChangeNotification attribute change notification}.
040: * The filtering is performed on the name of the observed attribute.
041: * <P>
042: * It manages a list of enabled attribute names.
043: * A method allows users to enable/disable as many attribute names as required.
044: */
045: public class AttributeChangeNotificationFilter implements
046: NotificationFilter, Serializable {
047: /**
048: * Vector that contains the enabled attribute names.
049: * The default value is an empty vector.
050: */
051: private Vector enabledAttributes = new Vector();
052:
053: /* Serial version UID */
054: private static final long serialVersionUID = 0xa7e9cc49419e9f53L;
055:
056: private static final ObjectStreamField[] serialPersistentFields = { new ObjectStreamField(
057: "enabledAttributes", java.util.Vector.class) };
058:
059: /**
060: * The noArg constructor .
061: */
062: public AttributeChangeNotificationFilter() {
063: //enabledAttributes = new Vector();
064: }
065:
066: /**
067: * Invoked before sending the specified notification to the listener.
068: * <BR>This filter compares the attribute name of the specified attribute
069: * change notification with each enabled attribute name. If the attribute
070: * name equals one of the enabled attribute names, the notification
071: * must be sent to the listener and this method returns <CODE>true</CODE>.
072: *
073: * @param notification The attribute change notification to be sent.
074: *
075: * @return <CODE>true</CODE> if the notification has to be sent to
076: * the listener, <CODE>false</CODE> otherwise.
077: */
078: public boolean isNotificationEnabled(Notification notification) {
079: if (notification == null)
080: return false;
081:
082: if (!(notification instanceof AttributeChangeNotification))
083: return false;
084:
085: String name = ((AttributeChangeNotification) notification)
086: .getAttributeName();
087:
088: for (int i = 0; i < enabledAttributes.size(); i++) {
089: String str = (String) enabledAttributes.elementAt(i);
090: if (name.equals(str))
091: return true;
092: }
093:
094: return false;
095: }
096:
097: /**
098: * Disables all the attribute names.
099: */
100: public void disableAllAttributes() {
101: enabledAttributes.removeAllElements();
102: }
103:
104: /**
105: * Disables all the attribute change notifications the attribute name of
106: * which equals the specified attribute name to be sent to the listener.
107: * <BR>If the specified name is not in the list of enabled attribute names,
108: * this method has no effect.
109: *
110: * @param name The attribute name.
111: */
112: public void disableAttribute(java.lang.String name) {
113: if (name == null)
114: return;
115:
116: for (int i = 0; i < enabledAttributes.size(); i++) {
117: String str = (String) enabledAttributes.elementAt(i);
118: if (name.equals(str))
119: enabledAttributes.removeElementAt(i);
120: }
121: }
122:
123: /**
124: * Enables all the attribute change notifications the attribute name of
125: * which equals the specified name to be sent to the listener.
126: * <BR>If the specified name is already in the list of enabled attribute
127: * names, this method has no effect.
128: *
129: * @param name The attribute name.
130: *
131: * @exception java.lang.IllegalArgumentException The attribute
132: * name parameter is null.
133: */
134: public void enableAttribute(java.lang.String name)
135: throws java.lang.IllegalArgumentException {
136: if (name == null)
137: throw new IllegalArgumentException(name);
138:
139: boolean flag = false;
140: for (int i = 0; i < enabledAttributes.size(); i++) {
141: String str = (String) enabledAttributes.elementAt(i);
142: if (name.equals(str))
143: flag = true;
144: }
145:
146: if (!flag)
147: enabledAttributes.add(name);
148: }
149:
150: /**
151: * Gets all the enabled attribute names for this filter.
152: *
153: * @return The list containing all the enabled attribute names.
154: */
155: public Vector getEnabledAttributes() {
156: return enabledAttributes;
157: }
158:
159: //-------------------------- Private methods ---------------------------//
160:
161: private void readObject(ObjectInputStream objectinputstream)
162: throws IOException, ClassNotFoundException {
163: ObjectInputStream.GetField getfield = objectinputstream
164: .readFields();
165:
166: try {
167: enabledAttributes = (Vector) getfield.get(
168: "enabledAttributes", null);
169: } catch (Exception exception) {
170: }
171: }
172:
173: private void writeObject(ObjectOutputStream objectoutputstream)
174: throws IOException {
175: ObjectOutputStream.PutField putfield = objectoutputstream
176: .putFields();
177: putfield.put("enabledAttributes", enabledAttributes);
178: objectoutputstream.writeFields();
179: }
180: }
|