📄 standardmetadataimpl.java
字号:
/* * @(#)StandardMetaDataImpl.java 1.24 05/05/27 * * Copyright 2005 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.jmx.mbeanserver;// java importimport java.lang.reflect.Method;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import java.lang.ref.WeakReference;import java.security.AccessController;import java.security.PrivilegedAction;import java.util.Hashtable;import java.util.Iterator;import java.io.PrintWriter;import java.io.StringWriter;// RI importimport javax.management.* ; import com.sun.jmx.trace.Trace; import com.sun.jmx.mbeanserver.GetPropertyAction;/** * The MetaData class provides local access to the metadata service in * an agent. * * @since 1.5 * @since.unbundled JMX RI 1.2 */class StandardMetaDataImpl extends BaseMetaDataImpl { /** The name of this class to be used for tracing */ private final static String dbgTag = "StandardMetaDataImpl"; /** * Cache of MBeanInfo objects. */ private static java.util.Map mbeanInfoCache = new java.util.WeakHashMap(); /** * Cache of MBean Interface objects. */ private static java.util.Map mbeanInterfaceCache = new java.util.WeakHashMap(); /** * True if RuntimeExceptions from getters, setters, and operations * should be wrapped in RuntimeMBeanException. We do not have * similar logic for Errors because DynamicMetaDataImpl does not * re-wrap RuntimeErrorException as it would * RuntimeMBeanException. */ private final boolean wrapRuntimeExceptions; /** * objects maps from primitive classes to primitive object classes. */ // private static Hashtable primitiveobjects = new Hashtable(); // { // primitiveobjects.put(Boolean.TYPE, getClass("java.lang.Boolean")); // primitiveobjects.put(Character.TYPE, getClass("java.lang.Character")); // primitiveobjects.put(Byte.TYPE, getClass("java.lang.Byte")); // primitiveobjects.put(Short.TYPE, getClass("java.lang.Short")); // primitiveobjects.put(Integer.TYPE, getClass("java.lang.Integer")); // primitiveobjects.put(Long.TYPE, getClass("java.lang.Long")); // primitiveobjects.put(Float.TYPE, getClass("java.lang.Float")); // primitiveobjects.put(Double.TYPE, getClass("java.lang.Double")); // } private final static Hashtable primitiveClasses = new Hashtable(8); { primitiveClasses.put(Boolean.TYPE.toString(), Boolean.TYPE); primitiveClasses.put(Character.TYPE.toString(), Character.TYPE); primitiveClasses.put(Byte.TYPE.toString(), Byte.TYPE); primitiveClasses.put(Short.TYPE.toString(), Short.TYPE); primitiveClasses.put(Integer.TYPE.toString(), Integer.TYPE); primitiveClasses.put(Long.TYPE.toString(), Long.TYPE); primitiveClasses.put(Float.TYPE.toString(), Float.TYPE); primitiveClasses.put(Double.TYPE.toString(), Double.TYPE); } /** * Creates a Metadata Service. */ public StandardMetaDataImpl() { this(true); } StandardMetaDataImpl(boolean wrapRuntimeExceptions) { this.wrapRuntimeExceptions = wrapRuntimeExceptions; } /** * Builds the MBeanInfo from the given concrete MBean class. * @param c The concrete MBean class from which the MBeanInfo * must be built. * * @exception NotCompliantMBeanException if the given class * is not MBean compliant. * @return the MBeanInfo built from class <var>c</var>, or null * if class <var>c</var> implements * {@link javax.management.DynamicMBean} */ public synchronized MBeanInfo buildMBeanInfo(Class c) throws NotCompliantMBeanException { return Introspector.testCompliance(c); } /** * Builds the MBeanInfo from the given concrete MBean class, * using the given <var>mbeanInterface</var> as Management Interface. * * @param c The concrete MBean class from which the MBeanInfo * must be built. * @param mbeanInterface The management interface of the MBean. * If <code>null</code>, will use the regular design pattern * to determine the management interface. * @exception NotCompliantMBeanException if the given class and interface * are not MBean compliant. Does not enforce that if class <var>c</var> * is "X", then interface <var>mbeanInterface</var> is "XMBean". * @return the MBeanInfo built from class <var>c</var>, according * to interface <var>mbeanInterface</var>. Does not check whether * class <var>c</var> implements {@link javax.management.DynamicMBean}. **/ public synchronized MBeanInfo buildMBeanInfo(Class c, Class mbeanInterface) throws NotCompliantMBeanException { return Introspector.testCompliance(c,mbeanInterface); } /** * This methods tests if the MBean is JMX compliant */ public synchronized void testCompliance(Class c) throws NotCompliantMBeanException { // ------------------------------ // ------------------------------ final MBeanInfo mbeanInfo = buildMBeanInfo(c); final Class mbeanInterface = Introspector.getMBeanInterface(c); cacheMBeanInfo(c,mbeanInterface,mbeanInfo); } /** * This methods tests if the MBean is JMX compliant. * <li>It does not enforce that if c="X", mbeanInterface="XMBean".</li> * <li>It does not check whether c is a DynamicMBean</li> */ public synchronized void testCompliance(Class c, Class mbeanInterface) throws NotCompliantMBeanException { // ------------------------------ // ------------------------------ final MBeanInfo mbeanInfo = buildMBeanInfo(c,mbeanInterface); if (mbeanInterface == null) mbeanInterface = Introspector.getStandardMBeanInterface(c); cacheMBeanInfo(c,mbeanInterface,mbeanInfo); } /** * This methods returns the MBean interface of an MBean */ public Class getMBeanInterfaceFromClass(Class c) { final Class itf = getCachedMBeanInterface(c); if (itf != null) return itf; synchronized (this) { return Introspector.getMBeanInterface(c); } } /** * This methods analizes the passed MBean class and * returns the default MBean interface according to JMX patterns. */ public Class getStandardMBeanInterface(Class c) { synchronized (this) { return Introspector.getStandardMBeanInterface(c); } } /** * This method discovers the attributes and operations that an MBean * exposes for management. * * @param beanClass The class to be analyzed. * * @return An instance of MBeanInfo allowing to retrieve all methods * and operations of this class. * * @exception IntrospectionException if an exception occurs during * introspection. * @exception NotCompliantMBeanException if the MBean class is not * MBean compliant. * */ public MBeanInfo getMBeanInfoFromClass(Class beanClass) throws IntrospectionException, NotCompliantMBeanException { // Check the mbean information cache. MBeanInfo bi = getCachedMBeanInfo(beanClass); // Make an independent copy of the MBeanInfo. if (bi != null) return (MBeanInfo) bi.clone() ; // We don't have have any MBeanInfo for that class yet. // => test compliance. testCompliance(beanClass); bi = getCachedMBeanInfo(beanClass);; // Make an independent copy of the MBeanInfo. if (bi != null) return (MBeanInfo) bi.clone() ; return bi; } //--------------------------------------------------------------------- // // From the MetaData interface // //--------------------------------------------------------------------- public String getMBeanClassName(Object moi) throws IntrospectionException, NotCompliantMBeanException { return moi.getClass().getName(); } public MBeanInfo getMBeanInfo(Object moi) throws IntrospectionException { try { final MBeanInfo mbi = getMBeanInfoFromClass(moi.getClass()); return new MBeanInfo(mbi.getClassName(), mbi.getDescription(), mbi.getAttributes(), mbi.getConstructors(), mbi.getOperations(), findNotifications(moi)); } catch (NotCompliantMBeanException x) { debugX("getMBeanInfo",x); throw new IntrospectionException("Can't build MBeanInfo for "+ moi.getClass().getName()); } } public Object getAttribute(Object instance, String attribute) throws MBeanException, AttributeNotFoundException, ReflectionException { Class mbeanClass = getMBeanInterfaceFromInstance(instance); if (isDebugOn()) { debug("getAttribute","MBean Class is " + instance.getClass()); debug("getAttribute","MBean Interface is " + mbeanClass); } return getAttribute(instance, attribute, mbeanClass); } public AttributeList getAttributes(Object instance, String[] attributes) throws ReflectionException { final Class mbeanClass = getMBeanInterfaceFromInstance(instance); if (isDebugOn()) { debug("getAttributes","MBean Class is " + instance.getClass()); debug("getAttributes","MBean Interface is " + mbeanClass); } if (attributes == null) { throw new RuntimeOperationsException(new IllegalArgumentException("Attributes cannot be null"), "Exception occured trying to invoke the getter on the MBean"); } // Go through the list of attributes // final int maxLimit = attributes.length; final AttributeList result = new AttributeList(maxLimit); for (int i=0;i<maxLimit;i++) { final String elmt = (String)attributes[i]; try { final Object value = getAttribute(instance, elmt, mbeanClass); result.add(new Attribute(elmt, value)); } catch (Exception excep) { if (isDebugOn()) { debug("getAttributes", "Object= " + instance + ", Attribute=" + elmt + " failed: " + excep); } } } return result; } public AttributeList setAttributes(Object instance, AttributeList attributes) throws ReflectionException { final Class objClass = instance.getClass(); final Class mbeanClass = getMBeanInterfaceFromInstance(instance); final ClassLoader aLoader = objClass.getClassLoader(); if (isDebugOn()) { debug("setAttributes","MBean Class is " + instance.getClass()); debug("setAttributes","MBean Interface is " + mbeanClass); } if (attributes == null) return new AttributeList(); final AttributeList result = new AttributeList(attributes.size()); // Go through the list of attributes for (final Iterator i = attributes.iterator(); i.hasNext();) { final Attribute attr = (Attribute) i.next(); final String id = attr.getName(); final Object value = attr.getValue(); try { final Object newValue = setAttribute(instance, attr, mbeanClass); if (isTraceOn()) { trace("setAttributes", "Updating the list\n"); } result.add(new Attribute(id, newValue)); } catch (Exception excep) { if (isDebugOn()) { debug("setAttributes", "Unexpected exception occured: " + excep.getClass().getName()); } } } return result; } public Object setAttribute(Object instance, Attribute attribute)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -