standardmetadataimpl.java
来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,043 行 · 第 1/3 页
JAVA
1,043 行
throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { final Class mbeanClass = getMBeanInterfaceFromInstance(instance); if (isDebugOn()) { debug("setAttribute","MBean Class is " + instance.getClass()); debug("setAttribute","MBean Interface is " + mbeanClass); } return setAttribute(instance,attribute,mbeanClass); } public Object invoke(Object instance, String operationName, Object params[], String signature[]) throws MBeanException, ReflectionException { if (operationName == null) { final RuntimeException r = new IllegalArgumentException("Operation name cannot be null"); throw new RuntimeOperationsException(r, "Exception occured trying to invoke the operation on the MBean"); } final Class objClass = instance.getClass(); final Class mbeanClass = getMBeanInterfaceFromInstance(instance); final ClassLoader aLoader = objClass.getClassLoader(); if (isDebugOn()) { debug("invoke","MBean Class is " + instance.getClass()); debug("invoke","MBean Interface is " + mbeanClass); } // Build the signature of the method // final Class[] tab = ((signature == null)?null: findSignatureClasses(signature,aLoader)); // Query the metadata service to get the right method // Method mth= findMethod(mbeanClass, operationName, tab); if (mth == null) { if (isTraceOn()) { trace("invoke", operationName + " not found in class " + mbeanClass.getName()); } throw new ReflectionException( new NoSuchMethodException(operationName), "The operation with name " + operationName + " could not be found"); } // Make it impossible to call getters and setters through invoke() // forbidInvokeGetterSetter(mth, operationName); // invoke the method if (isTraceOn()) { trace("invoke", "Invoking " + operationName); } Object result=null; try { result= mth.invoke(instance, params); } catch (IllegalAccessException e) { debugX("invoke",e); throw new ReflectionException(e, "IllegalAccessException" + " occured trying to invoke operation " + operationName); } catch (RuntimeException e) { debugX("invoke",e); throw new RuntimeOperationsException(e, "RuntimeException" + " occured trying to invoke operation " + operationName); } catch (InvocationTargetException e) { // Wrap the exception. Throwable t = e.getTargetException(); debugX("invoke",t); if (t instanceof RuntimeException) { final String msg = "RuntimeException thrown in operation " + operationName; throw wrapRuntimeException((RuntimeException) t, msg); } else if (t instanceof Error) { throw new RuntimeErrorException((Error) t, "Error thrown in operation " + operationName); } else { throw new MBeanException((Exception) t, "Exception thrown in operation " + operationName); } } if (isTraceOn()) { trace("invoke", "Send the result"); } return (result); } private static boolean startsWithAndHasMore(String s, String prefix) { return (s.startsWith(prefix) && s.length() > prefix.length()); } private static void forbidInvokeGetterSetter(Method mth, String operationName) throws ReflectionException { final Class argTypes[] = mth.getParameterTypes(); final Class resultType = mth.getReturnType(); final int argCount = argTypes.length; boolean isInvokeGetterSetter = false; switch (argCount) { case 0: // might be a getter if ((startsWithAndHasMore(operationName, "get") && resultType != Void.TYPE) || (startsWithAndHasMore(operationName, "is") && resultType == Boolean.TYPE)) { // Operation is a getter isInvokeGetterSetter = true; } break; case 1: // might be a setter if (startsWithAndHasMore(operationName, "set") && resultType == Void.TYPE) { // Operation is a setter isInvokeGetterSetter = true; } break; } if (isInvokeGetterSetter) { boolean allow; try { GetPropertyAction getProp = new GetPropertyAction("jmx.invoke.getters"); allow = (AccessController.doPrivileged(getProp) != null); } catch (SecurityException e) { // too bad, don't allow it allow = false; } if (!allow) { final String msg = "Cannot invoke getter or setter (" + operationName + ") as operation unless jmx.invoke.getters property is set"; final Exception nested = new NoSuchMethodException(operationName); throw new ReflectionException(nested, msg); } } } public boolean isInstanceOf(Object instance, String className) throws ReflectionException { final Class c = findClass(className, instance.getClass().getClassLoader()); return c.isInstance(instance); } /** * This methods returns the MBean interface of the given MBean * instance. * <p>It does so by calling * <code>getMBeanInterfaceFromClass(instance.getClass());</code> * @param instance the MBean instance. */ Class getMBeanInterfaceFromInstance(Object instance) { if (instance == null) return null; return getMBeanInterfaceFromClass(instance.getClass()); } /** * Cache the MBeanInfo and MBean interface obtained for class * <var>c</var>. * <p>This method is called by <code>testCompliance(...)</code> * after compliance is successfully verified. It uses two * {@link java.util.WeakHashMap WeakHashMaps} - one for the * MBeanInfo, one for the MBeanInterface, with calss <var>c</var> * as the key. * * @param c The concrete MBean class from which the MBeanInfo * was be built. * * @param mbeanInterface The management interface of the MBean. * Note that caching will not work if two MBeans of the same * class can have different mbeanInterface's. If you want * to use caching nonetheless, you will have to * to do it by redefining the method * {@link #getMBeanInterfaceFromInstance(java.lang.Object) * getMBeanInterfaceFromInstance()}. * @param info The MBeanInfo obtained from class <var>c</var> using * interface <var>mbeanInterface</var>. * **/ void cacheMBeanInfo(Class c, Class mbeanInterface, MBeanInfo info) throws NotCompliantMBeanException { if (info != null) { synchronized (mbeanInfoCache) { if (mbeanInfoCache.get(c) == null) { mbeanInfoCache.put(c, info); } } } if (mbeanInterface != null) { synchronized (mbeanInterfaceCache) { if ((mbeanInterfaceCache.get(c) == null) || (((WeakReference)mbeanInterfaceCache.get(c)).get() == null)) { mbeanInterfaceCache.put(c, new WeakReference(mbeanInterface)); } } } } /** * Returns the MBean interface that was cached for class <var>c</var>. * @param c The concrete MBean class. * @return The cached MBean interface if found, null otherwise. **/ Class getCachedMBeanInterface(Class c) { synchronized (mbeanInterfaceCache) { return (Class)(((WeakReference)mbeanInterfaceCache.get(c)).get()); } } /** * Returns the MBeanInfo that was cached for class <var>c</var>. * @param c The concrete MBean class. * @return The cached MBeanInfo if found, null otherwise. **/ MBeanInfo getCachedMBeanInfo(Class c) { synchronized (mbeanInfoCache) { return (MBeanInfo)mbeanInfoCache.get(c); } } /** * Find a class using the specified ClassLoader. **/ Class findClass(String className, ClassLoader loader) throws ReflectionException { return MBeanInstantiatorImpl.loadClass(className, loader); } /** * Find the classes from a signature using the specified ClassLoader. **/ Class[] findSignatureClasses(String[] signature, ClassLoader loader) throws ReflectionException { return ((signature == null)?null: MBeanInstantiatorImpl.loadSignatureClasses(signature,loader)); } /** * Invoke getAttribute through reflection on a standard MBean instance. **/ Object getAttribute(Object instance, String attribute, Class mbeanClass) throws MBeanException, AttributeNotFoundException, ReflectionException { if (attribute == null) { final RuntimeException r = new IllegalArgumentException("Attribute name cannot be null"); throw new RuntimeOperationsException(r, "Exception occured trying to invoke the getter on the MBean"); } // Standard MBean: need to reflect... Method meth = null; meth = findGetter(mbeanClass, attribute); if (meth == null) { if (isTraceOn()) { trace("getAttribute", "Cannot find getter for "+attribute+ " in class " + mbeanClass.getName()); } throw new AttributeNotFoundException(attribute + " not accessible"); } // Invoke the getter if (isTraceOn()) { trace("getAttribute", "Invoke callback"); } Object result= null; try { result = meth.invoke(instance, (Object[]) null); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); if (t instanceof RuntimeException) { debugX("getAttribute",t); final String msg = "RuntimeException thrown in the getter for the attribute " + attribute; throw wrapRuntimeException((RuntimeException) t, msg); } else if (t instanceof Error) { debugX("getAttribute",t); throw new RuntimeErrorException((Error) t , "Error thrown in the getter for the attribute " + attribute); } else { debugX("getAttribute",t); throw new MBeanException((Exception) t, "Exception thrown in the getter for the attribute " + attribute); } } catch (RuntimeException e) { debugX("getAttribute",e); throw new RuntimeOperationsException(e, "RuntimeException thrown trying to invoke the getter" + " for the attribute " + attribute); } catch (IllegalAccessException e) { debugX("getAttribute",e); throw new ReflectionException(e, "Exception thrown trying to" + " invoke the getter for the attribute " + attribute); } catch (Error e) { debugX("getAttribute",e); throw new RuntimeErrorException((Error)e, "Error thrown trying to invoke the getter " + " for the attribute " + attribute); } if (isTraceOn()) { trace("getAttribute", attribute + "= " + result + "\n"); } return result; } /** * Invoke setAttribute through reflection on a standard MBean instance. **/ Object setAttribute(Object instance, Attribute attribute, Class mbeanClass) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { if (attribute == null) { final RuntimeException r = new IllegalArgumentException("Attribute name cannot be null"); throw new RuntimeOperationsException(r, "Exception occured trying to invoke the setter on the MBean"); } final Class objClass = instance.getClass(); final ClassLoader aLoader = objClass.getClassLoader();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?