001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package javax.management.openmbean;
023:
024: import java.io.Serializable;
025:
026: import java.util.Arrays;
027:
028: import javax.management.MBeanAttributeInfo;
029: import javax.management.MBeanConstructorInfo;
030: import javax.management.MBeanInfo;
031: import javax.management.MBeanNotificationInfo;
032: import javax.management.MBeanOperationInfo;
033:
034: /**
035: * OpenMBeanInfo implementation
036: *
037: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
038: *
039: * @version $Revision: 57200 $
040: *
041: */
042: public class OpenMBeanInfoSupport extends MBeanInfo implements
043: OpenMBeanInfo, Serializable {
044: // Constants -----------------------------------------------------
045:
046: private static final long serialVersionUID = 4349395935420511492L;
047:
048: // Attributes ----------------------------------------------------
049:
050: private transient int cachedHashCode;
051:
052: private transient String cachedToString;
053:
054: // Static --------------------------------------------------------
055:
056: private static MBeanAttributeInfo[] convertArray(
057: OpenMBeanAttributeInfo[] array) {
058: if (array == null)
059: return null;
060: MBeanAttributeInfo[] result = new MBeanAttributeInfo[array.length];
061: System.arraycopy(array, 0, result, 0, array.length);
062: return result;
063: }
064:
065: private static MBeanConstructorInfo[] convertArray(
066: OpenMBeanConstructorInfo[] array) {
067: if (array == null)
068: return null;
069: MBeanConstructorInfo[] result = new MBeanConstructorInfo[array.length];
070: System.arraycopy(array, 0, result, 0, array.length);
071: return result;
072: }
073:
074: private static MBeanOperationInfo[] convertArray(
075: OpenMBeanOperationInfo[] array) {
076: if (array == null)
077: return null;
078: MBeanOperationInfo[] result = new MBeanOperationInfo[array.length];
079: System.arraycopy(array, 0, result, 0, array.length);
080: return result;
081: }
082:
083: // Constructors --------------------------------------------------
084:
085: /**
086: * Contruct an OpenMBeanInfoSupport<p>
087: *
088: * @param className cannot be null or empty
089: * @param description cannot be null or empty
090: * @param attributes the open mbean's attributes
091: * @param constructors the open mbean's constructors
092: * @param operations the open mbean's operations
093: * @param notifications the open mbean's notifications
094: * @exception IllegalArgumentException when one of the above
095: * constraints is not satisfied
096: */
097: public OpenMBeanInfoSupport(String className, String description,
098: OpenMBeanAttributeInfo[] attributes,
099: OpenMBeanConstructorInfo[] constructors,
100: OpenMBeanOperationInfo[] operations,
101: MBeanNotificationInfo[] notifications) {
102: super (className, description, convertArray(attributes),
103: convertArray(constructors), convertArray(operations),
104: notifications);
105: }
106:
107: // Public --------------------------------------------------------
108:
109: // OpenMBeanInfo Implementation ----------------------------------
110:
111: // Object Overrides ----------------------------------------------
112:
113: public boolean equals(Object obj) {
114: if (this == obj)
115: return true;
116: if (obj == null || !(obj instanceof OpenMBeanInfoSupport))
117: return false;
118: OpenMBeanInfo other = (OpenMBeanInfo) obj;
119:
120: if (getClassName().equals(other.getClassName()) == false)
121: return false;
122:
123: if (compareArray(getAttributes(), other.getAttributes()) == false)
124: return false;
125:
126: if (compareArray(getConstructors(), other.getConstructors()) == false)
127: return false;
128:
129: if (compareArray(getNotifications(), other.getNotifications()) == false)
130: return false;
131:
132: if (compareArray(getOperations(), other.getOperations()) == false)
133: return false;
134:
135: return true;
136: }
137:
138: public int hashCode() {
139: if (cachedHashCode != 0)
140: return cachedHashCode;
141: cachedHashCode = getClassName().hashCode();
142: MBeanAttributeInfo[] attrs = getAttributes();
143: for (int i = 0; i < attrs.length; i++)
144: cachedHashCode += attrs[i].hashCode();
145: MBeanConstructorInfo[] ctors = getConstructors();
146: for (int i = 0; i < ctors.length; i++)
147: cachedHashCode += ctors[i].hashCode();
148: MBeanNotificationInfo[] notify = getNotifications();
149: for (int i = 0; i < notify.length; i++)
150: cachedHashCode += notify[i].hashCode();
151: MBeanOperationInfo[] ops = getOperations();
152: for (int i = 0; i < ops.length; i++)
153: cachedHashCode += ops[i].hashCode();
154: return cachedHashCode;
155: }
156:
157: public String toString() {
158: if (cachedToString != null)
159: return cachedToString;
160: StringBuffer buffer = new StringBuffer(getClass().getName());
161: buffer.append(": className=");
162: buffer.append(getClassName());
163: buffer.append(", attributes=");
164: buffer.append(Arrays.asList(getAttributes()));
165: buffer.append(", constructors=");
166: buffer.append(Arrays.asList(getConstructors()));
167: buffer.append(", notifications=");
168: buffer.append(Arrays.asList(getNotifications()));
169: buffer.append(", operations=");
170: buffer.append(Arrays.asList(getOperations()));
171: cachedToString = buffer.toString();
172: return cachedToString;
173: }
174:
175: // Protected -----------------------------------------------------
176:
177: // Private -------------------------------------------------------
178:
179: private boolean compareArray(Object[] one, Object[] two) {
180: if (one.length != two.length)
181: return false;
182: if (Arrays.asList(one).containsAll(Arrays.asList(two)) == false)
183: return false;
184: return true;
185: }
186:
187: // Inner Classes -------------------------------------------------
188: }
|