001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package javax.management;
009:
010: import java.io.Serializable;
011: import java.util.List;
012: import java.util.Vector;
013: import java.util.Iterator;
014: import java.util.ArrayList;
015:
016: /**
017: * Provides an implementation of the {@link javax.management.NotificationFilter} interface.
018: * The filtering is performed on the notification type attribute.
019: * <P>
020: * Manages a list of enabled notification types.
021: * A method allows users to enable/disable as many notification types as required.
022: * <P>
023: * Then, before sending a notification to a listener registered with a filter,
024: * the notification broadcaster compares this notification type with all notification types
025: * enabled by the filter. The notification will be sent to the listener
026: * only if its filter enables this notification type.
027: * <P>
028: * Example:
029: * <BLOCKQUOTE>
030: * <PRE>
031: * NotificationFilterSupport myFilter = new NotificationFilterSupport();
032: * myFilter.enableType("my_example.my_type");
033: * myBroadcaster.addListener(myListener, myFilter, null);
034: * </PRE>
035: * </BLOCKQUOTE>
036: * The listener <CODE>myListener</CODE> will only receive notifications the type of which equals/starts with "my_example.my_type".
037: *
038: * @see javax.management.NotificationBroadcaster#addNotificationListener
039: *
040: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
041: */
042:
043: public class NotificationFilterSupport implements NotificationFilter,
044: Serializable {
045:
046: /**
047: * Vector that contains the enabled notification types.
048: * The default value is an empty vector.
049: */
050: private List enabledTypes = new ArrayList();
051:
052: /**
053: * Invoked before sending the specified notification to the listener.
054: * <BR>This filter compares the type of the specified notification with each enabled type.
055: * If the notification type matches one of the enabled types,
056: * the notification should be sent to the listener and this method returns <CODE>true</CODE>.
057: *
058: * @param notification The notification to be sent.
059: * @return <CODE>true</CODE> if the notification should be sent to the listener, <CODE>false</CODE> otherwise.
060: */
061: public synchronized boolean isNotificationEnabled(
062: Notification notification) {
063:
064: String type = notification.getType();
065: if (type == null)
066: return false;
067:
068: for (Iterator iter = enabledTypes.iterator(); iter.hasNext();) {
069: String prefix = (String) iter.next();
070: if (type.startsWith(prefix)) {
071: return true;
072: }
073: }
074:
075: return false;
076: }
077:
078: /**
079: * Enables all the notifications the type of which starts with the specified prefix
080: * to be sent to the listener.
081: * <BR>If the specified prefix is already in the list of enabled notification types,
082: * this method has no effect.
083: * <P>
084: * Example:
085: * <BLOCKQUOTE>
086: * <PRE>
087: * // Enables all notifications the type of which starts with "my_example" to be sent.
088: * myFilter.enableType("my_example");
089: * // Enables all notifications the type of which is "my_example.my_type" to be sent.
090: * myFilter.enableType("my_example.my_type");
091: * </PRE>
092: * </BLOCKQUOTE>
093: *
094: * Note that:
095: * <BLOCKQUOTE><CODE>
096: * myFilter.enableType("my_example.*");
097: * </CODE></BLOCKQUOTE>
098: * will no match any notification type.
099: *
100: * @param prefix The prefix.
101: * @exception java.lang.IllegalArgumentException The prefix parameter is null.
102: */
103: public synchronized void enableType(String prefix)
104: throws java.lang.IllegalArgumentException {
105:
106: if (prefix == null) {
107: throw new java.lang.IllegalArgumentException(
108: "The prefix cannot be null.");
109: }
110: if (!enabledTypes.contains(prefix)) {
111: enabledTypes.add(prefix);
112: }
113: }
114:
115: /**
116: * Disables all notifications the type of which starts with the specified prefix
117: * to be sent to the listener.
118: * <BR>If the specified prefix is not in the list of enabled notification types,
119: * this method has no effect.
120: *
121: * @param prefix The prefix.
122: */
123: public synchronized void disableType(String prefix) {
124: enabledTypes.remove(prefix);
125: }
126:
127: /**
128: * Disables all notification types.
129: */
130: public synchronized void disableAllTypes() {
131: enabledTypes.clear();
132: }
133:
134: /**
135: * Gets all the enabled notification types for this filter.
136: *
137: * @return The list containing all the enabled notification types.
138: */
139: public synchronized Vector getEnabledTypes() {
140: return new Vector(enabledTypes);
141: }
142:
143: }
|