001: /**
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */package javax.management.openmbean;
008:
009: import java.io.Serializable;
010: import java.util.Arrays;
011: import java.util.HashSet;
012: import javax.management.MBeanAttributeInfo;
013: import javax.management.MBeanConstructorInfo;
014: import javax.management.MBeanInfo;
015: import javax.management.MBeanNotificationInfo;
016: import javax.management.MBeanOperationInfo;
017:
018: /**
019: * @version $Revision: 1.11 $
020: */
021: public class OpenMBeanInfoSupport extends MBeanInfo implements
022: OpenMBeanInfo, Serializable {
023: private static final long serialVersionUID = 4349395935420511492L;
024:
025: // No non-transient data members allowed
026: private transient int hashCode = 0;
027:
028: public OpenMBeanInfoSupport(String className, String description,
029: OpenMBeanAttributeInfo[] openAttributes,
030: OpenMBeanConstructorInfo[] openConstructors,
031: OpenMBeanOperationInfo[] openOperations,
032: MBeanNotificationInfo[] notifications) {
033: // We cant pass this directly because OpenMBean*Info
034: // and friends isn't a direct subclass of their MBean*Info
035: // counterpart but the *Support. We need to do an arraycopy
036: // for this to work and the implementation should be a
037: // subclass of thir MBean*Support counterpart
038: super (className, description,
039: createMBeanAttributes(openAttributes),
040: createMBeanConstructors(openConstructors),
041: createMBeanOperations(openOperations), notifications);
042: }
043:
044: public boolean equals(Object obj) {
045: if (obj == null)
046: return false;
047: if (obj == this )
048: return true;
049: if (!(obj instanceof OpenMBeanInfo))
050: return false;
051:
052: OpenMBeanInfo other = (OpenMBeanInfo) obj;
053: String this ClassName = getClassName();
054: String otherClassName = other.getClassName();
055: if (this ClassName != null ? !this ClassName
056: .equals(otherClassName) : otherClassName != null)
057: return false;
058:
059: if (!compare(getConstructors(), other.getConstructors()))
060: return false;
061: if (!compare(getAttributes(), other.getAttributes()))
062: return false;
063: if (!compare(getOperations(), other.getOperations()))
064: return false;
065: if (!compare(getNotifications(), other.getNotifications()))
066: return false;
067:
068: return true;
069: }
070:
071: private boolean compare(Object[] o1, Object[] o2) {
072: return new HashSet(Arrays.asList(o1)).equals(new HashSet(Arrays
073: .asList(o2)));
074: }
075:
076: public int hashCode() {
077: if (hashCode == 0) {
078: int hash = getClassName() == null ? 0 : getClassName()
079: .hashCode();
080: if (getConstructors() != null)
081: hash += new HashSet(Arrays.asList(getConstructors()))
082: .hashCode();
083: if (getAttributes() != null)
084: hash += new HashSet(Arrays.asList(getAttributes()))
085: .hashCode();
086: if (getOperations() != null)
087: hash += new HashSet(Arrays.asList(getOperations()))
088: .hashCode();
089: if (getNotifications() != null)
090: hash += new HashSet(Arrays.asList(getNotifications()))
091: .hashCode();
092: hashCode = hash;
093: }
094: return hashCode;
095: }
096:
097: /**
098: * Helper Method for OpenMBeanAttributeInfo[] to MBeanAttributeInfo[]
099: */
100: private static MBeanAttributeInfo[] createMBeanAttributes(
101: OpenMBeanAttributeInfo[] attributes)
102: throws ArrayStoreException {
103: if (attributes == null)
104: return null;
105: MBeanAttributeInfo[] attrInfo = new MBeanAttributeInfo[attributes.length];
106: System.arraycopy(attributes, 0, attrInfo, 0, attrInfo.length);
107: return attrInfo;
108: }
109:
110: /**
111: * Helper Method for OpenMBeanConstructorInfo[] to MBeanConstructorInfo[]
112: */
113: private static MBeanConstructorInfo[] createMBeanConstructors(
114: OpenMBeanConstructorInfo[] constructors)
115: throws ArrayStoreException {
116: if (constructors == null)
117: return null;
118: MBeanConstructorInfo[] constInfo = new MBeanConstructorInfo[constructors.length];
119: System.arraycopy(constructors, 0, constInfo, 0,
120: constInfo.length);
121: return constInfo;
122: }
123:
124: /**
125: * Helper Method for OpenMBeanOperationsInfo[] to MBeanOperationsInfo[]
126: */
127: private static MBeanOperationInfo[] createMBeanOperations(
128: OpenMBeanOperationInfo[] operations)
129: throws ArrayStoreException {
130: if (operations == null)
131: return null;
132: MBeanOperationInfo[] operInfo = new MBeanOperationInfo[operations.length];
133: System.arraycopy(operations, 0, operInfo, 0, operInfo.length);
134: return operInfo;
135: }
136: }
|