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.util.Vector;
030:
031: /**
032: * Provides an implementation of the {@link javax.management.NotificationFilter}
033: * interface. The filtering is performed on the notification type attribute.
034: * <P>
035: * This class manages a list of enabled notification types.
036: * A method allows users to enable/disable as many notification types as required.
037: * <P>
038: * Then, before sending a notification to a listener registered with a filter,
039: * the notification broadcaster compares this notification type with all
040: * notification types enabled by the filter. The notification will be sent to
041: * the listener only if its filter enables this notification type.
042: * <P>
043: * Example:
044: * <BLOCKQUOTE>
045: * <PRE>
046: * NotificationFilterSupport myFilter = new NotificationFilterSupport();
047: * myFilter.enableType("my_example.my_type");
048: * myBroadcaster.addListener(myListener, myFilter, null);
049: * </PRE>
050: * </BLOCKQUOTE>
051: * The listener <CODE>myListener</CODE> will only receive notifications
052: * the type of which equals/starts with "my_example.my_type".
053: *
054: * @see javax.management.NotificationBroadcaster#addNotificationListener
055: */
056: public class NotificationFilterSupport implements NotificationFilter,
057: Serializable {
058: /**
059: * Vector that contains the enabled notification types.
060: * The default value is an empty vector.
061: */
062: protected Vector enabledTypes = null;
063:
064: /**
065: * Creates a NotificationFilterSupport
066: * Default constructor
067: */
068: public NotificationFilterSupport() {
069: enabledTypes = new Vector();
070: }
071:
072: /**
073: * Invoked before sending the specified notification to the listener.
074: * This filter compares the notification type with each enabled type.
075: * If the notification type matches one of the enabled type, the
076: * notification has to be sent to the listener and this method returns true.
077: *
078: * @param notification The notification to be sent.
079: *
080: * @return True if the notification has to be sent to the listener, false otherwise.
081: */
082: public boolean isNotificationEnabled(Notification notification) {
083: if (notification == null)
084: return false;
085:
086: String type = notification.getType();
087:
088: for (int i = 0; i < enabledTypes.size(); i++) {
089: String str = (String) enabledTypes.elementAt(i);
090:
091: if (type.toLowerCase().startsWith(str.toLowerCase()))
092: return true;
093: }
094:
095: return false;
096: }
097:
098: /**
099: * Disables all the notification types.
100: */
101: public void disableAllTypes() {
102: enabledTypes.removeAllElements();
103: }
104:
105: /**
106: * Disables all the notifications whose type matches the specified prefix
107: * to be sent to the listener. If the specified prefix is not in the list
108: * of enabled notification types, this method has no effect.
109: *
110: * @param prefix The prefix.
111: */
112: public void disableType(String prefix) {
113: if (prefix == null)
114: return;
115:
116: for (int i = 0; i < enabledTypes.size(); i++) {
117: String str = (String) enabledTypes.elementAt(i);
118:
119: if (str.toLowerCase().startsWith(prefix.toLowerCase()))
120: enabledTypes.removeElementAt(i);
121: }
122: }
123:
124: /**
125: * Enables all the notifications whose type matches the specified prefix
126: * to be sent to the listener. If the specified prefix is already in the
127: * list of enabled notification types, this method has no effect.
128: *
129: * @param prefix The prefix.
130: *
131: * @throws IllegalArgumentException The prefix parameter is null.
132: */
133: public void enableType(String prefix)
134: throws IllegalArgumentException {
135: if (prefix == null)
136: throw new IllegalArgumentException(prefix);
137:
138: boolean flag = false;
139:
140: for (int i = 0; i < enabledTypes.size(); i++) {
141: String str = (String) enabledTypes.elementAt(i);
142:
143: if (prefix.toLowerCase().startsWith(str.toLowerCase()))
144: flag = true;
145: else if (str.toLowerCase().startsWith(prefix.toLowerCase()))
146: enabledTypes.removeElementAt(i);
147: }
148:
149: if (!flag)
150: enabledTypes.add(prefix);
151: }
152:
153: /**
154: * Gets all the enabled notification types for this filter.
155: *
156: * @return The list containing all the enabled notification types.
157: */
158: public Vector getEnabledTypes() {
159: return enabledTypes;
160: }
161: }
|