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;
023:
024: import java.io.Serializable;
025: import java.util.Enumeration;
026: import java.util.Vector;
027:
028: /**
029: * An implementation of the {@link NotificationFilter} interface.<p>
030: *
031: * It filters on the notification type. It Maintains a list of enabled
032: * notification types. By default no notifications are enabled.<p>
033: *
034: * The enabled types are prefixes. That is a notification is enabled if
035: * it starts with an enabled string.
036: *
037: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
038: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>.
039: * @version $Revision: 57200 $
040: */
041: public class NotificationFilterSupport implements NotificationFilter,
042: Serializable {
043: // Constants ---------------------------------------------------
044:
045: private static final long serialVersionUID = 6579080007561786969L;
046:
047: // Attributes --------------------------------------------------
048:
049: /**
050: * Enabled notification types.
051: */
052: private Vector enabledTypes;
053:
054: // Static ------------------------------------------------------
055:
056: // Constructors ------------------------------------------------
057:
058: /**
059: * Create a filter that filters out all notification types.
060: */
061: public NotificationFilterSupport() {
062: enabledTypes = new Vector();
063: }
064:
065: // Public ------------------------------------------------------
066:
067: /**
068: * Disable all notification types. Rejects all notifications.
069: */
070: public synchronized void disableAllTypes() {
071: enabledTypes = new Vector();
072: }
073:
074: /**
075: * Disable a notification type.
076: *
077: * @param type the notification type to disable.
078: */
079: public synchronized void disableType(String type) {
080: // Null won't be in the list anyway.
081: enabledTypes.removeElement(type);
082: }
083:
084: /**
085: * Enable a notification type.
086: *
087: * @param type the notification type to enable.
088: * @exception IllegalArgumentException for a null type
089: */
090: public synchronized void enableType(String type)
091: throws IllegalArgumentException {
092: if (type == null)
093: throw new IllegalArgumentException("null notification type");
094: if (enabledTypes.contains(type) == false)
095: enabledTypes.addElement(type);
096: }
097:
098: /**
099: * Get all the enabled notification types.<p>
100: *
101: * Returns a vector of enabled notification type.<br>
102: * An empty vector means all types disabled.
103: *
104: * @return the vector of enabled types.
105: */
106: public synchronized Vector getEnabledTypes() {
107: return (Vector) enabledTypes.clone();
108: }
109:
110: /**
111: * @return human readable string.
112: */
113: public String toString() {
114: StringBuffer sb = new StringBuffer(100);
115:
116: sb.append(getClass().getName()).append(':');
117: sb.append(" enabledTypes=").append(getEnabledTypes());
118:
119: return sb.toString();
120: }
121:
122: // NotificationFilter implementation ---------------------------
123:
124: /**
125: * Test to see whether this notification is enabled
126: *
127: * @param notification the notification to filter
128: * @return true when the notification should be sent, false otherwise
129: * @exception IllegalArgumentException for null notification.
130: */
131: public synchronized boolean isNotificationEnabled(
132: Notification notification) {
133: if (notification == null)
134: throw new IllegalArgumentException("null notification");
135: // Is it enabled?
136: String notificationType = notification.getType();
137: for (Enumeration e = enabledTypes.elements(); e
138: .hasMoreElements();) {
139: String type = (String) e.nextElement();
140: if (notificationType.startsWith(type))
141: return true;
142: }
143: return false;
144: }
145:
146: // Private -----------------------------------------------------
147:
148: }
|