MBeanInfo.java 13 KB

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