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;
023:
024: import java.util.Arrays;
025:
026: /**
027: * Describes an MBeans' management interface.
028: *
029: * This implementation protects its immutability by taking shallow clones of all arrays
030: * supplied in constructors and by returning shallow array clones in getXXX() methods.
031: *
032: * @see javax.management.MBeanServer
033: *
034: * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
035: * @author <a href="mailto:trevor@protocool.com">Trevor Squires</a>.
036: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>
037: *
038: * @version $Revision: 57200 $
039: *
040: * <p><b>Revisions:</b>
041: * <p><b>20020711 Adrian Brock:</b>
042: * <ul>
043: * <li> Serialization </li>
044: * </ul>
045: *
046: */
047: public class MBeanInfo implements Cloneable, java.io.Serializable {
048: // Constants -----------------------------------------------------
049: private static final long serialVersionUID = -6451021435135161911L;
050:
051: // Attributes ----------------------------------------------------
052: private String className = null;
053: private String description = null;
054: private MBeanAttributeInfo[] attributes = null;
055: private MBeanConstructorInfo[] constructors = null;
056: private MBeanOperationInfo[] operations = null;
057: private MBeanNotificationInfo[] notifications = null;
058:
059: /**
060: * The cached string
061: */
062: private transient String cacheString;
063:
064: /**
065: * The cached hashCode
066: */
067: private transient int cacheHashCode;
068:
069: // Constructors --------------------------------------------------
070: public MBeanInfo(String className, String description,
071: MBeanAttributeInfo[] attributes,
072: MBeanConstructorInfo[] constructors,
073: MBeanOperationInfo[] operations,
074: MBeanNotificationInfo[] notifications)
075: throws IllegalArgumentException {
076: this .className = className;
077: this .description = description;
078: this .attributes = (null == attributes) ? new MBeanAttributeInfo[0]
079: : (MBeanAttributeInfo[]) attributes.clone();
080: this .constructors = (null == constructors) ? new MBeanConstructorInfo[0]
081: : (MBeanConstructorInfo[]) constructors.clone();
082: this .operations = (null == operations) ? new MBeanOperationInfo[0]
083: : (MBeanOperationInfo[]) operations.clone();
084: this .notifications = (null == notifications) ? new MBeanNotificationInfo[0]
085: : (MBeanNotificationInfo[]) notifications.clone();
086: }
087:
088: // Public --------------------------------------------------------
089: public String getClassName() {
090: return className;
091: }
092:
093: public String getDescription() {
094: return description;
095: }
096:
097: public MBeanAttributeInfo[] getAttributes() {
098: return (MBeanAttributeInfo[]) attributes.clone();
099: }
100:
101: public MBeanOperationInfo[] getOperations() {
102: return (MBeanOperationInfo[]) operations.clone();
103: }
104:
105: public MBeanConstructorInfo[] getConstructors() {
106: return (MBeanConstructorInfo[]) constructors.clone();
107: }
108:
109: public MBeanNotificationInfo[] getNotifications() {
110: return (MBeanNotificationInfo[]) notifications.clone();
111: }
112:
113: public boolean equals(Object object) {
114: if (this == object)
115: return true;
116: if (object == null || (object instanceof MBeanInfo) == false)
117: return false;
118:
119: MBeanInfo other = (MBeanInfo) object;
120:
121: if (this .getClassName().equals(other.getClassName()) == false)
122: return false;
123: if (this .getDescription() != null
124: && this .getDescription().equals(other.getDescription()) == false)
125: return false;
126: if (this .getDescription() == null
127: && other.getDescription() != null)
128: return false;
129:
130: MBeanAttributeInfo[] this Attrs = this .getAttributes();
131: MBeanAttributeInfo[] otherAttrs = other.getAttributes();
132: if (this Attrs.length != otherAttrs.length)
133: return false;
134: for (int i = 0; i < this Attrs.length; ++i)
135: if (this Attrs[i].equals(otherAttrs[i]) == false)
136: return false;
137:
138: MBeanConstructorInfo[] this Cons = this .getConstructors();
139: MBeanConstructorInfo[] otherCons = other.getConstructors();
140: if (this Cons.length != otherCons.length)
141: return false;
142: for (int i = 0; i < this Cons.length; ++i)
143: if (this Cons[i].equals(otherCons[i]) == false)
144: return false;
145:
146: MBeanNotificationInfo[] this Notfs = this .getNotifications();
147: MBeanNotificationInfo[] otherNotfs = other.getNotifications();
148: if (this Notfs.length != otherNotfs.length)
149: return false;
150: for (int i = 0; i < this Notfs.length; ++i)
151: if (this Notfs[i].equals(otherNotfs[i]) == false)
152: return false;
153:
154: MBeanOperationInfo[] this Opers = this .getOperations();
155: MBeanOperationInfo[] otherOpers = other.getOperations();
156: if (this Opers.length != otherOpers.length)
157: return false;
158: for (int i = 0; i < this Opers.length; ++i)
159: if (this Opers[i].equals(otherOpers[i]) == false)
160: return false;
161:
162: return true;
163: }
164:
165: public int hashCode() {
166: if (cacheHashCode == 0) {
167: cacheHashCode = getClassName().hashCode();
168: cacheHashCode += (getDescription() != null) ? getDescription()
169: .hashCode()
170: : 0;
171: }
172: return cacheHashCode;
173: }
174:
175: /**
176: * @return a human readable string
177: */
178: public String toString() {
179: if (cacheString == null) {
180: StringBuffer buffer = new StringBuffer(100);
181: buffer.append(getClass().getName()).append(":");
182: buffer.append(" className=").append(getClassName());
183: buffer.append(" description=").append(getDescription());
184: buffer.append(" attributes=").append(
185: Arrays.asList(attributes));
186: buffer.append(" constructors=").append(
187: Arrays.asList(constructors));
188: buffer.append(" notifications=").append(
189: Arrays.asList(notifications));
190: buffer.append(" operations=").append(
191: Arrays.asList(operations));
192: cacheString = buffer.toString();
193: }
194: return cacheString;
195: }
196:
197: // Cloneable implementation --------------------------------------
198: public Object clone() {
199: MBeanInfo clone = null;
200: try {
201: clone = (MBeanInfo) super .clone();
202: clone.className = getClassName();
203: clone.description = getDescription();
204:
205: clone.attributes = getAttributes();
206: clone.constructors = getConstructors();
207: clone.operations = getOperations();
208: clone.notifications = getNotifications();
209: } catch (CloneNotSupportedException e) {
210: }
211:
212: return clone;
213: }
214:
215: }
|