1. /*
  2. * @(#)MBeanServerDelegateImpl.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. import javax.management.ObjectName;
  9. import javax.management.MBeanServer;
  10. import javax.management.MBeanRegistration;
  11. import javax.management.DynamicMBean;
  12. import javax.management.AttributeNotFoundException;
  13. import javax.management.MBeanException;
  14. import javax.management.ReflectionException;
  15. import javax.management.MBeanAttributeInfo;
  16. import javax.management.MBeanInfo;
  17. import javax.management.MBeanNotificationInfo;
  18. import javax.management.JMRuntimeException;
  19. import javax.management.InvalidAttributeValueException;
  20. import javax.management.Attribute;
  21. import javax.management.AttributeList;
  22. import javax.management.RuntimeOperationsException;
  23. import com.sun.jmx.defaults.ServiceName;
  24. import com.sun.jmx.trace.Trace;
  25. /**
  26. * This class is the MBean implementation of the MBeanServerDelegate.
  27. *
  28. * @since 1.5
  29. */
  30. final class MBeanServerDelegateImpl
  31. extends javax.management.MBeanServerDelegate
  32. implements DynamicMBean, MBeanRegistration {
  33. /** The name of this class to be used for tracing */
  34. private final static String dbgTag = "MBeanServerDelegateImpl";
  35. final private static String[] attributeNames = new String[] {
  36. "MBeanServerId",
  37. "SpecificationName",
  38. "SpecificationVersion",
  39. "SpecificationVendor",
  40. "ImplementationName",
  41. "ImplementationVersion",
  42. "ImplementationVendor"
  43. };
  44. private static final MBeanAttributeInfo[] attributeInfos =
  45. new MBeanAttributeInfo[] {
  46. new MBeanAttributeInfo("MBeanServerId","java.lang.String",
  47. "The MBean server agent identification",
  48. true,false,false),
  49. new MBeanAttributeInfo("SpecificationName","java.lang.String",
  50. "The full name of the JMX specification "+
  51. "implemented by this product.",
  52. true,false,false),
  53. new MBeanAttributeInfo("SpecificationVersion","java.lang.String",
  54. "The version of the JMX specification "+
  55. "implemented by this product.",
  56. true,false,false),
  57. new MBeanAttributeInfo("SpecificationVendor","java.lang.String",
  58. "The vendor of the JMX specification "+
  59. "implemented by this product.",
  60. true,false,false),
  61. new MBeanAttributeInfo("ImplementationName","java.lang.String",
  62. "The JMX implementation name "+
  63. "(the name of this product)",
  64. true,false,false),
  65. new MBeanAttributeInfo("ImplementationVersion","java.lang.String",
  66. "The JMX implementation version "+
  67. "(the version of this product).",
  68. true,false,false),
  69. new MBeanAttributeInfo("ImplementationVendor","java.lang.String",
  70. "the JMX implementation vendor "+
  71. "(the vendor of this product).",
  72. true,false,false)
  73. };
  74. private final MBeanInfo delegateInfo;
  75. public MBeanServerDelegateImpl () {
  76. super();
  77. delegateInfo =
  78. new MBeanInfo("javax.management.MBeanServerDelegate",
  79. "Represents the MBean server from the management "+
  80. "point of view.",
  81. MBeanServerDelegateImpl.attributeInfos, null,
  82. null,getNotificationInfo());
  83. }
  84. final public ObjectName preRegister (MBeanServer server, ObjectName name)
  85. throws java.lang.Exception {
  86. if (name == null) return new ObjectName(ServiceName.DELEGATE);
  87. else return name;
  88. }
  89. final public void postRegister (Boolean registrationDone) {
  90. }
  91. final public void preDeregister()
  92. throws java.lang.Exception {
  93. throw new IllegalArgumentException(
  94. "The MBeanServerDelegate MBean cannot be unregistered");
  95. }
  96. final public void postDeregister() {
  97. }
  98. /**
  99. * Obtains the value of a specific attribute of the MBeanServerDelegate.
  100. *
  101. * @param attribute The name of the attribute to be retrieved
  102. *
  103. * @return The value of the attribute retrieved.
  104. *
  105. * @exception AttributeNotFoundException
  106. * @exception MBeanException
  107. * Wraps a <CODE>java.lang.Exception</CODE> thrown by the
  108. * MBean's getter.
  109. */
  110. public Object getAttribute(String attribute)
  111. throws AttributeNotFoundException,
  112. MBeanException, ReflectionException {
  113. try {
  114. // attribute must not be null
  115. //
  116. if (attribute == null)
  117. throw new AttributeNotFoundException("null");
  118. // Extract the requested attribute from file
  119. //
  120. if (attribute.equals("MBeanServerId"))
  121. return getMBeanServerId();
  122. else if (attribute.equals("SpecificationName"))
  123. return getSpecificationName();
  124. else if (attribute.equals("SpecificationVersion"))
  125. return getSpecificationVersion();
  126. else if (attribute.equals("SpecificationVendor"))
  127. return getSpecificationVendor();
  128. else if (attribute.equals("ImplementationName"))
  129. return getImplementationName();
  130. else if (attribute.equals("ImplementationVersion"))
  131. return getImplementationVersion();
  132. else if (attribute.equals("ImplementationVendor"))
  133. return getImplementationVendor();
  134. // Unknown attribute
  135. //
  136. else
  137. throw new AttributeNotFoundException("null");
  138. } catch (AttributeNotFoundException x) {
  139. throw x;
  140. } catch (JMRuntimeException j) {
  141. throw j;
  142. } catch (SecurityException s) {
  143. throw s;
  144. } catch (Exception x) {
  145. throw new MBeanException(x,"Failed to get " + attribute);
  146. }
  147. }
  148. /**
  149. * This method always fail since all MBeanServerDelegateMBean attributes
  150. * are read-only.
  151. *
  152. * @param attribute The identification of the attribute to
  153. * be set and the value it is to be set to.
  154. *
  155. * @exception AttributeNotFoundException
  156. */
  157. public void setAttribute(Attribute attribute)
  158. throws AttributeNotFoundException, InvalidAttributeValueException,
  159. MBeanException, ReflectionException {
  160. // Now we will always fail:
  161. // Either because the attribute is null or because it is not
  162. // accessible (or does not exist).
  163. //
  164. final String attname = (attribute==null?null:attribute.getName());
  165. if (attname == null) {
  166. final RuntimeException r =
  167. new IllegalArgumentException("Attribute name cannot be null");
  168. throw new RuntimeOperationsException(r,
  169. "Exception occured trying to invoke the setter on the MBean");
  170. }
  171. // This is a hack: we call getAttribute in order to generate an
  172. // AttributeNotFoundException if the attribute does not exist.
  173. //
  174. Object val = getAttribute(attname);
  175. // If we reach this point, we know that the requested attribute
  176. // exists. However, since all attributes are read-only, we throw
  177. // an AttributeNotFoundException.
  178. //
  179. throw new AttributeNotFoundException(attname + " not accessible");
  180. }
  181. /**
  182. * Makes it possible to get the values of several attributes of
  183. * the MBeanServerDelegate.
  184. *
  185. * @param attributes A list of the attributes to be retrieved.
  186. *
  187. * @return The list of attributes retrieved.
  188. *
  189. */
  190. public AttributeList getAttributes(String[] attributes) {
  191. // If attributes is null, the get all attributes.
  192. //
  193. final String[] attn = (attributes==null?attributeNames:attributes);
  194. // Prepare the result list.
  195. //
  196. final int len = attn.length;
  197. final AttributeList list = new AttributeList(len);
  198. // Get each requested attribute.
  199. //
  200. for (int i=0;i<len;i++) {
  201. try {
  202. final Attribute a =
  203. new Attribute(attn[i],getAttribute(attn[i]));
  204. list.add(a);
  205. } catch (Exception x) {
  206. // Skip the attribute that couldn't be obtained.
  207. //
  208. debug("getAttributes","Attribute " + attn[i] +
  209. " not found.");
  210. }
  211. }
  212. // Finally return the result.
  213. //
  214. return list;
  215. }
  216. /**
  217. * This method always return an empty list since all
  218. * MBeanServerDelegateMBean attributes are read-only.
  219. *
  220. * @param attributes A list of attributes: The identification of the
  221. * attributes to be set and the values they are to be set to.
  222. *
  223. * @return The list of attributes that were set, with their new values.
  224. * In fact, this method always return an empty list since all
  225. * MBeanServerDelegateMBean attributes are read-only.
  226. */
  227. public AttributeList setAttributes(AttributeList attributes) {
  228. return new AttributeList(0);
  229. }
  230. /**
  231. * Always fails since the MBeanServerDelegate MBean has no operation.
  232. *
  233. * @param actionName The name of the action to be invoked.
  234. * @param params An array containing the parameters to be set when the
  235. * action is invoked.
  236. * @param signature An array containing the signature of the action.
  237. *
  238. * @return The object returned by the action, which represents
  239. * the result of invoking the action on the MBean specified.
  240. *
  241. * @exception MBeanException Wraps a <CODE>java.lang.Exception</CODE>
  242. * thrown by the MBean's invoked method.
  243. * @exception ReflectionException Wraps a
  244. * <CODE>java.lang.Exception</CODE> thrown while trying to invoke
  245. * the method.
  246. */
  247. public Object invoke(String actionName, Object params[],
  248. String signature[])
  249. throws MBeanException, ReflectionException {
  250. // Check that operation name is not null.
  251. //
  252. if (actionName == null) {
  253. final RuntimeException r =
  254. new IllegalArgumentException("Operation name cannot be null");
  255. throw new RuntimeOperationsException(r,
  256. "Exception occured trying to invoke the operation on the MBean");
  257. }
  258. throw new ReflectionException(
  259. new NoSuchMethodException(actionName),
  260. "The operation with name " + actionName +
  261. " could not be found");
  262. }
  263. /**
  264. * Provides the MBeanInfo describing the MBeanServerDelegate.
  265. *
  266. * @return The MBeanInfo describing the MBeanServerDelegate.
  267. *
  268. */
  269. public MBeanInfo getMBeanInfo() {
  270. return delegateInfo;
  271. }
  272. // TRACES & DEBUG
  273. //---------------
  274. private final static boolean isTraceOn() {
  275. return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER);
  276. }
  277. private final static void trace(String clz, String func, String info) {
  278. Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info);
  279. }
  280. private final static void trace(String func, String info) {
  281. trace(dbgTag, func, info);
  282. }
  283. private final static boolean isDebugOn() {
  284. return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER);
  285. }
  286. private final static void debug(String clz, String func, String info) {
  287. Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info);
  288. }
  289. private final static void debug(String func, String info) {
  290. debug(dbgTag, func, info);
  291. }
  292. }