1. /*
  2. * @(#)NotificationFilterSupport.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 imports
  9. //
  10. import java.util.Iterator;
  11. import java.util.List;
  12. import java.util.Vector;
  13. /**
  14. * Provides an implementation of the {@link javax.management.NotificationFilter} interface.
  15. * The filtering is performed on the notification type attribute.
  16. * <P>
  17. * Manages a list of enabled notification types.
  18. * A method allows users to enable/disable as many notification types as required.
  19. * <P>
  20. * Then, before sending a notification to a listener registered with a filter,
  21. * the notification broadcaster compares this notification type with all notification types
  22. * enabled by the filter. The notification will be sent to the listener
  23. * only if its filter enables this notification type.
  24. * <P>
  25. * Example:
  26. * <BLOCKQUOTE>
  27. * <PRE>
  28. * NotificationFilterSupport myFilter = new NotificationFilterSupport();
  29. * myFilter.enableType("my_example.my_type");
  30. * myBroadcaster.addListener(myListener, myFilter, null);
  31. * </PRE>
  32. * </BLOCKQUOTE>
  33. * The listener <CODE>myListener</CODE> will only receive notifications the type of which equals/starts with "my_example.my_type".
  34. *
  35. * @see javax.management.NotificationBroadcaster#addNotificationListener
  36. *
  37. * @since 1.5
  38. */
  39. public class NotificationFilterSupport implements NotificationFilter, java.io.Serializable {
  40. /* Serial version */
  41. private static final long serialVersionUID = 6579080007561786969L;
  42. /**
  43. * @serial {@link Vector} that contains the enabled notification types.
  44. * The default value is an empty vector.
  45. */
  46. private List enabledTypes = new Vector();
  47. /**
  48. * Invoked before sending the specified notification to the listener.
  49. * <BR>This filter compares the type of the specified notification with each enabled type.
  50. * If the notification type matches one of the enabled types,
  51. * the notification should be sent to the listener and this method returns <CODE>true</CODE>.
  52. *
  53. * @param notification The notification to be sent.
  54. * @return <CODE>true</CODE> if the notification should be sent to the listener, <CODE>false</CODE> otherwise.
  55. */
  56. public synchronized boolean isNotificationEnabled(Notification notification) {
  57. String type = notification.getType();
  58. if (type == null) {
  59. return false;
  60. }
  61. try {
  62. for (Iterator i = enabledTypes.iterator(); i.hasNext(); ) {
  63. String prefix = (String)i.next();
  64. if (type.startsWith(prefix)) {
  65. return true;
  66. }
  67. }
  68. } catch (java.lang.NullPointerException e) {
  69. // Should never occurs...
  70. return false;
  71. }
  72. return false;
  73. }
  74. /**
  75. * Enables all the notifications the type of which starts with the specified prefix
  76. * to be sent to the listener.
  77. * <BR>If the specified prefix is already in the list of enabled notification types,
  78. * this method has no effect.
  79. * <P>
  80. * Example:
  81. * <BLOCKQUOTE>
  82. * <PRE>
  83. * // Enables all notifications the type of which starts with "my_example" to be sent.
  84. * myFilter.enableType("my_example");
  85. * // Enables all notifications the type of which is "my_example.my_type" to be sent.
  86. * myFilter.enableType("my_example.my_type");
  87. * </PRE>
  88. * </BLOCKQUOTE>
  89. *
  90. * Note that:
  91. * <BLOCKQUOTE><CODE>
  92. * myFilter.enableType("my_example.*");
  93. * </CODE></BLOCKQUOTE>
  94. * will no match any notification type.
  95. *
  96. * @param prefix The prefix.
  97. * @exception java.lang.IllegalArgumentException The prefix parameter is null.
  98. */
  99. public synchronized void enableType(String prefix) throws java.lang.IllegalArgumentException {
  100. if (prefix == null) {
  101. throw new java.lang.IllegalArgumentException("The prefix cannot be null.");
  102. }
  103. if (!enabledTypes.contains(prefix)) {
  104. enabledTypes.add(prefix);
  105. }
  106. }
  107. /**
  108. * Removes the given prefix from the prefix list.
  109. * <BR>If the specified prefix is not in the list of enabled notification types,
  110. * this method has no effect.
  111. *
  112. * @param prefix The prefix.
  113. */
  114. public synchronized void disableType(String prefix) {
  115. enabledTypes.remove(prefix);
  116. }
  117. /**
  118. * Disables all notification types.
  119. */
  120. public synchronized void disableAllTypes() {
  121. enabledTypes.clear();
  122. }
  123. /**
  124. * Gets all the enabled notification types for this filter.
  125. *
  126. * @return The list containing all the enabled notification types.
  127. */
  128. public synchronized Vector getEnabledTypes() {
  129. return (Vector)enabledTypes;
  130. }
  131. }