1. /*
  2. * @(#)ObjectInstance.java 4.21 04/03/18
  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.io.Serializable;
  10. // RI import
  11. import javax.management.ObjectName;
  12. /**
  13. * Used to represent the object name of an MBean and its class name.
  14. * If the MBean is a Dynamic MBean the class name should be retrieved from
  15. * the <CODE>MBeanInfo</CODE> it provides.
  16. *
  17. * @since 1.5
  18. */
  19. public class ObjectInstance implements Serializable {
  20. /* Serial version */
  21. private static final long serialVersionUID = -4099952623687795850L;
  22. /**
  23. * @serial Object name.
  24. */
  25. private ObjectName name;
  26. /**
  27. * @serial Class name.
  28. */
  29. private String className;
  30. /**
  31. * Allows an object instance to be created given a string representation of
  32. * an object name and the full class name, including the package name.
  33. *
  34. * @param objectName A string representation of the object name.
  35. * @param className The full class name, including the package
  36. * name, of the object instance. If the MBean is a Dynamic MBean
  37. * the class name corresponds to its {@link
  38. * DynamicMBean#getMBeanInfo()
  39. * getMBeanInfo()}<code>.getClassName()</code>.
  40. *
  41. * @exception MalformedObjectNameException The string passed as a
  42. * parameter does not have the right format.
  43. *
  44. */
  45. public ObjectInstance(String objectName, String className)
  46. throws MalformedObjectNameException {
  47. this(new ObjectName(objectName), className);
  48. }
  49. /**
  50. * Allows an object instance to be created given an object name and
  51. * the full class name, including the package name.
  52. *
  53. * @param objectName The object name.
  54. * @param className The full class name, including the package
  55. * name, of the object instance. If the MBean is a Dynamic MBean
  56. * the class name corresponds to its {@link
  57. * DynamicMBean#getMBeanInfo()
  58. * getMBeanInfo()}<code>.getClassName()</code>.
  59. * If the MBean is a Dynamic MBean the class name should be retrieved
  60. * from the <CODE>MBeanInfo</CODE> it provides.
  61. *
  62. */
  63. public ObjectInstance(ObjectName objectName, String className) {
  64. if (objectName.isPattern()) {
  65. final IllegalArgumentException iae =
  66. new IllegalArgumentException("Invalid name->"+
  67. objectName.toString());
  68. throw new RuntimeOperationsException(iae);
  69. }
  70. this.name= objectName;
  71. this.className= className;
  72. }
  73. /**
  74. * Compares the current object instance with another object instance.
  75. *
  76. * @param object The object instance that the current object instance is
  77. * to be compared with.
  78. *
  79. * @return True if the two object instances are equal, otherwise false.
  80. */
  81. public boolean equals(Object object) {
  82. if (!(object instanceof ObjectInstance)) {
  83. return false;
  84. }
  85. ObjectInstance val = (ObjectInstance) object;
  86. if (! name.equals(val.getObjectName())) return false;
  87. if (className == null)
  88. return (val.getClassName() == null);
  89. return className.equals(val.getClassName());
  90. }
  91. public int hashCode() {
  92. final int classHash = ((className==null)?0:className.hashCode());
  93. return name.hashCode() ^ classHash;
  94. }
  95. /**
  96. * Returns the object name part.
  97. *
  98. * @return the object name.
  99. */
  100. public ObjectName getObjectName() {
  101. return name;
  102. }
  103. /**
  104. * Returns the class part.
  105. *
  106. * @return the class name.
  107. */
  108. public String getClassName() {
  109. return className;
  110. }
  111. }