1. /*
  2. * @(#)BaseMetaDataImpl.java 1.14 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.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. abstract class BaseMetaDataImpl implements MetaData {
  24. /** The name of this class to be used for tracing */
  25. private final static String dbgTag = "BaseMetaDataImpl";
  26. /**
  27. * Creates a Metadata Service.
  28. */
  29. BaseMetaDataImpl() {
  30. // ------------------------------
  31. // ------------------------------
  32. }
  33. //---------------------------------------------------------------------
  34. //
  35. // From the MetaData interface
  36. //
  37. //---------------------------------------------------------------------
  38. public abstract MBeanInfo getMBeanInfo(Object moi)
  39. throws IntrospectionException;
  40. public abstract Object getAttribute(Object instance, String attribute)
  41. throws MBeanException, AttributeNotFoundException,
  42. ReflectionException;
  43. public abstract AttributeList getAttributes(Object instance,
  44. String[] attributes)
  45. throws ReflectionException;
  46. public abstract AttributeList setAttributes(Object instance,
  47. AttributeList attributes)
  48. throws ReflectionException;
  49. public abstract Object setAttribute(Object instance, Attribute attribute)
  50. throws AttributeNotFoundException, InvalidAttributeValueException,
  51. MBeanException, ReflectionException;
  52. public abstract Object invoke(Object instance, String operationName,
  53. Object params[], String signature[])
  54. throws MBeanException, ReflectionException;
  55. public ObjectName preRegisterInvoker(Object moi, ObjectName name,
  56. MBeanServer mbs)
  57. throws InstanceAlreadyExistsException, MBeanRegistrationException {
  58. if (!(moi instanceof MBeanRegistration)) return name;
  59. final ObjectName newName;
  60. try {
  61. newName=(ObjectName)
  62. ((MBeanRegistration)moi).preRegister(mbs, name);
  63. } catch (RuntimeException e) {
  64. throw new RuntimeMBeanException((RuntimeException)e,
  65. "RuntimeException thrown in preRegister method");
  66. } catch (Error er) {
  67. throw new RuntimeErrorException((Error) er,
  68. "Error thrown in preRegister method");
  69. } catch (MBeanRegistrationException r) {
  70. throw (MBeanRegistrationException)r;
  71. } catch (Exception ex) {
  72. throw new MBeanRegistrationException((Exception) ex,
  73. "Exception thrown in preRegister method");
  74. }
  75. if (newName != null) return newName;
  76. else return name;
  77. }
  78. public void postRegisterInvoker(Object moi, boolean registrationDone) {
  79. if (!(moi instanceof MBeanRegistration)) return;
  80. try {
  81. ((MBeanRegistration)moi).
  82. postRegister(new Boolean(registrationDone));
  83. } catch (RuntimeException e) {
  84. throw new RuntimeMBeanException((RuntimeException)e,
  85. "RuntimeException thrown in postRegister method");
  86. } catch (Error er) {
  87. throw new RuntimeErrorException((Error) er,
  88. "Error thrown in postRegister method");
  89. }
  90. }
  91. public void preDeregisterInvoker(Object moi)
  92. throws MBeanRegistrationException {
  93. if (!(moi instanceof MBeanRegistration)) return;
  94. try {
  95. ((MBeanRegistration)moi).preDeregister();
  96. } catch (RuntimeException e) {
  97. throw new RuntimeMBeanException((RuntimeException) e,
  98. "RuntimeException thrown in preDeregister method");
  99. } catch (Error er) {
  100. throw new RuntimeErrorException((Error) er,
  101. "Error thrown in preDeregister method");
  102. } catch (MBeanRegistrationException t) {
  103. throw (MBeanRegistrationException)t;
  104. } catch (Exception ex) {
  105. throw new MBeanRegistrationException((Exception)ex,
  106. "Exception thrown in preDeregister method");
  107. }
  108. }
  109. public void postDeregisterInvoker(Object moi) {
  110. if (!(moi instanceof MBeanRegistration)) return;
  111. try {
  112. ((MBeanRegistration)moi).postDeregister();
  113. } catch (RuntimeException e) {
  114. throw new RuntimeMBeanException((RuntimeException)e,
  115. "RuntimeException thrown in postDeregister method");
  116. } catch (Error er) {
  117. throw new RuntimeErrorException((Error) er,
  118. "Error thrown in postDeregister method");
  119. }
  120. }
  121. public String getMBeanClassName(Object moi)
  122. throws IntrospectionException, NotCompliantMBeanException {
  123. final MBeanInfo mbi;
  124. try {
  125. // Ask the MBeanInfo for the class name
  126. mbi = getMBeanInfo(moi);
  127. } catch (SecurityException x) {
  128. throw x;
  129. } catch (IntrospectionException x) {
  130. throw x;
  131. } catch (Exception x) {
  132. throw new NotCompliantMBeanException("Can't obtain MBeanInfo " +
  133. "from DynamicMBean: " + x);
  134. }
  135. if (mbi == null) {
  136. throw new
  137. NotCompliantMBeanException("The MBeanInfo returned is null");
  138. }
  139. final String className = mbi.getClassName();
  140. if (className == null) {
  141. throw new
  142. IntrospectionException("The class Name returned is null");
  143. }
  144. return className;
  145. }
  146. public boolean isInstanceOf(Object instance, String className)
  147. throws ReflectionException {
  148. try {
  149. final String cn = getMBeanClassName(instance);
  150. if (cn.equals(className))
  151. return true;
  152. try {
  153. final ClassLoader cl = instance.getClass().getClassLoader();
  154. final Class classNameClass = findClass(className,cl);
  155. if (classNameClass == null) return false;
  156. if (classNameClass.isInstance(instance)) return true;
  157. final Class instanceClass = findClass(cn,cl);
  158. if (instanceClass == null) return false;
  159. return classNameClass.isAssignableFrom(instanceClass);
  160. } catch (Exception x) {
  161. /* Could be SecurityException or ClassNotFoundException */
  162. debugX("isInstanceOf",x);
  163. return false;
  164. }
  165. } catch (IntrospectionException x) {
  166. debugX("isInstanceOf",x);
  167. throw new ReflectionException(x,x.getMessage());
  168. } catch (NotCompliantMBeanException x) {
  169. debugX("isInstanceOf",x);
  170. throw new ReflectionException(x,x.getMessage());
  171. }
  172. }
  173. protected Class findClass(String className, ClassLoader loader)
  174. throws ReflectionException {
  175. try {
  176. if (loader == null)
  177. return Class.forName(className);
  178. else
  179. return loader.loadClass(className);
  180. } catch (ClassNotFoundException x) {
  181. throw new ReflectionException(x,x.getMessage());
  182. }
  183. }
  184. // TRACES & DEBUG
  185. //---------------
  186. private static boolean isTraceOn() {
  187. return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER);
  188. }
  189. private static void trace(String clz, String func, String info) {
  190. Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info);
  191. }
  192. private static void trace(String func, String info) {
  193. trace(dbgTag, func, info);
  194. }
  195. private static boolean isDebugOn() {
  196. return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER);
  197. }
  198. private static void debug(String clz, String func, String info) {
  199. Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info);
  200. }
  201. private static void debug(String func, String info) {
  202. debug(dbgTag, func, info);
  203. }
  204. private static void debugX(String func,Throwable e) {
  205. if (isDebugOn()) {
  206. final StringWriter s = new StringWriter();
  207. e.printStackTrace(new PrintWriter(s));
  208. final String stack = s.toString();
  209. debug(dbgTag,func,"Exception caught in "+ func+"(): "+e);
  210. debug(dbgTag,func,stack);
  211. // java.lang.System.err.println("**** Exception caught in "+
  212. // func+"(): "+e);
  213. // java.lang.System.err.println(stack);
  214. }
  215. }
  216. }