StandardMBean.java 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /* StandardMBean.java -- A standard reflection-based 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.lang.reflect.Constructor;
  33. import java.lang.reflect.InvocationTargetException;
  34. import java.lang.reflect.Method;
  35. import java.util.ArrayList;
  36. import java.util.HashMap;
  37. import java.util.Iterator;
  38. import java.util.List;
  39. import java.util.Map;
  40. /**
  41. * Provides a dynamic management bean by using reflection on an
  42. * interface and an implementing class. By default, a bean instance
  43. * is paired up with its interface based on specific naming
  44. * conventions (if the implementation is called X, the interface must
  45. * be XMBean). Using this class removes the need to use a specific
  46. * naming system to match up the two. Instead, an instance of this
  47. * bean is created either via explicit construction or subclassing,
  48. * and this provides access to the attributes, constructors and
  49. * operations of the implementation via reflection. Various hooks are
  50. * provided in order to allow customization of this process.
  51. *
  52. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  53. * @since 1.5
  54. */
  55. public class StandardMBean
  56. implements DynamicMBean
  57. {
  58. /**
  59. * The interface for this bean.
  60. */
  61. private Class<?> iface;
  62. /**
  63. * The implementation of the interface.
  64. */
  65. private Object impl;
  66. /**
  67. * Cached bean information.
  68. */
  69. private MBeanInfo info;
  70. /**
  71. * Constructs a new {@link StandardMBean} using the specified
  72. * interface and <code>this</code> as the instance. This should
  73. * be used to create an instance via subclassing.
  74. *
  75. * @param iface the interface this bean implements, or <code>null</code>
  76. * if the interface should be determined using the naming
  77. * convention (class X has interface XMBean).
  78. * @throws NotCompliantMBeanException if this class doesn't implement
  79. * the interface or a method appears
  80. * in the interface that doesn't comply
  81. * with the naming conventions.
  82. */
  83. protected StandardMBean(Class<?> iface)
  84. throws NotCompliantMBeanException
  85. {
  86. if (iface == null)
  87. {
  88. String className = getClass().getName();
  89. try
  90. {
  91. iface = Class.forName(className + "MBean");
  92. }
  93. catch (ClassNotFoundException e)
  94. {
  95. for (Class<?> nextIface : getClass().getInterfaces())
  96. {
  97. if (JMX.isMXBeanInterface(nextIface))
  98. {
  99. iface = nextIface;
  100. break;
  101. }
  102. }
  103. if (iface == null)
  104. throw (NotCompliantMBeanException)
  105. (new NotCompliantMBeanException("An interface for the class "
  106. + className +
  107. " was not found.").initCause(e));
  108. }
  109. }
  110. if (!(iface.isInstance(this)))
  111. throw new NotCompliantMBeanException("The instance, " + impl +
  112. ", is not an instance of " + iface);
  113. impl = this;
  114. this.iface = iface;
  115. }
  116. /**
  117. * Constructs a new {@link StandardMBean} using the specified
  118. * interface and the supplied instance as the implementation.
  119. *
  120. * @param impl the implementation.
  121. * @param iface the interface the bean implements, or <code>null</code>
  122. * if the interface should be determined using the naming
  123. * convention (class X has interface XMBean).
  124. * @throws IllegalArgumentException if <code>impl</code> is <code>null</code>.
  125. * @throws NotCompliantMBeanException if <code>impl</code> doesn't implement
  126. * the interface or a method appears
  127. * in the interface that doesn't comply
  128. * with the naming conventions.
  129. */
  130. public <T> StandardMBean(T impl, Class<T> iface)
  131. throws NotCompliantMBeanException
  132. {
  133. if (impl == null)
  134. throw new IllegalArgumentException("The specified implementation is null.");
  135. if (iface == null)
  136. {
  137. Class<?> implClass = impl.getClass();
  138. String className = implClass.getName();
  139. try
  140. {
  141. this.iface = Class.forName(className + "MBean", true,
  142. implClass.getClassLoader());
  143. }
  144. catch (ClassNotFoundException e)
  145. {
  146. for (Class<?> nextIface : implClass.getInterfaces())
  147. {
  148. if (JMX.isMXBeanInterface(nextIface))
  149. {
  150. this.iface = nextIface;
  151. break;
  152. }
  153. }
  154. if (this.iface == null)
  155. throw (NotCompliantMBeanException)
  156. (new NotCompliantMBeanException("An interface for the class " +
  157. className +
  158. " was not found.").initCause(e));
  159. }
  160. }
  161. else
  162. this.iface = iface;
  163. if (!(this.iface.isInstance(impl)))
  164. throw new NotCompliantMBeanException("The instance, " + impl +
  165. ", is not an instance of " + iface);
  166. this.impl = impl;
  167. }
  168. /**
  169. * Caches the {@link MBeanInfo} instance for this object. This is a
  170. * customization hook, so that subclasses can choose the caching policy
  171. * used. The default implementation caches the value in the instance
  172. * itself. Subclasses may override this so as to not cache the data
  173. * at all, or so as to use a cache shared between multiple beans.
  174. *
  175. * @param info the {@link MBeanInfo} instance to cache, or <code>null</code>
  176. * if there is no new value to cache. When the value is not
  177. * <code>null</code>, the cache should replace the current value
  178. * with the value supplied here.
  179. * @see #getCachedMBeanInfo()
  180. */
  181. protected void cacheMBeanInfo(MBeanInfo info)
  182. {
  183. if (info != null)
  184. this.info = info;
  185. }
  186. /**
  187. * Obtains the value of the specified attribute of the
  188. * management bean. The management bean should perform
  189. * a lookup for the named attribute, and return its value
  190. * by calling the appropriate getter method, if possible.
  191. *
  192. * @param name the name of the attribute to retrieve.
  193. * @return the value of the specified attribute.
  194. * @throws AttributeNotFoundException if the name does not
  195. * correspond to an attribute
  196. * of the bean.
  197. * @throws MBeanException if retrieving the attribute causes
  198. * the bean to throw an exception (which
  199. * becomes the cause of this exception).
  200. * @throws ReflectionException if an exception occurred in trying
  201. * to use the reflection interface
  202. * to lookup the attribute. The
  203. * thrown exception is the cause of
  204. * this exception.
  205. * @see #setAttribute(String)
  206. */
  207. public Object getAttribute(String name)
  208. throws AttributeNotFoundException, MBeanException,
  209. ReflectionException
  210. {
  211. Method getter;
  212. try
  213. {
  214. getter = iface.getMethod("get" + name);
  215. }
  216. catch (NoSuchMethodException e)
  217. {
  218. try
  219. {
  220. getter = iface.getMethod("is" + name);
  221. }
  222. catch (NoSuchMethodException ex)
  223. {
  224. throw ((AttributeNotFoundException)
  225. new AttributeNotFoundException("The attribute, " + name +
  226. ", was not found.").initCause(ex));
  227. }
  228. }
  229. Object result;
  230. try
  231. {
  232. result = getter.invoke(impl);
  233. }
  234. catch (IllegalAccessException e)
  235. {
  236. throw new ReflectionException(e, "Failed to retrieve " + name);
  237. }
  238. catch (IllegalArgumentException e)
  239. {
  240. throw new ReflectionException(e, "Failed to retrieve " + name);
  241. }
  242. catch (InvocationTargetException e)
  243. {
  244. throw new MBeanException((Exception) e.getCause(),
  245. "The getter of " + name +
  246. " threw an exception");
  247. }
  248. return result;
  249. }
  250. /**
  251. * Obtains the values of each of the specified attributes
  252. * of the management bean. The returned list includes
  253. * those attributes that were retrieved and their
  254. * corresponding values.
  255. *
  256. * @param names the names of the attributes to retrieve.
  257. * @return a list of the retrieved attributes.
  258. * @see #setAttributes(AttributeList)
  259. */
  260. public AttributeList getAttributes(String[] names)
  261. {
  262. AttributeList list = new AttributeList(names.length);
  263. for (int a = 0; a < names.length; ++a)
  264. {
  265. try
  266. {
  267. Object value = getAttribute(names[a]);
  268. list.add(new Attribute(names[a], value));
  269. }
  270. catch (AttributeNotFoundException e)
  271. {
  272. /* Ignored */
  273. }
  274. catch (ReflectionException e)
  275. {
  276. /* Ignored */
  277. }
  278. catch (MBeanException e)
  279. {
  280. /* Ignored */
  281. }
  282. }
  283. return list;
  284. }
  285. /**
  286. * Returns the cached {@link MBeanInfo} instance for this object. This is a
  287. * customization hook, so that subclasses can choose the caching policy
  288. * used. The default implementation caches the value in the instance
  289. * itself, and returns this value on calls to this method.
  290. *
  291. * @return the cached {@link MBeanInfo} instance, or <code>null</code>
  292. * if no value is cached.
  293. * @see #cacheMBeanInfo(javax.management.MBeanInfo)
  294. */
  295. protected MBeanInfo getCachedMBeanInfo()
  296. {
  297. return info;
  298. }
  299. /**
  300. * Returns the class name that will be used in the {@link MBeanInfo}
  301. * instance. This is a customization hook, so that subclasses can
  302. * provide a custom class name. By default, this returns the class
  303. * name from the supplied {@link MBeanInfo} instance.
  304. *
  305. * @param info the {@link MBeanInfo} instance constructed via
  306. * reflection.
  307. * @return the class name to use in the instance.
  308. */
  309. protected String getClassName(MBeanInfo info)
  310. {
  311. return info.getClassName();
  312. }
  313. /**
  314. * Returns information on the constructors that will be used in
  315. * the {@link MBeanInfo} instance. This is a customization hook,
  316. * so that subclasses can provide their own information on the
  317. * bean's constructors, if necessary. By default, this method
  318. * returns <code>null</code> unless the implementation supplied
  319. * is either <code>null</code> or <code>this</code>. This default
  320. * implementation prevents the use of
  321. * {@link MBeanServer#createMBean} in cases where the bean is
  322. * not created as a subclass of {@link StandardMBean}.
  323. *
  324. * @param constructors the constructor information created via
  325. * reflection.
  326. * @param impl the implementation, or <code>null</code> if this
  327. * should be ignored.
  328. * @return the constructor information to use.
  329. */
  330. protected MBeanConstructorInfo[] getConstructors(MBeanConstructorInfo[]
  331. constructors, Object impl)
  332. {
  333. if (impl == null || impl == this)
  334. return constructors;
  335. return null;
  336. }
  337. /**
  338. * Returns the description of the attribute that will be used in
  339. * the supplied {@link MBeanAttributeInfo} instance. This is a
  340. * customization hook, so that subclasses can provide a custom
  341. * description. By default, this calls
  342. * {@link #getDescription(MBeanFeatureInfo)} with the supplied
  343. * {@link MBeanAttributeInfo} instance.
  344. *
  345. * @param info the {@link MBeanAttributeInfo} instance constructed
  346. * via reflection.
  347. * @return the description to use in the instance.
  348. */
  349. protected String getDescription(MBeanAttributeInfo info)
  350. {
  351. return getDescription((MBeanFeatureInfo) info);
  352. }
  353. /**
  354. * Returns the description of the constructor that will be used in
  355. * the supplied {@link MBeanConstructorInfo} instance. This is a
  356. * customization hook, so that subclasses can provide a custom
  357. * description. By default, this calls
  358. * {@link #getDescription(MBeanFeatureInfo)} with the supplied
  359. * {@link MBeanConstructorInfo} instance.
  360. *
  361. * @param info the {@link MBeanConstructorInfo} instance constructed
  362. * via reflection.
  363. * @return the description to use in the instance.
  364. */
  365. protected String getDescription(MBeanConstructorInfo info)
  366. {
  367. return getDescription((MBeanFeatureInfo) info);
  368. }
  369. /**
  370. * Returns the description of the nth parameter of the constructor
  371. * that will be used in the supplied {@link MBeanParameterInfo}
  372. * instance. This is a customization hook, so that subclasses
  373. * can provide a custom description. By default, this calls
  374. * <code>param.getDescription()</code>.
  375. *
  376. * @param info the {@link MBeanConstructorInfo} instance constructed
  377. * via reflection.
  378. * @param param the {@link MBeanParameterInfo} instance constructed
  379. * via reflection.
  380. * @param n the number of the parameter, in order to link it to the
  381. * information on the constructor.
  382. * @return the description to use in the instance.
  383. */
  384. protected String getDescription(MBeanConstructorInfo info,
  385. MBeanParameterInfo param, int n)
  386. {
  387. return param.getDescription();
  388. }
  389. /**
  390. * Returns the description of the supplied feature that
  391. * will be used in the supplied {@link MBeanFeatureInfo}
  392. * instance. This is a customization hook, so that subclasses
  393. * can provide a custom description. By default, this calls
  394. * <code>info.getDescription()</code>. This method is also called
  395. * by default for the more specific description methods for attributes,
  396. * constructors and operations.
  397. *
  398. * @param info the {@link MBeanFeatureInfo} instance constructed
  399. * via reflection.
  400. * @return the description to use in the instance.
  401. */
  402. protected String getDescription(MBeanFeatureInfo info)
  403. {
  404. return info.getDescription();
  405. }
  406. /**
  407. * Returns the description of the bean that will be used in the
  408. * supplied {@link MBeanInfo} instance. This is a customization
  409. * hook, so that subclasses can provide a custom description. By
  410. * default, this calls <code>info.getDescription()</code>.
  411. *
  412. * @param info the {@link MBeanInfo} instance constructed
  413. * via reflection.
  414. * @return the description to use in the instance.
  415. */
  416. protected String getDescription(MBeanInfo info)
  417. {
  418. return info.getDescription();
  419. }
  420. /**
  421. * Returns the description of the operation that will be used in
  422. * the supplied {@link MBeanOperationInfo} instance. This is a
  423. * customization hook, so that subclasses can provide a custom
  424. * description. By default, this calls
  425. * {@link #getDescription(MBeanFeatureInfo)} with the supplied
  426. * {@link MBeanOperationInfo} instance.
  427. *
  428. * @param info the {@link MBeanOperationInfo} instance constructed
  429. * via reflection.
  430. * @return the description to use in the instance.
  431. */
  432. protected String getDescription(MBeanOperationInfo info)
  433. {
  434. return getDescription((MBeanFeatureInfo) info);
  435. }
  436. /**
  437. * Returns the description of the nth parameter of the operation
  438. * that will be used in the supplied {@link MBeanParameterInfo}
  439. * instance. This is a customization hook, so that subclasses
  440. * can provide a custom description. By default, this calls
  441. * <code>param.getDescription()</code>.
  442. *
  443. * @param info the {@link MBeanOperationInfo} instance constructed
  444. * via reflection.
  445. * @param param the {@link MBeanParameterInfo} instance constructed
  446. * via reflection.
  447. * @param n the number of the parameter, in order to link it to the
  448. * information on the operation.
  449. * @return the description to use in the instance.
  450. */
  451. protected String getDescription(MBeanOperationInfo info,
  452. MBeanParameterInfo param, int n)
  453. {
  454. return param.getDescription();
  455. }
  456. /**
  457. * Returns the impact of the operation that will be used in the
  458. * supplied {@link MBeanOperationInfo} instance. This is a
  459. * customization hook, so that subclasses can provide a custom
  460. * impact flag. By default, this returns
  461. * <code>info.getImpact()</code>.
  462. *
  463. * @param info the {@link MBeanOperationInfo} instance constructed
  464. * via reflection.
  465. * @return the impact flag to use in the instance.
  466. */
  467. protected int getImpact(MBeanOperationInfo info)
  468. {
  469. return info.getImpact();
  470. }
  471. /**
  472. * Returns the instance that implements this bean.
  473. *
  474. * @return the implementation.
  475. */
  476. public Object getImplementation()
  477. {
  478. return impl;
  479. }
  480. /**
  481. * Returns the class of the instance that implements this bean.
  482. *
  483. * @return the implementation class.
  484. */
  485. public Class<?> getImplementationClass()
  486. {
  487. return impl.getClass();
  488. }
  489. /**
  490. * <p>
  491. * Returns an information object which lists the attributes
  492. * and actions associated with the management bean. This
  493. * implementation proceeds as follows:
  494. * </p>
  495. * <ol>
  496. * <li>{@link #getCachedMBeanInfo()} is called to obtain
  497. * the cached instance. If this returns a non-null value,
  498. * this value is returned.</li>
  499. * <li>If there is no cached value, then the method proceeds
  500. * to create one. During this process, the customization hooks
  501. * detailed in this class are called to allow the values used
  502. * to be overrided:
  503. * <ul>
  504. * <li>For each attribute,
  505. * {@link #getDescription(MBeanAttributeInfo)} is called.</li>
  506. * <li>For each constructor,
  507. * {@link #getDescription(MBeanConstructorInfo)} is called,
  508. * along with {@link #getDescription(MBeanConstructorInfo,
  509. * MBeanParameterInfo, int)} and
  510. * {@link #getParameterName(MBeanConstructorInfo,
  511. * MBeanParameterInfo, int)} for each parameter.</li>
  512. * <li>The constructors may be replaced as a whole by
  513. * a call to
  514. * {@link #getConstructors(MBeanConstructorInfo[], Object)}.</li>
  515. * <li>For each operation,
  516. * {@link #getDescription(MBeanOperationInfo)} and
  517. * {@link #getImpact(MBeanOperationInfo)} are called,
  518. * along with {@link #getDescription(MBeanOperationInfo,
  519. * MBeanParameterInfo, int)} and
  520. * {@link #getParameterName(MBeanOperationInfo,
  521. * MBeanParameterInfo, int)} for each parameter.</li>
  522. * <li>{@link #getClassName(MBeanInfo)} and
  523. * {@link #getDescription(MBeanInfo)} are called to customise
  524. * the basic information about the class.</li>
  525. * </ul>
  526. * </li>
  527. * <li>Finally, {@link #cacheMBeanInfo(MBeanInfo)} is called
  528. * with the created instance before it is returned.</li>
  529. * </ol>
  530. *
  531. * @return a description of the management bean, including
  532. * all exposed attributes and actions.
  533. */
  534. public MBeanInfo getMBeanInfo()
  535. {
  536. MBeanInfo info = getCachedMBeanInfo();
  537. if (info != null)
  538. return info;
  539. Method[] methods = iface.getMethods();
  540. Map<String,Method[]> attributes = new HashMap<String,Method[]>();
  541. List<MBeanOperationInfo> operations = new ArrayList<MBeanOperationInfo>();
  542. for (int a = 0; a < methods.length; ++a)
  543. {
  544. String name = methods[a].getName();
  545. if (((name.startsWith("get") &&
  546. methods[a].getReturnType() != Void.TYPE) ||
  547. (name.startsWith("is") &&
  548. methods[a].getReturnType() == Boolean.TYPE)) &&
  549. methods[a].getParameterTypes().length == 0)
  550. {
  551. Method[] amethods;
  552. String attrib;
  553. if (name.startsWith("is"))
  554. attrib = name.substring(2);
  555. else
  556. attrib = name.substring(3);
  557. if (attributes.containsKey(attrib))
  558. amethods = (Method[]) attributes.get(attrib);
  559. else
  560. {
  561. amethods = new Method[2];
  562. attributes.put(attrib, amethods);
  563. }
  564. amethods[0] = methods[a];
  565. }
  566. else if (name.startsWith("set") &&
  567. methods[a].getReturnType() == Void.TYPE &&
  568. methods[a].getParameterTypes().length == 1)
  569. {
  570. Method[] amethods;
  571. String attrib = name.substring(3);
  572. if (attributes.containsKey(attrib))
  573. amethods = (Method[]) attributes.get(attrib);
  574. else
  575. {
  576. amethods = new Method[2];
  577. attributes.put(attrib, amethods);
  578. }
  579. amethods[1] = methods[a];
  580. }
  581. else
  582. operations.add(new MBeanOperationInfo(methods[a].getName(),
  583. methods[a]));
  584. }
  585. List<MBeanAttributeInfo> attribs = new ArrayList<MBeanAttributeInfo>(attributes.size());
  586. for (Map.Entry<String,Method[]> entry : attributes.entrySet())
  587. {
  588. Method[] amethods = entry.getValue();
  589. try
  590. {
  591. attribs.add(new MBeanAttributeInfo(entry.getKey(),
  592. entry.getKey(),
  593. amethods[0], amethods[1]));
  594. }
  595. catch (IntrospectionException e)
  596. {
  597. /* Shouldn't happen; both shouldn't be null */
  598. throw new IllegalStateException("The two methods passed to " +
  599. "the MBeanAttributeInfo " +
  600. "constructor for " + entry +
  601. "were null.", e);
  602. }
  603. }
  604. MBeanAttributeInfo[] ainfo = new MBeanAttributeInfo[attribs.size()];
  605. for (int a = 0; a < ainfo.length; ++a)
  606. {
  607. MBeanAttributeInfo oldInfo = (MBeanAttributeInfo) attribs.get(a);
  608. String desc = getDescription(oldInfo);
  609. ainfo[a] = new MBeanAttributeInfo(oldInfo.getName(),
  610. oldInfo.getType(), desc,
  611. oldInfo.isReadable(),
  612. oldInfo.isWritable(),
  613. oldInfo.isIs());
  614. }
  615. Constructor<?>[] cons = impl.getClass().getConstructors();
  616. MBeanConstructorInfo[] cinfo = new MBeanConstructorInfo[cons.length];
  617. for (int a = 0; a < cinfo.length; ++a)
  618. {
  619. MBeanConstructorInfo oldInfo = new MBeanConstructorInfo(cons[a].getName(),
  620. cons[a]);
  621. String desc = getDescription(oldInfo);
  622. MBeanParameterInfo[] params = oldInfo.getSignature();
  623. MBeanParameterInfo[] pinfo = new MBeanParameterInfo[params.length];
  624. for (int b = 0; b < pinfo.length; ++b)
  625. {
  626. String pdesc = getDescription(oldInfo, params[b], b);
  627. String pname = getParameterName(oldInfo, params[b], b);
  628. pinfo[b] = new MBeanParameterInfo(pname, params[b].getType(),
  629. pdesc);
  630. }
  631. cinfo[a] = new MBeanConstructorInfo(oldInfo.getName(), desc,
  632. pinfo);
  633. }
  634. cinfo = getConstructors(cinfo, impl);
  635. MBeanOperationInfo[] oinfo = new MBeanOperationInfo[operations.size()];
  636. for (int a = 0; a < oinfo.length; ++a)
  637. {
  638. MBeanOperationInfo oldInfo = (MBeanOperationInfo) operations.get(a);
  639. String desc = getDescription(oldInfo);
  640. int impact = getImpact(oldInfo);
  641. MBeanParameterInfo[] params = oldInfo.getSignature();
  642. MBeanParameterInfo[] pinfo = new MBeanParameterInfo[params.length];
  643. for (int b = 0; b < pinfo.length; ++b)
  644. {
  645. String pdesc = getDescription(oldInfo, params[b], b);
  646. String pname = getParameterName(oldInfo, params[b], b);
  647. pinfo[b] = new MBeanParameterInfo(pname, params[b].getType(),
  648. pdesc);
  649. }
  650. oinfo[a] = new MBeanOperationInfo(oldInfo.getName(), desc, pinfo,
  651. oldInfo.getReturnType(), impact);
  652. }
  653. info = new MBeanInfo(impl.getClass().getName(), impl.getClass().getName(),
  654. ainfo, cinfo, oinfo, null);
  655. String cname = getClassName(info);
  656. String desc = getDescription(info);
  657. MBeanNotificationInfo[] ninfo = null;
  658. if (impl instanceof NotificationBroadcaster)
  659. ninfo = ((NotificationBroadcaster) impl).getNotificationInfo();
  660. info = new MBeanInfo(cname, desc, ainfo, cinfo, oinfo, ninfo);
  661. cacheMBeanInfo(info);
  662. return info;
  663. }
  664. /**
  665. * Returns the interface for this management bean.
  666. *
  667. * @return the management interface.
  668. */
  669. public final Class<?> getMBeanInterface()
  670. {
  671. return iface;
  672. }
  673. /**
  674. * Returns the name of the nth parameter of the constructor
  675. * that will be used in the supplied {@link MBeanParameterInfo}
  676. * instance. This is a customization hook, so that subclasses
  677. * can provide a custom name. By default, this calls
  678. * <code>param.getName()</code>.
  679. *
  680. * @param info the {@link MBeanConstructorInfo} instance constructed
  681. * via reflection.
  682. * @param param the {@link MBeanParameterInfo} instance constructed
  683. * via reflection.
  684. * @param n the number of the parameter, in order to link it to the
  685. * information on the constructor.
  686. * @return the name to use in the instance.
  687. */
  688. protected String getParameterName(MBeanConstructorInfo info,
  689. MBeanParameterInfo param, int n)
  690. {
  691. return param.getName();
  692. }
  693. /**
  694. * Returns the name of the nth parameter of the operation
  695. * that will be used in the supplied {@link MBeanParameterInfo}
  696. * instance. This is a customization hook, so that subclasses
  697. * can provide a custom name. By default, this calls
  698. * <code>param.getName()</code>.
  699. *
  700. * @param info the {@link MBeanOperationInfo} instance constructed
  701. * via reflection.
  702. * @param param the {@link MBeanParameterInfo} instance constructed
  703. * via reflection.
  704. * @param n the number of the parameter, in order to link it to the
  705. * information on the operation.
  706. * @return the name to use in the instance.
  707. */
  708. protected String getParameterName(MBeanOperationInfo info,
  709. MBeanParameterInfo param, int n)
  710. {
  711. return param.getName();
  712. }
  713. /**
  714. * Invokes the specified action on the management bean using
  715. * the supplied parameters. The signature of the action is
  716. * specified by a {@link String} array, which lists the classes
  717. * corresponding to each parameter. The class loader used to
  718. * load these classes is the same as that used for loading the
  719. * management bean itself.
  720. *
  721. * @param name the name of the action to invoke.
  722. * @param params the parameters used to call the action.
  723. * @param signature the signature of the action.
  724. * @return the return value of the action.
  725. * @throws MBeanException if the action throws an exception. The
  726. * thrown exception is the cause of this
  727. * exception.
  728. * @throws ReflectionException if an exception occurred in trying
  729. * to use the reflection interface
  730. * to invoke the action. The
  731. * thrown exception is the cause of
  732. * this exception.
  733. */
  734. public Object invoke(String name, Object[] params, String[] signature)
  735. throws MBeanException, ReflectionException
  736. {
  737. if (name.startsWith("get") || name.startsWith("is") ||
  738. name.startsWith("set"))
  739. throw new ReflectionException(new NoSuchMethodException(),
  740. "Invocation of an attribute " +
  741. "method is disallowed.");
  742. ClassLoader loader = getClass().getClassLoader();
  743. Class<?>[] sigTypes;
  744. if (signature != null)
  745. {
  746. sigTypes = new Class<?>[signature.length];
  747. for (int a = 0; a < signature.length; ++a)
  748. try
  749. {
  750. sigTypes[a] = Class.forName(signature[a], true, loader);
  751. }
  752. catch (ClassNotFoundException e)
  753. {
  754. throw new ReflectionException(e, "The class, " + signature[a] +
  755. ", in the method signature " +
  756. "could not be loaded.");
  757. }
  758. }
  759. else
  760. sigTypes = null;
  761. Method method;
  762. try
  763. {
  764. method = iface.getMethod(name, sigTypes);
  765. }
  766. catch (NoSuchMethodException e)
  767. {
  768. throw new ReflectionException(e, "The method, " + name +
  769. ", could not be found.");
  770. }
  771. Object result;
  772. try
  773. {
  774. result = method.invoke(impl, params);
  775. }
  776. catch (IllegalAccessException e)
  777. {
  778. throw new ReflectionException(e, "Failed to call " + name);
  779. }
  780. catch (IllegalArgumentException e)
  781. {
  782. throw new ReflectionException(e, "Failed to call " + name);
  783. }
  784. catch (InvocationTargetException e)
  785. {
  786. throw new MBeanException((Exception) e.getCause(), "The method "
  787. + name + " threw an exception");
  788. }
  789. return result;
  790. }
  791. /**
  792. * Sets the value of the specified attribute of the
  793. * management bean. The management bean should perform
  794. * a lookup for the named attribute, and sets its value
  795. * using the associated setter method, if possible.
  796. *
  797. * @param attribute the attribute to set.
  798. * @throws AttributeNotFoundException if the attribute does not
  799. * correspond to an attribute
  800. * of the bean.
  801. * @throws InvalidAttributeValueException if the value is invalid
  802. * for this particular
  803. * attribute of the bean.
  804. * @throws MBeanException if setting the attribute causes
  805. * the bean to throw an exception (which
  806. * becomes the cause of this exception).
  807. * @throws ReflectionException if an exception occurred in trying
  808. * to use the reflection interface
  809. * to lookup the attribute. The
  810. * thrown exception is the cause of
  811. * this exception.
  812. * @see #getAttribute(String)
  813. */
  814. public void setAttribute(Attribute attribute)
  815. throws AttributeNotFoundException, InvalidAttributeValueException,
  816. MBeanException, ReflectionException
  817. {
  818. String name = attribute.getName();
  819. String attName = name.substring(0, 1).toUpperCase() + name.substring(1);
  820. Object val = attribute.getValue();
  821. try
  822. {
  823. getMutator(attName, val.getClass()).invoke(impl, new Object[] { val });
  824. }
  825. catch (IllegalAccessException e)
  826. {
  827. throw new ReflectionException(e, "Failed to set " + name);
  828. }
  829. catch (IllegalArgumentException e)
  830. {
  831. throw ((InvalidAttributeValueException)
  832. new InvalidAttributeValueException(attribute.getValue() +
  833. " is an invalid value for " +
  834. name).initCause(e));
  835. }
  836. catch (InvocationTargetException e)
  837. {
  838. throw new MBeanException(e, "The getter of " + name +
  839. " threw an exception");
  840. }
  841. }
  842. /**
  843. * Sets the value of each of the specified attributes
  844. * to that supplied by the {@link Attribute} object.
  845. * The returned list contains the attributes that were
  846. * set and their new values.
  847. *
  848. * @param attributes the attributes to set.
  849. * @return a list of the changed attributes.
  850. * @see #getAttributes(AttributeList)
  851. */
  852. public AttributeList setAttributes(AttributeList attributes)
  853. {
  854. AttributeList list = new AttributeList(attributes.size());
  855. Iterator<Object> it = attributes.iterator();
  856. while (it.hasNext())
  857. {
  858. try
  859. {
  860. Attribute attrib = (Attribute) it.next();
  861. setAttribute(attrib);
  862. list.add(attrib);
  863. }
  864. catch (AttributeNotFoundException e)
  865. {
  866. /* Ignored */
  867. }
  868. catch (InvalidAttributeValueException e)
  869. {
  870. /* Ignored */
  871. }
  872. catch (ReflectionException e)
  873. {
  874. /* Ignored */
  875. }
  876. catch (MBeanException e)
  877. {
  878. /* Ignored */
  879. }
  880. }
  881. return list;
  882. }
  883. /**
  884. * Replaces the implementation of the interface used by this
  885. * instance with the one specified. The new implementation
  886. * must be non-null and implement the interface specified on
  887. * construction of this instance.
  888. *
  889. * @throws IllegalArgumentException if <code>impl</code> is <code>null</code>.
  890. * @throws NotCompliantMBeanException if <code>impl</code> doesn't implement
  891. * the interface or a method appears
  892. * in the interface that doesn't comply
  893. * with the naming conventions.
  894. */
  895. public void setImplementation(Object impl)
  896. throws NotCompliantMBeanException
  897. {
  898. if (impl == null)
  899. throw new IllegalArgumentException("The specified implementation is null.");
  900. if (!(iface.isInstance(impl)))
  901. throw new NotCompliantMBeanException("The instance, " + impl +
  902. ", is not an instance of " + iface);
  903. this.impl = impl;
  904. }
  905. /**
  906. * Returns the mutator method for a particular attribute name
  907. * with a parameter type matching that of the given value.
  908. *
  909. * @param name the name of the attribute.
  910. * @param type the type of the parameter.
  911. * @return the appropriate mutator method.
  912. * @throws AttributeNotFoundException if a method can't be found.
  913. */
  914. private Method getMutator(String name, Class<?> type)
  915. throws AttributeNotFoundException
  916. {
  917. String mutator = "set" + name;
  918. Exception ex = null;
  919. try
  920. {
  921. return iface.getMethod(mutator, type);
  922. }
  923. catch (NoSuchMethodException e)
  924. {
  925. /* Ignored; we'll try harder instead */
  926. ex = e;
  927. }
  928. /* Special cases */
  929. if (type == Boolean.class)
  930. try
  931. {
  932. return iface.getMethod(mutator, Boolean.TYPE);
  933. }
  934. catch (NoSuchMethodException e)
  935. {
  936. throw ((AttributeNotFoundException)
  937. new AttributeNotFoundException("The attribute, " + name +
  938. ", was not found.").initCause(e));
  939. }
  940. if (type == Byte.class)
  941. try
  942. {
  943. return iface.getMethod(mutator, Byte.TYPE);
  944. }
  945. catch (NoSuchMethodException e)
  946. {
  947. throw ((AttributeNotFoundException)
  948. new AttributeNotFoundException("The attribute, " + name +
  949. ", was not found.").initCause(e));
  950. }
  951. if (type == Character.class)
  952. try
  953. {
  954. return iface.getMethod(mutator, Character.TYPE);
  955. }
  956. catch (NoSuchMethodException e)
  957. {
  958. throw ((AttributeNotFoundException)
  959. new AttributeNotFoundException("The attribute, " + name +
  960. ", was not found.").initCause(e));
  961. }
  962. if (type == Double.class)
  963. try
  964. {
  965. return iface.getMethod(mutator, Double.TYPE);
  966. }
  967. catch (NoSuchMethodException e)
  968. {
  969. throw ((AttributeNotFoundException)
  970. new AttributeNotFoundException("The attribute, " + name +
  971. ", was not found.").initCause(e));
  972. }
  973. if (type == Float.class)
  974. try
  975. {
  976. return iface.getMethod(mutator, Float.TYPE);
  977. }
  978. catch (NoSuchMethodException e)
  979. {
  980. throw ((AttributeNotFoundException)
  981. new AttributeNotFoundException("The attribute, " + name +
  982. ", was not found.").initCause(e));
  983. }
  984. if (type == Integer.class)
  985. try
  986. {
  987. return iface.getMethod(mutator, Integer.TYPE);
  988. }
  989. catch (NoSuchMethodException e)
  990. {
  991. throw ((AttributeNotFoundException)
  992. new AttributeNotFoundException("The attribute, " + name +
  993. ", was not found.").initCause(e));
  994. }
  995. if (type == Long.class)
  996. try
  997. {
  998. return iface.getMethod(mutator, Long.TYPE);
  999. }
  1000. catch (NoSuchMethodException e)
  1001. {
  1002. throw ((AttributeNotFoundException)
  1003. new AttributeNotFoundException("The attribute, " + name +
  1004. ", was not found.").initCause(e));
  1005. }
  1006. if (type == Short.class)
  1007. try
  1008. {
  1009. return iface.getMethod(mutator, Short.TYPE);
  1010. }
  1011. catch (NoSuchMethodException e)
  1012. {
  1013. throw ((AttributeNotFoundException)
  1014. new AttributeNotFoundException("The attribute, " + name +
  1015. ", was not found.").initCause(e));
  1016. }
  1017. /* Superclasses and interfaces */
  1018. for (Class<?> i : type.getInterfaces())
  1019. try
  1020. {
  1021. return getMutator(name, i);
  1022. }
  1023. catch (AttributeNotFoundException e)
  1024. {
  1025. ex = e;
  1026. }
  1027. Class<?> sclass = type.getSuperclass();
  1028. if (sclass != null && sclass != Object.class)
  1029. try
  1030. {
  1031. return getMutator(name, sclass);
  1032. }
  1033. catch (AttributeNotFoundException e)
  1034. {
  1035. ex = e;
  1036. }
  1037. /* If we get this far, give up */
  1038. throw ((AttributeNotFoundException)
  1039. new AttributeNotFoundException("The attribute, " + name +
  1040. ", was not found.").initCause(ex));
  1041. }
  1042. }