1. /*
  2. * @(#)DynamicMetaDataImpl.java 1.27 04/05/03
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.jmx.mbeanserver;
  8. // java import
  9. import java.util.Iterator;
  10. import java.io.PrintWriter;
  11. import java.io.StringWriter;
  12. // RI import
  13. import javax.management.* ;
  14. import com.sun.jmx.trace.Trace;
  15. /**
  16. * The DynamicMetaDataImpl class provides local access to the metadata
  17. * service in an agent.
  18. * The DynamicMetaDataImpl only handles DynamicMBeans.
  19. *
  20. * @since 1.5
  21. * @since.unbundled JMX RI 1.2
  22. */
  23. public class DynamicMetaDataImpl extends BaseMetaDataImpl {
  24. /** The name of this class to be used for tracing */
  25. private final static String dbgTag = "DynamicMetaDataImpl";
  26. /**
  27. * Creates a Metadata Service.
  28. */
  29. public DynamicMetaDataImpl() {
  30. // ------------------------------
  31. // ------------------------------
  32. }
  33. /**
  34. * This methods tests if the MBean is JMX compliant
  35. */
  36. public void testCompliance(Class c)
  37. throws NotCompliantMBeanException {
  38. // ------------------------------
  39. // ------------------------------
  40. if (DynamicMBean.class.isAssignableFrom(c)) return;
  41. throw new NotCompliantMBeanException(
  42. "Only DynamicMBeans are supported by this implementation");
  43. }
  44. //---------------------------------------------------------------------
  45. //
  46. // From the MetaData interface
  47. //
  48. //---------------------------------------------------------------------
  49. public MBeanInfo getMBeanInfo(Object moi)
  50. throws IntrospectionException {
  51. try {
  52. return (MBeanInfo)
  53. ((javax.management.DynamicMBean)moi).getMBeanInfo();
  54. } catch (RuntimeMBeanException r) {
  55. throw r;
  56. } catch (RuntimeErrorException r) {
  57. throw r;
  58. } catch (RuntimeException r) {
  59. debugX("getMBeanInfo",r);
  60. throw new RuntimeMBeanException((RuntimeException)r,
  61. "Runtime Exception thrown by getMBeanInfo method of Dynamic MBean");
  62. } catch (Error e ) {
  63. debugX("getMBeanInfo",e);
  64. throw new RuntimeErrorException((Error)e,
  65. "Error thrown by getMBeanInfo method of Dynamic MBean");
  66. }
  67. }
  68. public Object getAttribute(Object instance, String attribute)
  69. throws MBeanException, AttributeNotFoundException,
  70. ReflectionException {
  71. if (attribute == null) {
  72. final RuntimeException r =
  73. new IllegalArgumentException("Attribute name cannot be null");
  74. throw new RuntimeOperationsException(r,
  75. "Exception occured trying to invoke the getter on the MBean");
  76. }
  77. try {
  78. return ((javax.management.DynamicMBean)instance).
  79. getAttribute(attribute);
  80. } catch (RuntimeOperationsException r) {
  81. throw r;
  82. } catch (RuntimeErrorException r) {
  83. throw r;
  84. } catch (RuntimeException e) {
  85. debugX("getAttribute",e);
  86. throw new RuntimeMBeanException(e, "RuntimeException" +
  87. " thrown by the getAttribute method of the DynamicMBean" +
  88. " for the attribute " + attribute);
  89. } catch (Error e) {
  90. debugX("getAttribute",e);
  91. throw new RuntimeErrorException((Error)e, "Error" +
  92. " thrown by the getAttribute method of the DynamicMBean "+
  93. " for the attribute " + attribute);
  94. }
  95. }
  96. public AttributeList getAttributes(Object instance, String[] attributes)
  97. throws ReflectionException {
  98. if (attributes == null) {
  99. throw new RuntimeOperationsException(new
  100. IllegalArgumentException("Attributes cannot be null"),
  101. "Exception occured trying to invoke the getter on the MBean");
  102. }
  103. try {
  104. return ((javax.management.DynamicMBean)instance).
  105. getAttributes(attributes);
  106. } catch (RuntimeOperationsException r) {
  107. throw r;
  108. } catch (RuntimeErrorException r) {
  109. throw r;
  110. } catch (RuntimeException e) {
  111. debugX("getAttributes",e);
  112. throw new RuntimeOperationsException(e, "RuntimeException" +
  113. " thrown by the getAttributes method of the DynamicMBean");
  114. } catch (Error e) {
  115. debugX("getAttributes",e);
  116. throw new RuntimeErrorException((Error)e, "Error" +
  117. " thrown by the getAttributes method of the DynamicMBean");
  118. }
  119. }
  120. public AttributeList setAttributes(Object instance,
  121. AttributeList attributes)
  122. throws ReflectionException {
  123. try {
  124. return ((javax.management.DynamicMBean)instance).
  125. setAttributes(attributes);
  126. } catch (RuntimeOperationsException r) {
  127. throw r;
  128. } catch (RuntimeErrorException r) {
  129. throw r;
  130. } catch (RuntimeException e) {
  131. debugX("setAttributes",e);
  132. throw new RuntimeOperationsException(e,
  133. "RuntimeException thrown by the setAttributes " +
  134. "method of the Dynamic MBean");
  135. } catch (Error e) {
  136. debugX("setAttributes",e);
  137. throw new RuntimeErrorException((Error)e,
  138. "Error thrown by the setAttributes " +
  139. "method of the Dynamic MBean");
  140. }
  141. }
  142. public Object setAttribute(Object instance, Attribute attribute)
  143. throws AttributeNotFoundException, InvalidAttributeValueException,
  144. MBeanException, ReflectionException {
  145. if (attribute == null) {
  146. final RuntimeException r =
  147. new IllegalArgumentException("Attribute name cannot be null");
  148. throw new RuntimeOperationsException(r,
  149. "Exception occured trying to invoke the setter on the MBean");
  150. }
  151. try {
  152. ((javax.management.DynamicMBean)instance).
  153. setAttribute(attribute);
  154. return attribute.getValue();
  155. } catch (RuntimeOperationsException r) {
  156. throw r;
  157. } catch (RuntimeErrorException r) {
  158. throw r;
  159. } catch (RuntimeException e) {
  160. debugX("setAttribute",e);
  161. throw new RuntimeMBeanException(e,
  162. "RuntimeException thrown by the setAttribute " +
  163. attribute + "method of the Dynamic MBean");
  164. } catch (Error e) {
  165. debugX("setAttribute",e);
  166. throw new RuntimeErrorException((Error)e,
  167. "Error thrown by the setAttribute " + attribute +
  168. "method of the Dynamic MBean");
  169. }
  170. }
  171. public Object invoke(Object instance, String operationName,
  172. Object params[], String signature[])
  173. throws MBeanException, ReflectionException {
  174. if (operationName == null) {
  175. final RuntimeException r =
  176. new IllegalArgumentException("Operation name cannot be null");
  177. throw new RuntimeOperationsException(r,
  178. "Exception occured trying to invoke the operation on the MBean");
  179. }
  180. try {
  181. return (((javax.management.DynamicMBean)instance).
  182. invoke(operationName, params, signature));
  183. } catch (ReflectionException e) {
  184. debugX("invoke",e);
  185. throw e;
  186. } catch (MBeanException e) {
  187. debugX("invoke",e);
  188. throw e;
  189. } catch (RuntimeOperationsException r) {
  190. throw r;
  191. } catch (RuntimeErrorException r) {
  192. throw r;
  193. } catch (RuntimeException e) {
  194. debugX("invoke",e);
  195. throw new RuntimeMBeanException(e, "RuntimeException" +
  196. " thrown by the invoke method of the Dynamic MBean");
  197. } catch (Error e) {
  198. debugX("invoke",e);
  199. throw new RuntimeErrorException((Error)e, "Error" +
  200. " thrown by the invoke method of the Dynamic MBean");
  201. }
  202. }
  203. // TRACES & DEBUG
  204. //---------------
  205. private static boolean isTraceOn() {
  206. return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER);
  207. }
  208. private static void trace(String clz, String func, String info) {
  209. Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info);
  210. }
  211. private static void trace(String func, String info) {
  212. trace(dbgTag, func, info);
  213. }
  214. private static boolean isDebugOn() {
  215. return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER);
  216. }
  217. private static void debug(String clz, String func, String info) {
  218. Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info);
  219. }
  220. private static void debug(String func, String info) {
  221. debug(dbgTag, func, info);
  222. }
  223. private static void debugX(String func,Throwable e) {
  224. if (isDebugOn()) {
  225. final StringWriter s = new StringWriter();
  226. e.printStackTrace(new PrintWriter(s));
  227. final String stack = s.toString();
  228. debug(dbgTag,func,"Exception caught in "+ func+"(): "+e);
  229. debug(dbgTag,func,stack);
  230. // java.lang.System.err.println("**** Exception caught in "+
  231. // func+"(): "+e);
  232. // java.lang.System.err.println(stack);
  233. }
  234. }
  235. }