xmbeanoperation.java

来自「jmx codeJava源码」· Java 代码 · 共 76 行

JAVA
76
字号

package book.jmx.examples;

import java.lang.reflect.*;
import javax.management.*;
import javax.management.modelmbean.*;
import javax.management.loading.*;

public class XMBeanOperation implements XMBeanConstants {

  protected Object managedResource       = null;
  protected Class[] signature            = null;
  protected Method operationMethod       = null;
  protected String operationName         = null;
  
  
  public XMBeanOperation(XMBean mbean,
      ModelMBeanOperationInfo operationInfo) throws MBeanException {
    
    try {
      this.operationName   = operationInfo.getName();
      this.managedResource = mbean.resource;
      this.signature       = createSignature(operationInfo);
      this.operationMethod = managedResource.getClass().
          getMethod(operationName, signature);
    }
    catch (NoSuchMethodException e) {
      throw new MBeanException(e,
          "Resource method " + operationName + " not found."
      );
    }
  }    
  
  private Class[] createSignature(ModelMBeanOperationInfo info) 
      throws MBeanException {
    
    Class[] paramTypes        = null;
    MBeanParameterInfo[] sign = info.getSignature();    
    
    if (sign != null) {
      paramTypes = new Class[sign.length];
   
      for (int i = 0; i < paramTypes.length; ++i) {
        try {
          String type = sign[i].getType();
          paramTypes[i] = DefaultLoaderRepository.loadClass(type);
        }
        catch (ClassNotFoundException e) {
          throw new MBeanException(e, 
              "Error loading parameter class " + sign[i].getType()
          );
        }
      }
    }

    return paramTypes;    
  }
  
  public Object invoke(Object[] args) throws ReflectionException {

    if (operationMethod == null) {
      throw new ReflectionException(new Exception(        
          "Method " + operationName + " not found.")
       );
    }
  
    try {
      return operationMethod.invoke(managedResource, args);
    }
    catch (Exception e) {
      throw new ReflectionException(e);
    }
  }
  
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?