Source for javax.management.NotificationFilterSupport

   1: /* NotificationFilterSupport.java -- Filter on notification type.
   2:    Copyright (C) 2007 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package javax.management;
  39: 
  40: import java.util.Vector;
  41: 
  42: /**
  43:  * Performs filtering of {@link Notification}s
  44:  * based on a list of type prefixes.  The type of a notification
  45:  * is compared with each member of the list using
  46:  * {@link String#startsWith(String)} and, if one matches,
  47:  * the notification is allowed to pass through the filter.
  48:  * Matching on the beginning of the string is used in
  49:  * preference to wildcards, so <code>type.*</code> will
  50:  * match only notifications with a type beginning with
  51:  * code>type.*</code>, not <code>type.</code> as
  52:  * expected.
  53:  *
  54:  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  55:  * @since 1.5
  56:  */
  57: public class NotificationFilterSupport
  58:   implements NotificationFilter
  59: {
  60: 
  61:   /**
  62:    * Compatible with JDK 1.5
  63:    */
  64:   private static final long serialVersionUID = 6579080007561786969L;
  65: 
  66:   /**
  67:    * Lists the types that may pass through the filter.
  68:    */
  69:   private final Vector<String> enabledTypes = new Vector<String>();
  70: 
  71:   /**
  72:    * Blocks all types by emptying the list of enabled attributes.
  73:    */
  74:   public void disableAllTypes()
  75:   {
  76:     enabledTypes.clear();
  77:   }
  78: 
  79:   /**
  80:    * Removes the specified type prefix from the list
  81:    * of enabled types, thus preventing matching types
  82:    * from passing through the filter.  If the specified
  83:    * type prefix is not enabled, this operation has no
  84:    * effect.
  85:    *
  86:    * @param prefix the prefix to disable.
  87:    */
  88:   public void disableType(String prefix)
  89:   {
  90:     enabledTypes.remove(prefix);
  91:   }
  92: 
  93:   /**
  94:    * Adds the specified type prefix to the list
  95:    * of enabled types, thus allowing
  96:    * types starting with this string to pass through
  97:    * the filter.  If the type prefix is already
  98:    * enabled, this has no effect.
  99:    *
 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: }