1. /*
  2. * @(#)MetaDataImpl.java 1.28 03/12/19
  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.lang.reflect.Method;
  10. import java.lang.reflect.Constructor;
  11. import java.lang.reflect.InvocationTargetException;
  12. import java.util.Hashtable;
  13. import java.util.Iterator;
  14. import java.io.PrintWriter;
  15. import java.io.StringWriter;
  16. // RI import
  17. import javax.management.* ;
  18. import com.sun.jmx.trace.Trace;
  19. /**
  20. * The MetaData class provides local access to the metadata service in
  21. * an agent.
  22. *
  23. * @since 1.5
  24. * @since.unbundled JMX RI 1.2
  25. */
  26. public class MetaDataImpl implements MetaData {
  27. /** The name of this class to be used for tracing */
  28. private final static String dbgTag = "MetaDataImpl";
  29. /** MetaData for DynamicMBeans **/
  30. private final DynamicMetaDataImpl dynamic;
  31. private final StandardMetaDataImpl standard;
  32. /**
  33. * The MBeanInstantiator associated to the MetaData
  34. */
  35. protected final MBeanInstantiator instantiator;
  36. // Not sure we need this...
  37. private final class PrivateStandardMeta extends StandardMetaDataImpl {
  38. PrivateStandardMeta() {
  39. super();
  40. }
  41. // public synchronized void testCompliance(Class c) {
  42. // MetaDataImpl.this.testStrictCompliance(c);
  43. // }
  44. protected Class findClass(String className, ClassLoader loader)
  45. throws ReflectionException {
  46. return MetaDataImpl.this.findClass(className,loader);
  47. }
  48. protected Class[] findSignatureClasses(String[] signature,
  49. ClassLoader loader)
  50. throws ReflectionException {
  51. return MetaDataImpl.this.findSignatureClasses(signature,loader);
  52. }
  53. }
  54. // Not sure we need this...
  55. private final class PrivateDynamicMeta extends DynamicMetaDataImpl {
  56. PrivateDynamicMeta() {
  57. super();
  58. }
  59. // public synchronized void testCompliance(Class c) {
  60. // MetaDataImpl.this.testStrictCompliance(c);
  61. // }
  62. protected Class findClass(String className, ClassLoader loader)
  63. throws ReflectionException {
  64. return MetaDataImpl.this.findClass(className,loader);
  65. }
  66. protected Class[] findSignatureClasses(String[] signature,
  67. ClassLoader loader)
  68. throws ReflectionException {
  69. return MetaDataImpl.this.findSignatureClasses(signature,loader);
  70. }
  71. }
  72. /**
  73. * Creates a Metadata Service.
  74. * @param instantiator The MBeanInstantiator that will be used to
  75. * take care of class loading issues.
  76. * This parameter may not be null.
  77. * @exception IllegalArgumentException if the instantiator is null.
  78. */
  79. public MetaDataImpl(MBeanInstantiator instantiator) {
  80. if (instantiator == null) throw new
  81. IllegalArgumentException("instantiator must not be null.");
  82. this.instantiator = instantiator;
  83. this.dynamic = new PrivateDynamicMeta();
  84. this.standard = new PrivateStandardMeta();
  85. // ------------------------------
  86. // ------------------------------
  87. }
  88. protected MetaData getMetaData(Class c) {
  89. if (DynamicMBean.class.isAssignableFrom(c))
  90. return dynamic;
  91. else
  92. return standard;
  93. }
  94. protected MetaData getMetaData(Object moi) {
  95. if (moi instanceof DynamicMBean)
  96. return dynamic;
  97. else
  98. return standard;
  99. }
  100. /**
  101. * This methods tests if the MBean is JMX compliant
  102. */
  103. public synchronized void testCompliance(Class c)
  104. throws NotCompliantMBeanException {
  105. final MetaData meta = getMetaData(c);
  106. meta.testCompliance(c);
  107. }
  108. /**
  109. * This methods returns the MBean interface of an MBean
  110. */
  111. public Class getMBeanInterfaceFromClass(Class c) {
  112. return standard.getMBeanInterfaceFromClass(c);
  113. }
  114. /**
  115. * This method discovers the attributes and operations that an MBean
  116. * exposes for management.
  117. *
  118. * @param beanClass The class to be analyzed.
  119. *
  120. * @return An instance of MBeanInfo allowing to retrieve all methods
  121. * and operations of this class.
  122. *
  123. * @exception IntrospectionException if an exception occurs during
  124. * introspection.
  125. * @exception NotCompliantMBeanException if the MBean class is not
  126. * MBean compliant.
  127. *
  128. */
  129. public MBeanInfo getMBeanInfoFromClass(Class beanClass)
  130. throws IntrospectionException, NotCompliantMBeanException {
  131. return standard.getMBeanInfoFromClass(beanClass);
  132. }
  133. //---------------------------------------------------------------------
  134. //
  135. // From the MetaData interface
  136. //
  137. //---------------------------------------------------------------------
  138. public final String getMBeanClassName(Object moi)
  139. throws IntrospectionException, NotCompliantMBeanException {
  140. final MetaData meta = getMetaData(moi);
  141. return meta.getMBeanClassName(moi);
  142. }
  143. public final MBeanInfo getMBeanInfo(Object moi)
  144. throws IntrospectionException {
  145. final MetaData meta = getMetaData(moi);
  146. return meta.getMBeanInfo(moi);
  147. }
  148. public final Object getAttribute(Object instance, String attribute)
  149. throws MBeanException, AttributeNotFoundException,
  150. ReflectionException {
  151. final MetaData meta = getMetaData(instance);
  152. return meta.getAttribute(instance,attribute);
  153. }
  154. public final AttributeList getAttributes(Object instance,
  155. String[] attributes)
  156. throws ReflectionException {
  157. final MetaData meta = getMetaData(instance);
  158. return meta.getAttributes(instance, attributes);
  159. }
  160. public final AttributeList setAttributes(Object instance,
  161. AttributeList attributes)
  162. throws ReflectionException {
  163. final MetaData meta = getMetaData(instance);
  164. return meta.setAttributes(instance,attributes);
  165. }
  166. public final Object setAttribute(Object instance, Attribute attribute)
  167. throws AttributeNotFoundException, InvalidAttributeValueException,
  168. MBeanException, ReflectionException {
  169. final MetaData meta = getMetaData(instance);
  170. return meta.setAttribute(instance,attribute);
  171. }
  172. public final Object invoke(Object instance, String operationName,
  173. Object params[], String signature[])
  174. throws MBeanException, ReflectionException {
  175. if (operationName == null) {
  176. final RuntimeException r =
  177. new IllegalArgumentException("Operation name cannot be null");
  178. throw new RuntimeOperationsException(r,
  179. "Exception occured trying to invoke the operation on the MBean");
  180. }
  181. final MetaData meta = getMetaData(instance);
  182. return meta.invoke(instance,operationName,params,signature);
  183. }
  184. public boolean isInstanceOf(Object instance, String className)
  185. throws ReflectionException {
  186. // XXX revisit here: ModelMBean ???
  187. final MetaData meta = getMetaData(instance);
  188. return meta.isInstanceOf(instance,className);
  189. }
  190. public ObjectName preRegisterInvoker(Object moi, ObjectName name,
  191. MBeanServer mbs)
  192. throws InstanceAlreadyExistsException, MBeanRegistrationException {
  193. if (!(moi instanceof MBeanRegistration)) return name;
  194. final MetaData meta = getMetaData(moi);
  195. return meta.preRegisterInvoker(moi,name,mbs);
  196. }
  197. public void postRegisterInvoker(Object moi, boolean registrationDone) {
  198. if (!(moi instanceof MBeanRegistration)) return;
  199. final MetaData meta = getMetaData(moi);
  200. meta.postRegisterInvoker(moi,registrationDone);
  201. }
  202. public void preDeregisterInvoker(Object moi)
  203. throws MBeanRegistrationException {
  204. if (!(moi instanceof MBeanRegistration)) return;
  205. final MetaData meta = getMetaData(moi);
  206. meta.preDeregisterInvoker(moi);
  207. }
  208. public void postDeregisterInvoker(Object moi) {
  209. if (!(moi instanceof MBeanRegistration)) return;
  210. final MetaData meta = getMetaData(moi);
  211. meta.postDeregisterInvoker(moi);
  212. }
  213. /**
  214. * Find a class using the specified ClassLoader.
  215. **/
  216. protected Class findClass(String className, ClassLoader loader)
  217. throws ReflectionException {
  218. return instantiator.findClass(className, loader);
  219. }
  220. /**
  221. * Find the classes from a signature using the specified ClassLoader.
  222. **/
  223. protected Class[] findSignatureClasses(String[] signature,
  224. ClassLoader loader)
  225. throws ReflectionException {
  226. return ((signature == null)?null:
  227. instantiator.findSignatureClasses(signature,loader));
  228. }
  229. // TRACES & DEBUG
  230. //---------------
  231. private static boolean isTraceOn() {
  232. return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER);
  233. }
  234. private static void trace(String clz, String func, String info) {
  235. Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info);
  236. }
  237. private static void trace(String func, String info) {
  238. trace(dbgTag, func, info);
  239. }
  240. private static boolean isDebugOn() {
  241. return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER);
  242. }
  243. private static void debug(String clz, String func, String info) {
  244. Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info);
  245. }
  246. private static void debug(String func, String info) {
  247. debug(dbgTag, func, info);
  248. }
  249. private static void debugX(String func,Throwable e) {
  250. if (isDebugOn()) {
  251. final StringWriter s = new StringWriter();
  252. e.printStackTrace(new PrintWriter(s));
  253. final String stack = s.toString();
  254. debug(dbgTag,func,"Exception caught in "+ func+"(): "+e);
  255. debug(dbgTag,func,stack);
  256. // java.lang.System.err.println("**** Exception caught in "+
  257. // func+"(): "+e);
  258. // java.lang.System.err.println(stack);
  259. }
  260. }
  261. }