1. /*
  2. * @(#)AttributeChangeNotificationFilter.java 4.25 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.management;
  8. // java import
  9. import java.util.Enumeration;
  10. import java.util.Vector;
  11. /**
  12. * This class implements of the {@link javax.management.NotificationFilter NotificationFilter}
  13. * interface for the {@link javax.management.AttributeChangeNotification attribute change notification}.
  14. * The filtering is performed on the name of the observed attribute.
  15. * <P>
  16. * It manages a list of enabled attribute names.
  17. * A method allows users to enable/disable as many attribute names as required.
  18. *
  19. * @since 1.5
  20. */
  21. public class AttributeChangeNotificationFilter implements NotificationFilter, java.io.Serializable {
  22. /* Serial version */
  23. private static final long serialVersionUID = -6347317584796410029L;
  24. /**
  25. * @serial {@link Vector} that contains the enabled attribute names.
  26. * The default value is an empty vector.
  27. */
  28. private Vector enabledAttributes = new Vector();
  29. /**
  30. * Invoked before sending the specified notification to the listener.
  31. * <BR>This filter compares the attribute name of the specified attribute change notification
  32. * with each enabled attribute name.
  33. * If the attribute name equals one of the enabled attribute names,
  34. * the notification must be sent to the listener and this method returns <CODE>true</CODE>.
  35. *
  36. * @param notification The attribute change notification to be sent.
  37. * @return <CODE>true</CODE> if the notification has to be sent to the listener, <CODE>false</CODE> otherwise.
  38. */
  39. public synchronized boolean isNotificationEnabled(Notification notification) {
  40. String type = notification.getType();
  41. if ((type == null) ||
  42. (type.equals(AttributeChangeNotification.ATTRIBUTE_CHANGE) == false) ||
  43. (!(notification instanceof AttributeChangeNotification))) {
  44. return false;
  45. }
  46. String attributeName =
  47. ((AttributeChangeNotification)notification).getAttributeName();
  48. return enabledAttributes.contains(attributeName);
  49. }
  50. /**
  51. * Enables all the attribute change notifications the attribute name of which equals
  52. * the specified name to be sent to the listener.
  53. * <BR>If the specified name is already in the list of enabled attribute names,
  54. * this method has no effect.
  55. *
  56. * @param name The attribute name.
  57. * @exception java.lang.IllegalArgumentException The attribute name parameter is null.
  58. */
  59. public synchronized void enableAttribute(String name) throws java.lang.IllegalArgumentException {
  60. if (name == null) {
  61. throw new java.lang.IllegalArgumentException("The name cannot be null.");
  62. }
  63. if (!enabledAttributes.contains(name)) {
  64. enabledAttributes.addElement(name);
  65. }
  66. }
  67. /**
  68. * Disables all the attribute change notifications the attribute name of which equals
  69. * the specified attribute name to be sent to the listener.
  70. * <BR>If the specified name is not in the list of enabled attribute names,
  71. * this method has no effect.
  72. *
  73. * @param name The attribute name.
  74. */
  75. public synchronized void disableAttribute(String name) {
  76. enabledAttributes.removeElement(name);
  77. }
  78. /**
  79. * Disables all the attribute names.
  80. */
  81. public synchronized void disableAllAttributes() {
  82. enabledAttributes.removeAllElements();
  83. }
  84. /**
  85. * Gets all the enabled attribute names for this filter.
  86. *
  87. * @return The list containing all the enabled attribute names.
  88. */
  89. public synchronized Vector getEnabledAttributes() {
  90. return enabledAttributes;
  91. }
  92. }