001    /* NotificationFilterSupport.java -- Filter on notification type.
002       Copyright (C) 2007 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    package javax.management;
039    
040    import java.util.Vector;
041    
042    /**
043     * Performs filtering of {@link Notification}s
044     * based on a list of type prefixes.  The type of a notification
045     * is compared with each member of the list using
046     * {@link String#startsWith(String)} and, if one matches,
047     * the notification is allowed to pass through the filter.
048     * Matching on the beginning of the string is used in
049     * preference to wildcards, so <code>type.*</code> will
050     * match only notifications with a type beginning with
051     * code>type.*</code>, not <code>type.</code> as
052     * expected.
053     *
054     * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
055     * @since 1.5
056     */
057    public class NotificationFilterSupport
058      implements NotificationFilter
059    {
060    
061      /**
062       * Compatible with JDK 1.5
063       */
064      private static final long serialVersionUID = 6579080007561786969L;
065    
066      /**
067       * Lists the types that may pass through the filter.
068       */
069      private final Vector<String> enabledTypes = new Vector<String>();
070    
071      /**
072       * Blocks all types by emptying the list of enabled attributes.
073       */
074      public void disableAllTypes()
075      {
076        enabledTypes.clear();
077      }
078    
079      /**
080       * Removes the specified type prefix from the list
081       * of enabled types, thus preventing matching types
082       * from passing through the filter.  If the specified
083       * type prefix is not enabled, this operation has no
084       * effect.
085       *
086       * @param prefix the prefix to disable.
087       */
088      public void disableType(String prefix)
089      {
090        enabledTypes.remove(prefix);
091      }
092    
093      /**
094       * Adds the specified type prefix to the list
095       * of enabled types, thus allowing
096       * types starting with this string to pass through
097       * the filter.  If the type prefix is already
098       * enabled, this has no effect.
099       *
100       * @param prefix the prefix to enable.
101       * @throws IllegalArgumentException if <code>prefix</code>
102       *                                  is <code>null</code>.
103       */
104      public void enableType(String prefix)
105      {
106        if (prefix == null)
107          throw new IllegalArgumentException("A null prefix was supplied.");
108        if (!enabledTypes.contains(prefix))
109          enabledTypes.add(prefix);
110      }
111      
112      /**
113       * Returns the list of enabled types for this
114       * filter.
115       *
116       * @return the list of enabled types.
117       */
118      public Vector<String> getEnabledTypes()
119      {
120        return enabledTypes;
121      }
122    
123      /**
124       * Returns true if the type of the specified notification
125       * begins with one of the enabled type prefixes.
126       *
127       * @param notif the notification being filtered.
128       * @return true if the notification's type is enabled.
129       */
130      public boolean isNotificationEnabled(Notification notif)
131      {
132        String nType = notif.getType();
133        for (String type : enabledTypes)
134          if (nType.startsWith(type))
135            return true;
136        return false;
137      }
138    
139    }