/* * @(#)OpenMBeanInfoSupport.java 3.22 03/12/19 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.management.openmbean; // java import // import java.io.Serializable; import java.util.Arrays; import java.util.HashSet; // jmx import // import javax.management.MBeanInfo; import javax.management.MBeanAttributeInfo; import javax.management.MBeanOperationInfo; import javax.management.MBeanConstructorInfo; import javax.management.MBeanNotificationInfo; /** * The OpenMBeanInfoSupport class describes the management information of an open MBean: * it is a subclass of {@link javax.management.MBeanInfo}, and it implements the {@link OpenMBeanInfo} interface. * Note that an open MBean is recognized as such if its getMBeanInfo() method returns an instance of a class * which implements the OpenMBeanInfo interface, typically OpenMBeanInfoSupport. * * @version 3.22 03/12/19 * @author Sun Microsystems, Inc. * * @since 1.5 * @since.unbundled JMX 1.1 */ public class OpenMBeanInfoSupport extends MBeanInfo implements OpenMBeanInfo, Serializable { /* Serial version */ static final long serialVersionUID = 4349395935420511492L; private transient Integer myHashCode = null; // As this instance is immutable, these two values private transient String myToString = null; // need only be calculated once. /** * Constructs an OpenMBeanInfoSupport instance, * which describes a class of open MBeans with the specified * className, description, openAttributes, * openConstructors , openOperations and notifications. *

* The openAttributes, openConstructors, * openOperations and notifications * array parameters are internally copied, so that subsequent changes * to the arrays referenced by these parameters have no effect on this instance. *

* * * @param className The fully qualified Java class name of * the open MBean described by this OpenMBeanInfoSupport instance. * * @param description A human readable description of * the open MBean described by this OpenMBeanInfoSupport instance. * * @param openAttributes The list of exposed attributes of the described open MBean; * Must be an array of instances of a subclass of MBeanAttributeInfo, * typically OpenMBeanAttributeInfoSupport. * * @param openConstructors The list of exposed public constructors of the described open MBean; * Must be an array of instances of a subclass of MBeanConstructorInfo, * typically OpenMBeanConstructorInfoSupport. * * @param openOperations The list of exposed operations of the described open MBean. * Must be an array of instances of a subclass of MBeanOperationInfo, * typically OpenMBeanOperationInfoSupport. * * @param notifications The list of notifications emitted by the described open MBean. * * @throws ArrayStoreException If openAttributes, openConstructors or openOperations * is not an array of instances of a subclass of MBeanAttributeInfo, * MBeanConstructorInfo or MBeanOperationInfo respectively. */ public OpenMBeanInfoSupport(String className, String description, OpenMBeanAttributeInfo[] openAttributes, OpenMBeanConstructorInfo[] openConstructors, OpenMBeanOperationInfo[] openOperations, MBeanNotificationInfo[] notifications) { super(className, description, ( openAttributes == null ? null : attributesArrayCopyCast(openAttributes) ), // may throw an ArrayStoreException ( openConstructors == null ? null : constructorsArrayCopyCast(openConstructors) ), // may throw an ArrayStoreException ( openOperations == null ? null : operationsArrayCopyCast(openOperations) ), // may throw an ArrayStoreException ( notifications == null ? null : notificationsArrayCopy(notifications) )); } private static MBeanAttributeInfo[] attributesArrayCopyCast(OpenMBeanAttributeInfo[] src) throws ArrayStoreException { MBeanAttributeInfo[] dst = new MBeanAttributeInfo[src.length]; System.arraycopy(src, 0, dst, 0, src.length); // may throw an ArrayStoreException return dst; } private static MBeanConstructorInfo[] constructorsArrayCopyCast(OpenMBeanConstructorInfo[] src) throws ArrayStoreException { MBeanConstructorInfo[] dst = new MBeanConstructorInfo[src.length]; System.arraycopy(src, 0, dst, 0, src.length); // may throw an ArrayStoreException return dst; } private static MBeanOperationInfo[] operationsArrayCopyCast(OpenMBeanOperationInfo[] src) throws ArrayStoreException { MBeanOperationInfo[] dst = new MBeanOperationInfo[src.length]; System.arraycopy(src, 0, dst, 0, src.length); // may throw an ArrayStoreException return dst; } private static MBeanNotificationInfo[] notificationsArrayCopy(MBeanNotificationInfo[] src) { MBeanNotificationInfo[] dst = new MBeanNotificationInfo[src.length]; System.arraycopy(src, 0, dst, 0, src.length); return dst; } /* *** Commodity methods from java.lang.Object *** */ /** * Compares the specified obj parameter with this OpenMBeanInfoSupport instance for equality. *

* Returns true if and only if all of the following statements are true: *

* This ensures that this equals method works properly for obj parameters which are * different implementations of the OpenMBeanInfo interface. *
  * @param obj the object to be compared for equality with this OpenMBeanInfoSupport instance; * * @return true if the specified object is equal to this OpenMBeanInfoSupport instance. */ public boolean equals(Object obj) { // if obj is null, return false // if (obj == null) { return false; } // if obj is not a OpenMBeanInfo, return false // OpenMBeanInfo other; try { other = (OpenMBeanInfo) obj; } catch (ClassCastException e) { return false; } // Now, really test for equality between this OpenMBeanInfo implementation and the other: // // their MBean className should be equal if ( ! this.getClassName().equals(other.getClassName()) ) { return false; } // their infos on attributes should be equal (order not significant => equality between sets, not arrays or lists) if ( ! new HashSet(Arrays.asList(this.getAttributes())).equals(new HashSet(Arrays.asList(other.getAttributes()))) ) { return false; } // their infos on constructors should be equal (order not significant => equality between sets, not arrays or lists) if ( ! new HashSet(Arrays.asList(this.getConstructors())).equals(new HashSet(Arrays.asList(other.getConstructors()))) ) { return false; } // their infos on operations should be equal (order not significant => equality between sets, not arrays or lists) if ( ! new HashSet(Arrays.asList(this.getOperations())).equals(new HashSet(Arrays.asList(other.getOperations()))) ) { return false; } // their infos on notifications should be equal (order not significant => equality between sets, not arrays or lists) if ( ! new HashSet(Arrays.asList(this.getNotifications())).equals(new HashSet(Arrays.asList(other.getNotifications()))) ) { return false; } // All tests for equality were successfull // return true; } /** * Returns the hash code value for this OpenMBeanInfoSupport instance. *

* The hash code of an OpenMBeanInfoSupport instance is the sum of the hash codes * of all elements of information used in equals comparisons * (ie: its class name, and its infos on attributes, constructors, operations and notifications, * where the hashCode of each of these arrays is calculated by a call to * new java.util.HashSet(java.util.Arrays.asList(this.getSignature)).hashCode()). *

* This ensures that t1.equals(t2) implies that t1.hashCode()==t2.hashCode() * for any two OpenMBeanInfoSupport instances t1 and t2, * as required by the general contract of the method * {@link Object#hashCode() Object.hashCode()}. *

* However, note that another instance of a class implementing the OpenMBeanInfo interface * may be equal to this OpenMBeanInfoSupport instance as defined by {@link #equals(java.lang.Object)}, * but may have a different hash code if it is calculated differently. *

* As OpenMBeanInfoSupport instances are immutable, the hash code for this instance is calculated once, * on the first call to hashCode, and then the same value is returned for subsequent calls. * * @return the hash code value for this OpenMBeanInfoSupport instance */ public int hashCode() { // Calculate the hash code value if it has not yet been done (ie 1st call to hashCode()) // if (myHashCode == null) { int value = 0; value += this.getClassName().hashCode(); value += new HashSet(Arrays.asList(this.getAttributes())).hashCode(); value += new HashSet(Arrays.asList(this.getConstructors())).hashCode(); value += new HashSet(Arrays.asList(this.getOperations())).hashCode(); value += new HashSet(Arrays.asList(this.getNotifications())).hashCode(); myHashCode = new Integer(value); } // return always the same hash code for this instance (immutable) // return myHashCode.intValue(); } /** * Returns a string representation of this OpenMBeanInfoSupport instance. *

* The string representation consists of the name of this class (ie javax.management.openmbean.OpenMBeanInfoSupport), * the MBean class name, * and the string representation of infos on attributes, constructors, operations and notifications of the described MBean. *

* As OpenMBeanInfoSupport instances are immutable, * the string representation for this instance is calculated once, * on the first call to toString, and then the same value is returned for subsequent calls. * * @return a string representation of this OpenMBeanInfoSupport instance */ public String toString() { // Calculate the hash code value if it has not yet been done (ie 1st call to toString()) // if (myToString == null) { myToString = new StringBuffer() .append(this.getClass().getName()) .append("(mbean_class_name=") .append(this.getClassName()) .append(",attributes=") .append(Arrays.asList(this.getAttributes()).toString()) .append(",constructors=") .append(Arrays.asList(this.getConstructors()).toString()) .append(",operations=") .append(Arrays.asList(this.getOperations()).toString()) .append(",notifications=") .append(Arrays.asList(this.getNotifications()).toString()) .append(")") .toString(); } // return always the same string representation for this instance (immutable) // return myToString; } }