1. /*
  2. * @(#)MBeanInfo.java 1.44 04/06/03
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.management;
  8. import java.lang.reflect.Method;
  9. import java.util.Arrays;
  10. import java.util.Map;
  11. import java.util.WeakHashMap;
  12. import java.security.AccessController;
  13. import java.security.PrivilegedAction;
  14. /**
  15. * <p>Describes the management interface exposed by an MBean; that is,
  16. * the set of attributes and operations which are available for
  17. * management operations. Instances of this class are immutable.
  18. * Subclasses may be mutable but this is not recommended.</p>
  19. *
  20. * <p>The contents of the <code>MBeanInfo</code> for a Dynamic MBean
  21. * are determined by its {@link DynamicMBean#getMBeanInfo
  22. * getMBeanInfo()} method. This includes Open MBeans and Model
  23. * MBeans, which are kinds of Dynamic MBeans.</p>
  24. *
  25. * <p>The contents of the <code>MBeanInfo</code> for a Standard MBean
  26. * are determined by the MBean server as follows:</p>
  27. *
  28. * <ul>
  29. *
  30. * <li>{@link #getClassName()} returns the Java class name of the MBean
  31. * object;
  32. *
  33. * <li>{@link #getConstructors()} returns the list of all public
  34. * constructors in that object;
  35. *
  36. * <li>{@link #getAttributes()} returns the list of all attributes
  37. * whose existence is deduced from the presence in the MBean interface
  38. * of a <code>get<i>Name</i></code>, <code>is<i>Name</i></code>, or
  39. * <code>set<i>Name</i></code> method that conforms to the conventions
  40. * for Standard MBeans;
  41. *
  42. * <li>{@link #getOperations()} returns the list of all methods in
  43. * the MBean interface that do not represent attributes;
  44. *
  45. * <li>{@link #getNotifications()} returns an empty array if the MBean
  46. * does not implement the {@link NotificationBroadcaster} interface,
  47. * otherwise the result of calling {@link
  48. * NotificationBroadcaster#getNotificationInfo()} on it.
  49. *
  50. * </ul>
  51. *
  52. * <p>The remaining details of the <code>MBeanInfo</code> for a
  53. * Standard MBean are not specified. This includes the description of
  54. * the <code>MBeanInfo</code> and of any contained constructors,
  55. * attributes, operations, and notifications; and the names and
  56. * descriptions of parameters to constructors and operations.
  57. *
  58. * @since 1.5
  59. */
  60. public class MBeanInfo implements Cloneable, java.io.Serializable {
  61. /* Serial version */
  62. static final long serialVersionUID = -6451021435135161911L;
  63. /**
  64. * @serial The human readable description of the class.
  65. */
  66. private final String description;
  67. /**
  68. * @serial The MBean qualified name.
  69. */
  70. private final String className;
  71. /**
  72. * @serial The MBean attribute descriptors.
  73. */
  74. private final MBeanAttributeInfo[] attributes;
  75. /**
  76. * @serial The MBean operation descriptors.
  77. */
  78. private final MBeanOperationInfo[] operations;
  79. /**
  80. * @serial The MBean constructor descriptors.
  81. */
  82. private final MBeanConstructorInfo[] constructors;
  83. /**
  84. * @serial The MBean notification descriptors.
  85. */
  86. private final MBeanNotificationInfo[] notifications;
  87. private transient int hashCode;
  88. /**
  89. * <p>True if this class is known not to override the getters of
  90. * MBeanInfo. Obviously true for MBeanInfo itself, and true
  91. * for a subclass where we succeed in reflecting on the methods
  92. * and discover they are not overridden.</p>
  93. *
  94. * <p>The purpose of this variable is to avoid cloning the arrays
  95. * when doing operations like {@link #equals} where we know they
  96. * will not be changed. If a subclass overrides a getter, we
  97. * cannot access the corresponding array directly.</p>
  98. */
  99. private final transient boolean immutable;
  100. /**
  101. * Constructs an <CODE>MBeanInfo</CODE>.
  102. *
  103. * @param className The name of the Java class of the MBean described
  104. * by this <CODE>MBeanInfo</CODE>. This value may be any
  105. * syntactically legal Java class name. It does not have to be a
  106. * Java class known to the MBean server or to the MBean's
  107. * ClassLoader. If it is a Java class known to the MBean's
  108. * ClassLoader, it is recommended but not required that the
  109. * class's public methods include those that would appear in a
  110. * Standard MBean implementing the attributes and operations in
  111. * this MBeanInfo.
  112. * @param description A human readable description of the MBean (optional).
  113. * @param attributes The list of exposed attributes of the MBean.
  114. * This may be null with the same effect as a zero-length array.
  115. * @param constructors The list of public constructors of the
  116. * MBean. This may be null with the same effect as a zero-length
  117. * array.
  118. * @param operations The list of operations of the MBean. This
  119. * may be null with the same effect as a zero-length array.
  120. * @param notifications The list of notifications emitted. This
  121. * may be null with the same effect as a zero-length array.
  122. */
  123. public MBeanInfo(String className,
  124. String description,
  125. MBeanAttributeInfo[] attributes,
  126. MBeanConstructorInfo[] constructors,
  127. MBeanOperationInfo[] operations,
  128. MBeanNotificationInfo[] notifications)
  129. throws IllegalArgumentException {
  130. this.className = className;
  131. this.description = description;
  132. if (attributes == null)
  133. attributes = MBeanAttributeInfo.NO_ATTRIBUTES;
  134. this.attributes = attributes;
  135. if (operations == null)
  136. operations = MBeanOperationInfo.NO_OPERATIONS;
  137. this.operations = operations;
  138. if (constructors == null)
  139. constructors = MBeanConstructorInfo.NO_CONSTRUCTORS;
  140. this.constructors = constructors;
  141. if (notifications == null)
  142. notifications = MBeanNotificationInfo.NO_NOTIFICATIONS;
  143. this.notifications = notifications;
  144. this.immutable = isImmutableClass(this.getClass(), MBeanInfo.class);
  145. }
  146. /**
  147. * <p>Returns a shallow clone of this instance.
  148. * The clone is obtained by simply calling <tt>super.clone()</tt>,
  149. * thus calling the default native shallow cloning mechanism
  150. * implemented by <tt>Object.clone()</tt>.
  151. * No deeper cloning of any internal field is made.</p>
  152. *
  153. * <p>Since this class is immutable, the clone method is chiefly of
  154. * interest to subclasses.</p>
  155. */
  156. public Object clone () {
  157. try {
  158. return super.clone() ;
  159. } catch (CloneNotSupportedException e) {
  160. // should not happen as this class is cloneable
  161. return null;
  162. }
  163. }
  164. /**
  165. * Returns the name of the Java class of the MBean described by
  166. * this <CODE>MBeanInfo</CODE>.
  167. *
  168. * @return the class name.
  169. */
  170. public String getClassName() {
  171. return className;
  172. }
  173. /**
  174. * Returns a human readable description of the MBean.
  175. *
  176. * @return the description.
  177. */
  178. public String getDescription() {
  179. return description;
  180. }
  181. /**
  182. * Returns the list of attributes exposed for management.
  183. * Each attribute is described by an <CODE>MBeanAttributeInfo</CODE> object.
  184. *
  185. * The returned array is a shallow copy of the internal array,
  186. * which means that it is a copy of the internal array of
  187. * references to the <CODE>MBeanAttributeInfo</CODE> objects
  188. * but that each referenced <CODE>MBeanAttributeInfo</CODE> object is not copied.
  189. *
  190. * @return An array of <CODE>MBeanAttributeInfo</CODE> objects.
  191. */
  192. public MBeanAttributeInfo[] getAttributes() {
  193. MBeanAttributeInfo[] as = nonNullAttributes();
  194. if (as.length == 0)
  195. return as;
  196. else
  197. return (MBeanAttributeInfo[]) as.clone();
  198. }
  199. private MBeanAttributeInfo[] fastGetAttributes() {
  200. if (immutable)
  201. return nonNullAttributes();
  202. else
  203. return getAttributes();
  204. }
  205. /**
  206. * Return the value of the attributes field, or an empty array if
  207. * the field is null. This can't happen with a
  208. * normally-constructed instance of this class, but can if the
  209. * instance was deserialized from another implementation that
  210. * allows the field to be null. It would be simpler if we enforced
  211. * the class invariant that these fields cannot be null by writing
  212. * a readObject() method, but that would require us to define the
  213. * various array fields as non-final, which is annoying because
  214. * conceptually they are indeed final.
  215. */
  216. private MBeanAttributeInfo[] nonNullAttributes() {
  217. return (attributes == null) ?
  218. MBeanAttributeInfo.NO_ATTRIBUTES : attributes;
  219. }
  220. /**
  221. * Returns the list of operations of the MBean.
  222. * Each operation is described by an <CODE>MBeanOperationInfo</CODE> object.
  223. *
  224. * The returned array is a shallow copy of the internal array,
  225. * which means that it is a copy of the internal array of
  226. * references to the <CODE>MBeanOperationInfo</CODE> objects
  227. * but that each referenced <CODE>MBeanOperationInfo</CODE> object is not copied.
  228. *
  229. * @return An array of <CODE>MBeanOperationInfo</CODE> objects.
  230. */
  231. public MBeanOperationInfo[] getOperations() {
  232. MBeanOperationInfo[] os = nonNullOperations();
  233. if (os.length == 0)
  234. return os;
  235. else
  236. return (MBeanOperationInfo[]) os.clone();
  237. }
  238. private MBeanOperationInfo[] fastGetOperations() {
  239. if (immutable)
  240. return nonNullOperations();
  241. else
  242. return getOperations();
  243. }
  244. private MBeanOperationInfo[] nonNullOperations() {
  245. return (operations == null) ?
  246. MBeanOperationInfo.NO_OPERATIONS : operations;
  247. }
  248. /**
  249. * <p>Returns the list of the public constructors of the MBean.
  250. * Each constructor is described by an
  251. * <CODE>MBeanConstructorInfo</CODE> object.</p>
  252. *
  253. * <p>The returned array is a shallow copy of the internal array,
  254. * which means that it is a copy of the internal array of
  255. * references to the <CODE>MBeanConstructorInfo</CODE> objects but
  256. * that each referenced <CODE>MBeanConstructorInfo</CODE> object
  257. * is not copied.</p>
  258. *
  259. * <p>The returned list is not necessarily exhaustive. That is,
  260. * the MBean may have a public constructor that is not in the
  261. * list. In this case, the MBean server can construct another
  262. * instance of this MBean's class using that constructor, even
  263. * though it is not listed here.</p>
  264. *
  265. * @return An array of <CODE>MBeanConstructorInfo</CODE> objects.
  266. */
  267. public MBeanConstructorInfo[] getConstructors() {
  268. MBeanConstructorInfo[] cs = nonNullConstructors();
  269. if (cs.length == 0)
  270. return cs;
  271. else
  272. return (MBeanConstructorInfo[]) cs.clone();
  273. }
  274. private MBeanConstructorInfo[] fastGetConstructors() {
  275. if (immutable)
  276. return nonNullConstructors();
  277. else
  278. return getConstructors();
  279. }
  280. private MBeanConstructorInfo[] nonNullConstructors() {
  281. return (constructors == null) ?
  282. MBeanConstructorInfo.NO_CONSTRUCTORS : constructors;
  283. }
  284. /**
  285. * Returns the list of the notifications emitted by the MBean.
  286. * Each notification is described by an <CODE>MBeanNotificationInfo</CODE> object.
  287. *
  288. * The returned array is a shallow copy of the internal array,
  289. * which means that it is a copy of the internal array of
  290. * references to the <CODE>MBeanNotificationInfo</CODE> objects
  291. * but that each referenced <CODE>MBeanNotificationInfo</CODE> object is not copied.
  292. *
  293. * @return An array of <CODE>MBeanNotificationInfo</CODE> objects.
  294. */
  295. public MBeanNotificationInfo[] getNotifications() {
  296. MBeanNotificationInfo[] ns = nonNullNotifications();
  297. if (ns.length == 0)
  298. return ns;
  299. else
  300. return (MBeanNotificationInfo[]) ns.clone();
  301. }
  302. private MBeanNotificationInfo[] fastGetNotifications() {
  303. if (immutable)
  304. return nonNullNotifications();
  305. else
  306. return getNotifications();
  307. }
  308. private MBeanNotificationInfo[] nonNullNotifications() {
  309. return (notifications == null) ?
  310. MBeanNotificationInfo.NO_NOTIFICATIONS : notifications;
  311. }
  312. /**
  313. * <p>Compare this MBeanInfo to another. Two MBeanInfo objects
  314. * are equal iff they return equal values for {@link
  315. * #getClassName()} and for {@link #getDescription()}, and the
  316. * arrays returned by the two objects for {@link
  317. * #getAttributes()}, {@link #getOperations()}, {@link
  318. * #getConstructors()}, and {@link #getNotifications()} are
  319. * pairwise equal. Here "equal" means {@link
  320. * Object#equals(Object)}, not identity.</p>
  321. *
  322. * <p>If two MBeanInfo objects return the same values in one of
  323. * their arrays but in a different order then they are not equal.</p>
  324. *
  325. * @param o the object to compare to.
  326. *
  327. * @return true iff <code>o</code> is an MBeanInfo that is equal
  328. * to this one according to the rules above.
  329. */
  330. public boolean equals(Object o) {
  331. if (o == this)
  332. return true;
  333. if (!(o instanceof MBeanInfo))
  334. return false;
  335. MBeanInfo p = (MBeanInfo) o;
  336. if (!p.getClassName().equals(getClassName()) ||
  337. !p.getDescription().equals(getDescription()))
  338. return false;
  339. return
  340. (Arrays.equals(p.fastGetAttributes(), fastGetAttributes()) &&
  341. Arrays.equals(p.fastGetOperations(), fastGetOperations()) &&
  342. Arrays.equals(p.fastGetConstructors(), fastGetConstructors()) &&
  343. Arrays.equals(p.fastGetNotifications(), fastGetNotifications()));
  344. }
  345. public int hashCode() {
  346. /* Since computing the hashCode is quite expensive, we cache it.
  347. If by some terrible misfortune the computed value is 0, the
  348. caching won't work and we will recompute it every time.
  349. We don't bother synchronizing, because, at worst, n different
  350. threads will compute the same hashCode at the same time. */
  351. if (hashCode != 0)
  352. return hashCode;
  353. hashCode =
  354. getClassName().hashCode() ^
  355. arrayHashCode(fastGetAttributes()) ^
  356. arrayHashCode(fastGetOperations()) ^
  357. arrayHashCode(fastGetConstructors()) ^
  358. arrayHashCode(fastGetNotifications());
  359. return hashCode;
  360. }
  361. private static int arrayHashCode(Object[] array) {
  362. int hash = 0;
  363. for (int i = 0; i < array.length; i++)
  364. hash ^= array[i].hashCode();
  365. return hash;
  366. }
  367. /**
  368. * Cached results of previous calls to isImmutableClass. Maps
  369. * Class to Boolean. This is a WeakHashMap so that we don't
  370. * prevent a class from being garbage collected just because
  371. * we know whether it's immutable.
  372. */
  373. private static final Map immutability = new WeakHashMap();
  374. /**
  375. * Return true if <code>subclass</code> is known to preserve the
  376. * immutability of <code>immutableClass</code>. The class
  377. * <code>immutableClass</code> is a reference class that is known
  378. * to be immutable. The subclass <code>subclass</code> is
  379. * considered immutable if it does not override any public method
  380. * of <code>immutableClass</code> whose name begins with "get" or
  381. * "is". This is obviously not an infallible test for immutability,
  382. * but it works for the public interfaces of the MBean*Info classes.
  383. */
  384. static boolean isImmutableClass(Class subclass, Class immutableClass) {
  385. if (subclass == immutableClass)
  386. return true;
  387. synchronized (immutability) {
  388. Boolean immutable = (Boolean) immutability.get(subclass);
  389. if (immutable == null) {
  390. try {
  391. PrivilegedAction immutabilityAction =
  392. new ImmutabilityAction(subclass, immutableClass);
  393. immutable = (Boolean)
  394. AccessController.doPrivileged(immutabilityAction);
  395. } catch (Exception e) { // e.g. SecurityException
  396. /* We don't know, so we assume it isn't. */
  397. immutable = Boolean.FALSE;
  398. }
  399. immutability.put(subclass, immutable);
  400. }
  401. return immutable.booleanValue();
  402. }
  403. }
  404. /*
  405. * The PrivilegedAction stuff is probably overkill. We can be
  406. * pretty sure the caller does have the required privileges -- a
  407. * JMX user that can't do reflection can't even use Standard
  408. * MBeans! But there's probably a performance gain by not having
  409. * to check the whole call stack.
  410. */
  411. private static class ImmutabilityAction implements PrivilegedAction {
  412. private final Class subclass;
  413. private final Class immutableClass;
  414. ImmutabilityAction(Class subclass, Class immutableClass) {
  415. this.subclass = subclass;
  416. this.immutableClass = immutableClass;
  417. }
  418. public Object run() {
  419. Method[] methods = immutableClass.getMethods();
  420. for (int i = 0; i < methods.length; i++) {
  421. Method method = methods[i];
  422. String methodName = method.getName();
  423. if (methodName.startsWith("get")
  424. || methodName.startsWith("is")) {
  425. Class[] paramTypes = method.getParameterTypes();
  426. try {
  427. Method submethod =
  428. subclass.getMethod(methodName, paramTypes);
  429. if (!submethod.equals(method))
  430. return Boolean.FALSE;
  431. } catch (NoSuchMethodException e) {
  432. return Boolean.FALSE;
  433. }
  434. }
  435. }
  436. return Boolean.TRUE;
  437. }
  438. }
  439. }