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

📄 simpledynamic.java

📁 Sun公司官方网站上下载的JDK文档
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @(#)file      SimpleDynamic.java * @(#)author    Sun Microsystems, Inc. * @(#)version   1.1 * @(#)lastedit  04/01/12 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. *//** * Simple definition of a dynamic MBean, named "SimpleDynamic". * * The "SimpleDynamic" dynamic MBean shows how to expose for management * attributes and operations, at runtime,  by implementing the   * "javax.management.DynamicMBean" interface. * * This MBean exposes for management two attributes and one operation: *      - the read/write "State" attribute, *      - the read only "NbChanges" attribute, *      - the "reset()" operation. * It does so by putting this information in an MBeanInfo object that * is returned by the getMBeanInfo() method of the DynamicMBean interface. * * It implements the access to its attributes through the getAttribute(), * getAttributes(), setAttribute(), and setAttributes() methods of the * DynamicMBean interface. * * It implements the invocation of its reset() operation through the * invoke() method of the DynamicMBean interface. *  * Note that as "SimpleDynamic" explicitly defines one constructor, * this constructor must be public and exposed for management through * the MBeanInfo object. */import java.lang.reflect.Constructor;import java.util.Iterator;import javax.management.*;public class SimpleDynamic    extends NotificationBroadcasterSupport    implements DynamicMBean {    /*     * -----------------------------------------------------     * CONSTRUCTORS     * -----------------------------------------------------     */    public SimpleDynamic() {        // Build the management information to be exposed by the dynamic MBean        //        buildDynamicMBeanInfo();    }    /*     * -----------------------------------------------------     * IMPLEMENTATION OF THE DynamicMBean INTERFACE     * -----------------------------------------------------     */    /**     * Allows the value of the specified attribute of the Dynamic MBean to be     * obtained.     */    public Object getAttribute(String attribute_name)         throws AttributeNotFoundException,               MBeanException,               ReflectionException {        // Check attribute_name is not null to avoid NullPointerException        // later on        //        if (attribute_name == null) {            throw new RuntimeOperationsException(                  new IllegalArgumentException("Attribute name cannot be null"),                  "Cannot invoke a getter of " + dClassName +                  " with null attribute name");        }        // Check for a recognized attribute_name and call the corresponding        // getter        //        if (attribute_name.equals("State")) {            return getState();        }         if (attribute_name.equals("NbChanges")) {            return getNbChanges();        }        // If attribute_name has not been recognized throw an        // AttributeNotFoundException        //        throw new AttributeNotFoundException("Cannot find " +                                             attribute_name +                                             " attribute in " +                                             dClassName);    }    /**     * Sets the value of the specified attribute of the Dynamic MBean.     */    public void setAttribute(Attribute attribute)         throws AttributeNotFoundException,               InvalidAttributeValueException,               MBeanException,                ReflectionException {        // Check attribute is not null to avoid NullPointerException later on        //        if (attribute == null) {            throw new RuntimeOperationsException(                  new IllegalArgumentException("Attribute cannot be null"),                   "Cannot invoke a setter of " + dClassName +                  " with null attribute");        }        String name = attribute.getName();        Object value = attribute.getValue();        if (name == null) {            throw new RuntimeOperationsException(                  new IllegalArgumentException("Attribute name cannot be null"),                  "Cannot invoke the setter of " + dClassName +                  " with null attribute name");        }        // Check for a recognized attribute name and call the corresponding        // setter        //        if (name.equals("State")) {            // if null value, try and see if the setter returns any exception            if (value == null) {                try {                    setState( null );                } catch (Exception e) {                    throw new InvalidAttributeValueException(                              "Cannot set attribute " + name + " to null");                }            }            // if non null value, make sure it is assignable to the attribute            else {                try {                    if (Class.forName("java.lang.String").isAssignableFrom(                                                          value.getClass())) {                        setState((String) value);                    } else {                        throw new InvalidAttributeValueException(                                  "Cannot set attribute " + name + " to a " +                                   value.getClass().getName() +                                  " object, String expected");                    }                } catch (ClassNotFoundException e) {                    e.printStackTrace();                }            }        }        // recognize an attempt to set "NbChanges" attribute (read-only):        else if (name.equals("NbChanges")) {            throw new AttributeNotFoundException(                  "Cannot set attribute " + name + " because it is read-only");        }        // unrecognized attribute name:        else {            throw new AttributeNotFoundException("Attribute " + name +                                                 " not found in " +                                                 this.getClass().getName());        }    }    /**     * Enables the to get the values of several attributes of the Dynamic MBean.     */    public AttributeList getAttributes(String[] attributeNames) {        // Check attributeNames is not null to avoid NullPointerException        // later on        //        if (attributeNames == null) {            throw new RuntimeOperationsException(                new IllegalArgumentException("attributeNames[] cannot be null"),                "Cannot invoke a getter of " + dClassName);        }        AttributeList resultList = new AttributeList();        // If attributeNames is empty, return an empty result list        //        if (attributeNames.length == 0)            return resultList;                // Build the result attribute list        //        for (int i = 0 ; i < attributeNames.length ; i++) {            try {                        Object value = getAttribute((String) attributeNames[i]);                     resultList.add(new Attribute(attributeNames[i],value));            } catch (Exception e) {                e.printStackTrace();            }        }        return resultList;    }    /**     * Sets the values of several attributes of the Dynamic MBean, and returns     * the list of attributes that have been set.     */    public AttributeList setAttributes(AttributeList attributes) {        // Check attributes is not null to avoid NullPointerException later on        //        if (attributes == null) {            throw new RuntimeOperationsException(                      new IllegalArgumentException(                                 "AttributeList attributes cannot be null"),                      "Cannot invoke a setter of " + dClassName);        }

⌨️ 快捷键说明

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