KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > openmbean > OpenMBeanInfoSupport


1 /*
2  * @(#)OpenMBeanInfoSupport.java 3.22 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8
9 package javax.management.openmbean;
10
11
12 // java import
13
//
14
import java.io.Serializable JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.HashSet JavaDoc;
17
18
19 // jmx import
20
//
21
import javax.management.MBeanInfo JavaDoc;
22 import javax.management.MBeanAttributeInfo JavaDoc;
23 import javax.management.MBeanOperationInfo JavaDoc;
24 import javax.management.MBeanConstructorInfo JavaDoc;
25 import javax.management.MBeanNotificationInfo JavaDoc;
26
27
28
29 /**
30  * The <tt>OpenMBeanInfoSupport</tt> class describes the management information of an <i>open MBean</i>:
31  * it is a subclass of {@link javax.management.MBeanInfo}, and it implements the {@link OpenMBeanInfo} interface.
32  * Note that an <i>open MBean</i> is recognized as such if its <tt>getMBeanInfo()</tt> method returns an instance of a class
33  * which implements the OpenMBeanInfo interface, typically <tt>OpenMBeanInfoSupport</tt>.
34  *
35  * @version 3.22 03/12/19
36  * @author Sun Microsystems, Inc.
37  *
38  * @since 1.5
39  * @since.unbundled JMX 1.1
40  */

41 public class OpenMBeanInfoSupport
42     extends MBeanInfo JavaDoc
43     implements OpenMBeanInfo JavaDoc, Serializable JavaDoc {
44     
45     /* Serial version */
46     static final long serialVersionUID = 4349395935420511492L;
47
48
49     private transient Integer JavaDoc myHashCode = null; // As this instance is immutable, these two values
50
private transient String JavaDoc myToString = null; // need only be calculated once.
51

52
53     /**
54      * Constructs an <tt>OpenMBeanInfoSupport</tt> instance,
55      * which describes a class of open MBeans with the specified
56      * <var>className</var>, <var>description</var>, <var>openAttributes</var>,
57      * <var>openConstructors</var> , <var>openOperations</var> and <var>notifications</var>.
58      * <p>
59      * The <var>openAttributes</var>, <var>openConstructors</var>,
60      * <var>openOperations</var> and <var>notifications</var>
61      * array parameters are internally copied, so that subsequent changes
62      * to the arrays referenced by these parameters have no effect on this instance.
63      * <p>
64      *
65      *
66      * @param className The fully qualified Java class name of
67      * the open MBean described by this <CODE>OpenMBeanInfoSupport</CODE> instance.
68      *
69      * @param description A human readable description of
70      * the open MBean described by this <CODE>OpenMBeanInfoSupport</CODE> instance.
71      *
72      * @param openAttributes The list of exposed attributes of the described open MBean;
73      * Must be an array of instances of a subclass of <tt>MBeanAttributeInfo</tt>,
74      * typically <tt>OpenMBeanAttributeInfoSupport</tt>.
75      *
76      * @param openConstructors The list of exposed public constructors of the described open MBean;
77      * Must be an array of instances of a subclass of <tt>MBeanConstructorInfo</tt>,
78      * typically <tt>OpenMBeanConstructorInfoSupport</tt>.
79      *
80      * @param openOperations The list of exposed operations of the described open MBean.
81      * Must be an array of instances of a subclass of <tt>MBeanOperationInfo</tt>,
82      * typically <tt>OpenMBeanOperationInfoSupport</tt>.
83      *
84      * @param notifications The list of notifications emitted by the described open MBean.
85      *
86      * @throws ArrayStoreException If <var>openAttributes</var>, <var>openConstructors</var> or <var>openOperations</var>
87      * is not an array of instances of a subclass of <tt>MBeanAttributeInfo</tt>,
88      * <tt>MBeanConstructorInfo</tt> or <tt>MBeanOperationInfo</tt> respectively.
89      */

90     public OpenMBeanInfoSupport(String JavaDoc className,
91                 String JavaDoc description,
92                 OpenMBeanAttributeInfo JavaDoc[] openAttributes,
93                 OpenMBeanConstructorInfo JavaDoc[] openConstructors,
94                 OpenMBeanOperationInfo JavaDoc[] openOperations,
95                 MBeanNotificationInfo JavaDoc[] notifications) {
96     
97     super(className,
98           description,
99           ( openAttributes == null ? null : attributesArrayCopyCast(openAttributes) ), // may throw an ArrayStoreException
100
( openConstructors == null ? null : constructorsArrayCopyCast(openConstructors) ), // may throw an ArrayStoreException
101
( openOperations == null ? null : operationsArrayCopyCast(openOperations) ), // may throw an ArrayStoreException
102
( notifications == null ? null : notificationsArrayCopy(notifications) ));
103  
104     }
105
106
107     private static MBeanAttributeInfo JavaDoc[] attributesArrayCopyCast(OpenMBeanAttributeInfo JavaDoc[] src) throws ArrayStoreException JavaDoc {
108
109     MBeanAttributeInfo JavaDoc[] dst = new MBeanAttributeInfo JavaDoc[src.length];
110     System.arraycopy(src, 0, dst, 0, src.length); // may throw an ArrayStoreException
111
return dst;
112     }
113
114     private static MBeanConstructorInfo JavaDoc[] constructorsArrayCopyCast(OpenMBeanConstructorInfo JavaDoc[] src) throws ArrayStoreException JavaDoc {
115
116     MBeanConstructorInfo JavaDoc[] dst = new MBeanConstructorInfo JavaDoc[src.length];
117     System.arraycopy(src, 0, dst, 0, src.length); // may throw an ArrayStoreException
118
return dst;
119     }
120
121     private static MBeanOperationInfo JavaDoc[] operationsArrayCopyCast(OpenMBeanOperationInfo JavaDoc[] src) throws ArrayStoreException JavaDoc {
122
123     MBeanOperationInfo JavaDoc[] dst = new MBeanOperationInfo JavaDoc[src.length];
124     System.arraycopy(src, 0, dst, 0, src.length); // may throw an ArrayStoreException
125
return dst;
126     }
127
128     private static MBeanNotificationInfo JavaDoc[] notificationsArrayCopy(MBeanNotificationInfo JavaDoc[] src) {
129
130     MBeanNotificationInfo JavaDoc[] dst = new MBeanNotificationInfo JavaDoc[src.length];
131     System.arraycopy(src, 0, dst, 0, src.length);
132     return dst;
133     }
134
135
136
137     /* *** Commodity methods from java.lang.Object *** */
138
139
140     /**
141      * Compares the specified <var>obj</var> parameter with this <code>OpenMBeanInfoSupport</code> instance for equality.
142      * <p>
143      * Returns <tt>true</tt> if and only if all of the following statements are true:
144      * <ul>
145      * <li><var>obj</var> is non null,</li>
146      * <li><var>obj</var> also implements the <code>OpenMBeanInfo</code> interface,</li>
147      * <li>their class names are equal</li>
148      * <li>their infos on attributes, constructors, operations and notifications are equal</li>
149      * </ul>
150      * This ensures that this <tt>equals</tt> method works properly for <var>obj</var> parameters which are
151      * different implementations of the <code>OpenMBeanInfo</code> interface.
152      * <br>&nbsp;
153      * @param obj the object to be compared for equality with this <code>OpenMBeanInfoSupport</code> instance;
154      *
155      * @return <code>true</code> if the specified object is equal to this <code>OpenMBeanInfoSupport</code> instance.
156      */

157     public boolean equals(Object JavaDoc obj) {
158
159     // if obj is null, return false
160
//
161
if (obj == null) {
162         return false;
163     }
164
165     // if obj is not a OpenMBeanInfo, return false
166
//
167
OpenMBeanInfo JavaDoc other;
168     try {
169         other = (OpenMBeanInfo JavaDoc) obj;
170     } catch (ClassCastException JavaDoc e) {
171         return false;
172     }
173
174     // Now, really test for equality between this OpenMBeanInfo implementation and the other:
175
//
176

177     // their MBean className should be equal
178
if ( ! this.getClassName().equals(other.getClassName()) ) {
179         return false;
180     }
181
182     // their infos on attributes should be equal (order not significant => equality between sets, not arrays or lists)
183
if ( ! new HashSet JavaDoc(Arrays.asList(this.getAttributes())).equals(new HashSet JavaDoc(Arrays.asList(other.getAttributes()))) ) {
184         return false;
185     }
186        
187     // their infos on constructors should be equal (order not significant => equality between sets, not arrays or lists)
188
if ( ! new HashSet JavaDoc(Arrays.asList(this.getConstructors())).equals(new HashSet JavaDoc(Arrays.asList(other.getConstructors()))) ) {
189         return false;
190     }
191        
192     // their infos on operations should be equal (order not significant => equality between sets, not arrays or lists)
193
if ( ! new HashSet JavaDoc(Arrays.asList(this.getOperations())).equals(new HashSet JavaDoc(Arrays.asList(other.getOperations()))) ) {
194         return false;
195     }
196        
197     // their infos on notifications should be equal (order not significant => equality between sets, not arrays or lists)
198
if ( ! new HashSet JavaDoc(Arrays.asList(this.getNotifications())).equals(new HashSet JavaDoc(Arrays.asList(other.getNotifications()))) ) {
199         return false;
200     }
201        
202     // All tests for equality were successfull
203
//
204
return true;
205     }
206
207     /**
208      * Returns the hash code value for this <code>OpenMBeanInfoSupport</code> instance.
209      * <p>
210      * The hash code of an <code>OpenMBeanInfoSupport</code> instance is the sum of the hash codes
211      * of all elements of information used in <code>equals</code> comparisons
212      * (ie: its class name, and its infos on attributes, constructors, operations and notifications,
213      * where the hashCode of each of these arrays is calculated by a call to
214      * <tt>new java.util.HashSet(java.util.Arrays.asList(this.getSignature)).hashCode()</tt>).
215      * <p>
216      * This ensures that <code> t1.equals(t2) </code> implies that <code> t1.hashCode()==t2.hashCode() </code>
217      * for any two <code>OpenMBeanInfoSupport</code> instances <code>t1</code> and <code>t2</code>,
218      * as required by the general contract of the method
219      * {@link Object#hashCode() Object.hashCode()}.
220      * <p>
221      * However, note that another instance of a class implementing the <code>OpenMBeanInfo</code> interface
222      * may be equal to this <code>OpenMBeanInfoSupport</code> instance as defined by {@link #equals(java.lang.Object)},
223      * but may have a different hash code if it is calculated differently.
224      * <p>
225      * As <code>OpenMBeanInfoSupport</code> instances are immutable, the hash code for this instance is calculated once,
226      * on the first call to <code>hashCode</code>, and then the same value is returned for subsequent calls.
227      *
228      * @return the hash code value for this <code>OpenMBeanInfoSupport</code> instance
229      */

230     public int hashCode() {
231
232     // Calculate the hash code value if it has not yet been done (ie 1st call to hashCode())
233
//
234
if (myHashCode == null) {
235         int value = 0;
236         value += this.getClassName().hashCode();
237         value += new HashSet JavaDoc(Arrays.asList(this.getAttributes())).hashCode();
238         value += new HashSet JavaDoc(Arrays.asList(this.getConstructors())).hashCode();
239         value += new HashSet JavaDoc(Arrays.asList(this.getOperations())).hashCode();
240         value += new HashSet JavaDoc(Arrays.asList(this.getNotifications())).hashCode();
241         myHashCode = new Integer JavaDoc(value);
242     }
243     
244     // return always the same hash code for this instance (immutable)
245
//
246
return myHashCode.intValue();
247     }
248
249     /**
250      * Returns a string representation of this <code>OpenMBeanInfoSupport</code> instance.
251      * <p>
252      * The string representation consists of the name of this class (ie <code>javax.management.openmbean.OpenMBeanInfoSupport</code>),
253      * the MBean class name,
254      * and the string representation of infos on attributes, constructors, operations and notifications of the described MBean.
255      * <p>
256      * As <code>OpenMBeanInfoSupport</code> instances are immutable,
257      * the string representation for this instance is calculated once,
258      * on the first call to <code>toString</code>, and then the same value is returned for subsequent calls.
259      *
260      * @return a string representation of this <code>OpenMBeanInfoSupport</code> instance
261      */

262     public String JavaDoc toString() {
263
264     // Calculate the hash code value if it has not yet been done (ie 1st call to toString())
265
//
266
if (myToString == null) {
267         myToString = new StringBuffer JavaDoc()
268         .append(this.getClass().getName())
269         .append("(mbean_class_name=")
270         .append(this.getClassName())
271         .append(",attributes=")
272         .append(Arrays.asList(this.getAttributes()).toString())
273         .append(",constructors=")
274         .append(Arrays.asList(this.getConstructors()).toString())
275         .append(",operations=")
276         .append(Arrays.asList(this.getOperations()).toString())
277         .append(",notifications=")
278         .append(Arrays.asList(this.getNotifications()).toString())
279         .append(")")
280         .toString();
281     }
282
283     // return always the same string representation for this instance (immutable)
284
//
285
return myToString;
286     }
287
288 }
289
Popular Tags