1. /*
  2. * @(#)MBeanNotificationInfo.java 1.32 04/02/10
  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. import java.util.Arrays;
  9. /**
  10. * <p>The <CODE>MBeanNotificationInfo</CODE> class is used to describe the
  11. * characteristics of the different notification instances
  12. * emitted by an MBean, for a given Java class of notification.
  13. * If an MBean emits notifications that can be instances of different Java classes,
  14. * then the metadata for that MBean should provide an <CODE>MBeanNotificationInfo</CODE>
  15. * object for each of these notification Java classes.</p>
  16. *
  17. * <p>Instances of this class are immutable. Subclasses may be
  18. * mutable but this is not recommended.</p>
  19. *
  20. * <p>This class extends <CODE>javax.management.MBeanFeatureInfo</CODE>
  21. * and thus provides <CODE>name</CODE> and <CODE>description</CODE> fields.
  22. * The <CODE>name</CODE> field should be the fully qualified Java class name of
  23. * the notification objects described by this class.</p>
  24. *
  25. * <p>The <CODE>getNotifTypes</CODE> method returns an array of
  26. * strings containing the notification types that the MBean may
  27. * emit. The notification type is a dot-notation string which
  28. * describes what the emitted notification is about, not the Java
  29. * class of the notification. A single generic notification class can
  30. * be used to send notifications of several types. All of these types
  31. * are returned in the string array result of the
  32. * <CODE>getNotifTypes</CODE> method.
  33. *
  34. * @since 1.5
  35. */
  36. public class MBeanNotificationInfo extends MBeanFeatureInfo implements Cloneable, java.io.Serializable {
  37. /* Serial version */
  38. static final long serialVersionUID = -3888371564530107064L;
  39. private static final String[] NO_TYPES = new String[0];
  40. static final MBeanNotificationInfo[] NO_NOTIFICATIONS =
  41. new MBeanNotificationInfo[0];
  42. /**
  43. * @serial The different types of the notification.
  44. */
  45. private final String[] types;
  46. /** @see MBeanInfo#immutable */
  47. private final transient boolean immutable;
  48. /**
  49. * Constructs an <CODE>MBeanNotificationInfo</CODE> object.
  50. *
  51. * @param notifTypes The array of strings (in dot notation)
  52. * containing the notification types that the MBean may emit.
  53. * This may be null with the same effect as a zero-length array.
  54. * @param name The fully qualified Java class name of the
  55. * described notifications.
  56. * @param description A human readable description of the data.
  57. */
  58. public MBeanNotificationInfo(String[] notifTypes,
  59. String name,
  60. String description)
  61. throws IllegalArgumentException {
  62. super(name, description);
  63. /* We do not validate the notifTypes, since the spec just says
  64. they are dot-separated, not that they must look like Java
  65. classes. E.g. the spec doesn't forbid "sun.prob.25" as a
  66. notifType, though it doesn't explicitly allow it
  67. either. */
  68. if (notifTypes == null)
  69. notifTypes = NO_TYPES;
  70. this.types = notifTypes;
  71. this.immutable =
  72. MBeanInfo.isImmutableClass(this.getClass(),
  73. MBeanNotificationInfo.class);
  74. }
  75. /**
  76. * Returns a shallow clone of this instance.
  77. * The clone is obtained by simply calling <tt>super.clone()</tt>,
  78. * thus calling the default native shallow cloning mechanism
  79. * implemented by <tt>Object.clone()</tt>.
  80. * No deeper cloning of any internal field is made.
  81. */
  82. public Object clone () {
  83. try {
  84. return super.clone() ;
  85. } catch (CloneNotSupportedException e) {
  86. // should not happen as this class is cloneable
  87. return null;
  88. }
  89. }
  90. /**
  91. * Returns the array of strings (in dot notation) containing the
  92. * notification types that the MBean may emit.
  93. *
  94. * @return the array of strings. Changing the returned array has no
  95. * effect on this MBeanNotificationInfo.
  96. */
  97. public String[] getNotifTypes() {
  98. if (types.length == 0)
  99. return NO_TYPES;
  100. else
  101. return (String[]) types.clone();
  102. }
  103. private String[] fastGetNotifTypes() {
  104. if (immutable)
  105. return types;
  106. else
  107. return getNotifTypes();
  108. }
  109. /**
  110. * Compare this MBeanAttributeInfo to another.
  111. *
  112. * @param o the object to compare to.
  113. *
  114. * @return true iff <code>o</code> is an MBeanNotificationInfo
  115. * such that its {@link #getName()}, {@link #getDescription()},
  116. * and {@link #getNotifTypes()} values are equal (not necessarily
  117. * identical) to those of this MBeanNotificationInfo. Two
  118. * notification type arrays are equal if their corresponding
  119. * elements are equal. They are not equal if they have the same
  120. * elements but in a different order.
  121. */
  122. public boolean equals(Object o) {
  123. if (o == this)
  124. return true;
  125. if (!(o instanceof MBeanNotificationInfo))
  126. return false;
  127. MBeanNotificationInfo p = (MBeanNotificationInfo) o;
  128. return (p.getName().equals(getName()) &&
  129. p.getDescription().equals(getDescription()) &&
  130. Arrays.equals(p.fastGetNotifTypes(), fastGetNotifTypes()));
  131. }
  132. public int hashCode() {
  133. int hash = getName().hashCode();
  134. for (int i = 0; i < types.length; i++)
  135. hash ^= types[i].hashCode();
  136. return hash;
  137. }
  138. }