⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmbean.java

📁 jmx codeJava源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     
       metadata = (ModelMBeanInfoSupport)ois.readObject();
     }
     catch (Exception e) {
       System.out.println("Error loading MBean state");
     }
   }
  }
  
  public void store() throws MBeanException,
      InstanceNotFoundException {

   try {
     Descriptor d = metadata.getMBeanDescriptor();
     String dir  = (String)d.getFieldValue(PERSIST_LOCATION);
     String file = (String)d.getFieldValue(PERSIST_NAME);
    
     File f = new File(dir, file);
     FileOutputStream fos   = new FileOutputStream(f);
     ObjectOutputStream oos = new ObjectOutputStream(fos);
    
     oos.writeObject(metadata);
   }
   catch (IOException e) {
     throw new MBeanException(e, "Error in persisting MBean.");
   }
    //throw new UnsupportedOperationException();
  }       
            
      
  // MBeanRegistration interface
    
  public ObjectName preRegister(MBeanServer server,
                                ObjectName name) 
                                throws Exception {    

    // store the server reference
    this.server = server;
    this.name    = name;
    
    // provide a default object name
    if (name == null || name.equals("")) {
      name = new ObjectName(
          server.getDefaultDomain() +
          ":type=XMBean,name=Default"
      );
    }    

    // check if configured to load xml definition
//    if (metadata instanceof XMBeanInfo) {
//      String url = 
//          (String)metadata.getMBeanDescriptor().
//              getFieldValue(XML_DEFINITION_URL);
    
   //   Descriptor   mbeanDescriptor = metadata.getMBeanDescriptor();
   //   Descriptor[] attrDescriptors = metadata.getDescriptors("attribute");
   //   Descriptor[] operDescriptors = metadata.getDescriptors("operation");
      
//      MetaDataBuilder builder = new XMLMetaDataBuilder(
//          resource.getClass().getName(),
//          new URL(url)
//      );
//      metadata = builder.build();
      
     // metadata.setMBeanDescriptor(mbeanDescriptor);
     // metadata.setDescriptors(attrDescriptors);
//    }
  
    // create attribute and operation maps
    attributeMap = createAttributeMap(metadata.getAttributes());
    operationMap = createOperationMap(metadata.getOperations());

    
//    init();
    
    return name;
  }

  private Map createAttributeMap(MBeanAttributeInfo[] attributes) 
      throws MBeanException  {
    
    Map attrMap = new HashMap();
    
    for (int i = 0; i < attributes.length; ++i) {
      String name = attributes[i].getName();
      
      ModelMBeanAttributeInfo info = 
          (ModelMBeanAttributeInfo)attributes[i];
          
      attrMap.put(name, new XMBeanAttribute(this, info));        
    }
    
    return attrMap;
  }
  
  private Map createOperationMap(MBeanOperationInfo[] operations) 
      throws MBeanException {
      
    Map operMap = new HashMap();
    
    for (int i = 0; i < operations.length; ++i) {
      String name = operations[i].getName();
      MBeanParameterInfo[] params = operations[i].getSignature();
      
      for (int j = 0; j < params.length; ++j)
        name += params[j].getType();
        
      ModelMBeanOperationInfo info =
          (ModelMBeanOperationInfo)operations[i];
          
      XMBeanOperation operation = new XMBeanOperation(this, info);
      operMap.put(name, operation);
      
    }
    
    return operMap;
  }


  
  public void postRegister(Boolean registrationSuccessful) {
//    if (registrationSuccessful.booleanValue() == false) {
//      System.out.println("Registration of " + name + " failed.");
//      return;
//    }
//    
//    start();      
//
// For Chapter 10:
//
//    try {
//      Descriptor descr = metadata.getMBeanDescriptor();
//      Object policy = descr.getFieldValue(EXPORT);
//    
//      if (policy != null && policy instanceof ExportPolicy) {
//        ObjectName exportManager = 
//            new ObjectName("Service:name=ExportManager");        
//      
//        server.invoke(
//            exportManager,
//            "export",
//            new Object[] { name, "MyMBean"/*policy*/ },
//            new String[] { "javax.management.ObjectName", "java.lang.String" /*"book.jmx.examples.ExportPolicy"*/ }
//        );
//      }
//    }
//    catch (JMException e) {
//      e.printStackTrace();
//    }
//    
  }
  
  public void preDeregister() throws Exception {  }
  public void postDeregister() {  }
  
  
  // DynamicMBean interface
  
  public Object getAttribute(String attribute)
      throws AttributeNotFoundException, MBeanException,
      ReflectionException {
       
    XMBeanAttribute attr = 
        (XMBeanAttribute)attributeMap.get(attribute);
        
    if (attr == null)
      throw new AttributeNotFoundException();
      
    return attr.getValue();
  }

            
  public AttributeList getAttributes(String[] attributes) {
    
    if (attributes == null)
      throw new IllegalArgumentException("null array");
      
    AttributeList list = new AttributeList();
    
    for (int i = 0; i < attributes.length; ++i) {
      try {
        list.add(new Attribute(
            attributes[i], getAttribute(attributes[i])
        ));
      }
      catch (JMException ignored) {
        // if the attribute could not be retrieved, skip it            
      }
    }
    
    return list;
  }

   
  public void setAttribute(Attribute attribute) 
      throws AttributeNotFoundException,
      InvalidAttributeValueException, MBeanException,
      ReflectionException {
  
    String attrName  = attribute.getName();
    Object attrValue = attribute.getValue();

    XMBeanAttribute attr = 
        (XMBeanAttribute)attributeMap.get(attrName);
    
    if (attr == null)
      throw new AttributeNotFoundException();
    
    try {
      attr.setValue(attrValue);
    }
    catch (InstanceNotFoundException e) {
      
      // may be thrown by PersistentMBean.store()
        
      throw new MBeanException(e);
    }              
  }
            
  public AttributeList setAttributes(AttributeList list) {
      
    if (list == null)
      throw new IllegalArgumentException("null list");
      
    AttributeList results = new AttributeList();
    Iterator it           = list.iterator();
    
    while (it.hasNext()) {
      try {
        Attribute attr = (Attribute)it.next();
        setAttribute(attr);
        results.add(attr);
      }
      catch (JMException ignored) {
        // if unable to set the attribute, skip it
      }
    }    
    
    return results;
  }
          
    
  public Object invoke(String actionName, Object[] params,
                       String[] signature)
      throws MBeanException, ReflectionException {
          
    
    String method = actionName/* + "("*/;
    
    if (signature != null) {
      for (int i = 0; i < signature.length; ++i)
        method += signature[i];
    }
    
    
    XMBeanOperation operation =
        (XMBeanOperation)operationMap.get(method);
    
    if (operation == null) {
      throw new ReflectionException(
         new IllegalArgumentException("unknown operation")
      );
    }
      
    return operation.invoke(params);
  }
  
  public MBeanInfo getMBeanInfo() {
    return (MBeanInfo)metadata;
  }
    

  
}

⌨️ 快捷键说明

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