OSDN Git Service

Merged gcj-eclipse branch to trunk.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / javax / management / MBeanInfo.java
1 /* MBeanInfo.java -- Information about a management bean.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38 package javax.management;
39
40 import java.io.Serializable;
41
42 import java.util.Arrays;
43
44 /**
45  * <p>
46  * Describes the interface of a management bean.  This allows
47  * the user to access the bean dynamically, without knowing
48  * the details of any of its attributes, operations,
49  * constructors or notifications beforehand.  The information
50  * is immutable as standard.  Of course, subclasses may change
51  * this, but this behaviour is not recommended.
52  * </p>
53  * <p>
54  * The contents of this class, for standard management beans,
55  * are dynamically compiled using reflection.
56  * {@link #getClassName()} and {@link #getConstructors()}
57  * return the name of the class and its constructors, respectively.
58  * This is much the same as could be obtained by reflection on the
59  * bean.  {@link #getAttributes()} and {@link #getOperations()},
60  * however, do something more in splitting the methods of the
61  * class into two sets.  Those of the form, <code>getXXX</code>,
62  * <code>setXXX</code> and <code>isXXX</code> are taken to be
63  * the accessors and mutators of a series of attributes, with
64  * <code>XXX</code> being the attribute name.  These are returned
65  * by {@link getAttributes()} and the {@link Attribute} class can
66  * be used to manipulate them.  The remaining methods are classified
67  * as operations and returned by {@link getOperations()}.
68  * </p>
69  * <p>
70  * Beans can also broadcast notifications.  If the bean provides this
71  * facility, by implementing the {@link NotificationBroadcaster}
72  * interface, then an array of {@link MBeanNotificationInfo} objects
73  * may be obtained from {@link #getNotifications()}, which describe
74  * the notifications emitted.
75  * </p>
76  * <p>
77  * Model management beans and open management beans also supply an
78  * instance of this class, as part of implementing the
79  * {@link DynamicMBean#getMBeanInfo()} method of {@link DynamicMBean}.
80  * </p>
81  * 
82  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
83  * @since 1.5
84  */
85 public class MBeanInfo
86   implements Cloneable, Serializable
87 {
88
89   /**
90    * Compatible with JDK 1.5
91    */
92   private static final long serialVersionUID = -6451021435135161911L;
93
94   /**
95    * A description of the bean.
96    * 
97    * @serial The bean's description.
98    */
99   private String description;
100
101   /**
102    * The class name of the management bean.
103    *
104    * @serial The bean's class name.
105    */
106   private String className;
107
108   /**
109    * Descriptions of the attributes provided by the bean.
110    */
111   private MBeanAttributeInfo[] attributes;
112
113   /**
114    * Descriptions of the operations provided by the bean.
115    */
116   private MBeanOperationInfo[] operations;
117
118   /**
119    * Descriptions of the bean's constructors.
120    */
121   private MBeanConstructorInfo[] constructors;
122
123   /**
124    * Descriptions of the notifications emitted by the bean.
125    *
126    * @serial The bean's notifications.
127    */
128   private MBeanNotificationInfo[] notifications;
129
130   /**
131    * The <code>toString()</code> result of this instance.
132    */
133   private transient String string;
134
135   /**
136    * Constructs a new {@link MBeanInfo} using the supplied
137    * class name and description with the given attributes,
138    * operations, constructors and notifications.  The class
139    * name does not have to actually specify a valid class that
140    * can be loaded by the MBean server or class loader; it merely
141    * has to be a syntactically correct class name.  Any of the
142    * arrays may be <code>null</code>; this will be treated as if
143    * an empty array was supplied.  A copy of the arrays is
144    * taken, so later changes have no effect.
145    *
146    * @param name the name of the class this instance describes.
147    * @param desc a description of the bean.
148    * @param attribs the attribute descriptions for the bean,
149    *                or <code>null</code>.
150    * @param cons the constructor descriptions for the bean,
151    *             or <code>null</code>.
152    * @param ops the operation descriptions for the bean,
153    *            or <code>null</code>.
154    * @param notifs the notification descriptions for the bean,
155    *               or <code>null</code>.
156    */
157   public MBeanInfo(String name, String desc, MBeanAttributeInfo[] attribs,
158                    MBeanConstructorInfo[] cons, MBeanOperationInfo[] ops,
159                    MBeanNotificationInfo[] notifs)
160   {
161     className = name;
162     description = desc;
163     if (attribs == null)
164       attributes = new MBeanAttributeInfo[0];
165     else
166       {
167         attributes = new MBeanAttributeInfo[attribs.length];
168         System.arraycopy(attribs, 0, attributes, 0, attribs.length);
169       }
170     if (cons == null)
171       constructors = new MBeanConstructorInfo[0];
172     else
173       {
174         constructors = new MBeanConstructorInfo[cons.length];
175         System.arraycopy(cons, 0, constructors, 0, cons.length);
176       }
177     if (ops == null)
178       operations = new MBeanOperationInfo[0];
179     else
180       {
181         operations = new MBeanOperationInfo[ops.length];
182         System.arraycopy(ops, 0, operations, 0, ops.length);
183       }
184     if (notifs == null)
185       notifications = new MBeanNotificationInfo[0];
186     else
187       {
188         notifications = new MBeanNotificationInfo[notifs.length];
189         System.arraycopy(notifs, 0, notifications, 0, notifs.length);
190       }
191   }
192
193   /**
194    * Returns a shallow clone of the information.  This is
195    * simply a new copy of each string and a clone
196    * of each array, which still references the same objects,
197    * as obtained by the {@link Object} implementation of
198    * {@link Object#clone()}.  As the fields can not be
199    * changed, this method is only really of interest to
200    * subclasses which may add new mutable fields or make
201    * the existing ones mutable.
202    *
203    * @return a shallow clone of this {@link MBeanInfo}.
204    */
205   public Object clone()
206   {
207     MBeanInfo clone = null;
208     try
209       {
210         clone = (MBeanInfo) super.clone();
211       }
212     catch (CloneNotSupportedException e)
213       {
214         /* This won't happen as we implement Cloneable */
215       }
216     return clone;
217   }
218
219   /**
220    * Compares this feature with the supplied object.  This returns
221    * true iff the object is an instance of {@link MBeanInfo} and
222    * {@link Object#equals()} returns true for a comparison of the
223    * class name and description, and the arrays each contain the same
224    * elements in the same order (but one may be longer than the
225    * other).
226    *
227    * @param obj the object to compare.
228    * @return true if the object is a {@link MBeanInfo}
229    *         instance, 
230    *         <code>className.equals(object.getClassName())</code>,
231    *         <code>description.equals(object.getDescription())</code>
232    *         and the corresponding elements of the arrays are
233    *         equal.
234    */
235   public boolean equals(Object obj)
236   {
237     if (!(obj instanceof MBeanInfo))
238       return false;
239     if (!(super.equals(obj)))
240       return false;
241     MBeanInfo o = (MBeanInfo) obj;
242     MBeanAttributeInfo[] attr = o.getAttributes();
243     for (int a = 0; a < attributes.length; ++a)
244       {
245         if (a == attr.length)
246           return true;
247         if (!(attributes[a].equals(attr[a])))
248           return false;
249       }
250     MBeanConstructorInfo[] cons = o.getConstructors();
251     for (int a = 0; a < constructors.length; ++a)
252       {
253         if (a == cons.length)
254           return true;
255         if (!(constructors[a].equals(cons[a])))
256           return false;
257       }
258     MBeanOperationInfo[] ops = o.getOperations();
259     for (int a = 0; a < operations.length; ++a)
260       {
261         if (a == ops.length)
262           return true;
263         if (!(operations[a].equals(ops[a])))
264           return false;
265       }
266     MBeanNotificationInfo[] notifs = o.getNotifications();
267     for (int a = 0; a < notifications.length; ++a)
268       {
269         if (a == notifs.length)
270           return true;
271         if (!(notifications[a].equals(notifs[a])))
272           return false;
273       }
274     return (className.equals(o.getClassName()) &&
275             description.equals(o.getDescription()));
276   }
277
278   /**
279    * Returns descriptions of each of the attributes provided
280    * by this management bean.  The returned value is a shallow
281    * copy of the attribute array maintained by this instance.
282    * Hence, changing the elements of the returned array will not
283    * affect the attribute array, and the elements (instances
284    * of the {@link MBeanAttributeInfo} class) are immutable.
285    *
286    * @return an array of {@link MBeanAttributeInfo} objects,
287    *         representing the attributes emitted by this
288    *         management bean.
289    */
290   public MBeanAttributeInfo[] getAttributes()
291   {
292     return (MBeanAttributeInfo[]) attributes.clone();
293   }
294
295   /**
296    * Returns the class name of the management bean.
297    *
298    * @return the bean's class name.
299    */
300   public String getClassName()
301   {
302     return className;
303   }
304
305   /**
306    * Returns descriptions of each of the constructors provided
307    * by this management bean.  The returned value is a shallow
308    * copy of the constructor array maintained by this instance.
309    * Hence, changing the elements of the returned array will not
310    * affect the constructor array, and the elements (instances
311    * of the {@link MBeanConstructorInfo} class) are immutable.
312    *
313    * @return an array of {@link MBeanConstructorInfo} objects,
314    *         representing the constructors emitted by this
315    *         management bean.
316    */
317   public MBeanConstructorInfo[] getConstructors()
318   {
319     return (MBeanConstructorInfo[]) constructors.clone();
320   }
321
322   /**
323    * Returns a description of the management bean.
324    *
325    * @return the bean's description.
326    */
327   public String getDescription()
328   {
329     return description;
330   }
331
332   /**
333    * Returns descriptions of each of the notifications emitted
334    * by this management bean.  The returned value is a shallow
335    * copy of the notification array maintained by this instance.
336    * Hence, changing the elements of the returned array will not
337    * affect the notification array, and the elements (instances
338    * of the {@link MBeanNotificationInfo} class) are immutable.
339    *
340    * @return an array of {@link MBeanNotificationInfo} objects,
341    *         representing the notifications emitted by this
342    *         management bean.
343    */
344   public MBeanNotificationInfo[] getNotifications()
345   {
346     return (MBeanNotificationInfo[]) notifications.clone();
347   }
348
349   /**
350    * Returns descriptions of each of the operations provided
351    * by this management bean.  The returned value is a shallow
352    * copy of the operation array maintained by this instance.
353    * Hence, changing the elements of the returned array will not
354    * affect the operation array, and the elements (instances
355    * of the {@link MBeanOperationInfo} class) are immutable.
356    *
357    * @return an array of {@link MBeanOperationInfo} objects,
358    *         representing the operations emitted by this
359    *         management bean.
360    */
361   public MBeanOperationInfo[] getOperations()
362   {
363     return (MBeanOperationInfo[]) operations.clone();
364   }
365
366   /**
367    * Returns the hashcode of the information as the sum of the
368    * hashcode of the classname, description and each array.
369    *
370    * @return the hashcode of the information.
371    */
372   public int hashCode()
373   {
374     return className.hashCode() + description.hashCode()
375       + Arrays.hashCode(attributes) + Arrays.hashCode(constructors)
376       + Arrays.hashCode(operations) + Arrays.hashCode(notifications);
377   }
378
379   /**
380    * <p>
381    * Returns a textual representation of this instance.  This
382    * is constructed using the class name
383    * (<code>javax.management.MBeanInfo</code>),
384    * the name and description of the bean and the contents
385    * of the four arrays.
386    * </p>
387    * <p>
388    * As instances of this class are immutable, the return value
389    * is computed just once for each instance and reused
390    * throughout its life.
391    * </p>
392    *
393    * @return a @link{java.lang.String} instance representing
394    *         the instance in textual form.
395    */
396   public String toString()
397   {
398     if (string == null)
399       string = getClass().getName()
400         + "[name=" + className 
401         + ",desc=" + description 
402         + ",attributes=" + Arrays.toString(attributes)
403         + ",constructors=" + Arrays.toString(constructors)
404         + ",operations=" + Arrays.toString(operations)
405         + ",notifications=" + Arrays.toString(notifications)
406         + "]";
407     return string;
408   }
409
410 }