0N/A/*
2362N/A * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A/*
0N/A * @author IBM Corp.
0N/A *
0N/A * Copyright IBM Corp. 1999-2000. All rights reserved.
0N/A */
0N/A
0N/Apackage javax.management.modelmbean;
0N/A
0N/Aimport static com.sun.jmx.defaults.JmxProperties.MODELMBEAN_LOGGER;
0N/Aimport com.sun.jmx.mbeanserver.GetPropertyAction;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectStreamField;
0N/Aimport java.lang.reflect.Method;
0N/Aimport java.security.AccessController;
0N/Aimport java.util.logging.Level;
0N/A
0N/Aimport javax.management.Descriptor;
0N/Aimport javax.management.DescriptorKey;
0N/Aimport javax.management.DescriptorAccess;
0N/Aimport javax.management.MBeanAttributeInfo;
0N/Aimport javax.management.RuntimeOperationsException;
0N/A
0N/A/**
689N/A * <p>The ModelMBeanAttributeInfo object describes an attribute of the ModelMBean.
0N/A * It is a subclass of MBeanAttributeInfo with the addition of an associated Descriptor
689N/A * and an implementation of the DescriptorAccess interface.</p>
689N/A *
689N/A * <P id="descriptor">
689N/A * The fields in the descriptor are defined, but not limited to, the following.
689N/A * Note that when the Type in this table is Number, a String that is the decimal
689N/A * representation of a Long can also be used.</P>
689N/A *
689N/A * <table border="1" cellpadding="5">
689N/A * <tr><th>Name</th><th>Type</th><th>Meaning</th></tr>
689N/A * <tr><td>name</td><td>String</td>
689N/A * <td>Attribute name.</td></tr>
689N/A * <tr><td>descriptorType</td><td>String</td>
689N/A * <td>Must be "attribute".</td></tr>
689N/A * <tr id="value-field"><td>value</td><td>Object</td>
689N/A * <td>Current (cached) value for attribute.</td></tr>
689N/A * <tr><td>default</td><td>Object</td>
689N/A * <td>Default value for attribute.</td></tr>
689N/A * <tr><td>displayName</td><td>String</td>
689N/A * <td>Name of attribute to be used in displays.</td></tr>
689N/A * <tr><td>getMethod</td><td>String</td>
689N/A * <td>Name of operation descriptor for get method.</td></tr>
689N/A * <tr><td>setMethod</td><td>String</td>
689N/A * <td>Name of operation descriptor for set method.</td></tr>
689N/A * <tr><td>protocolMap</td><td>Descriptor</td>
689N/A * <td>See the section "Protocol Map Support" in the JMX specification
689N/A * document. Mappings must be appropriate for the attribute and entries
689N/A * can be updated or augmented at runtime.</td></tr>
689N/A * <tr><td>persistPolicy</td><td>String</td>
689N/A * <td>One of: OnUpdate|OnTimer|NoMoreOftenThan|OnUnregister|Always|Never.
689N/A * See the section "MBean Descriptor Fields" in the JMX specification
689N/A * document.</td></tr>
689N/A * <tr><td>persistPeriod</td><td>Number</td>
689N/A * <td>Frequency of persist cycle in seconds. Used when persistPolicy is
689N/A * "OnTimer" or "NoMoreOftenThan".</td></tr>
689N/A * <tr><td>currencyTimeLimit</td><td>Number</td>
689N/A * <td>How long <a href="#value=field">value</a> is valid: &lt;0 never,
689N/A * =0 always, &gt;0 seconds.</td></tr>
689N/A * <tr><td>lastUpdatedTimeStamp</td><td>Number</td>
689N/A * <td>When <a href="#value-field">value</a> was set.</td></tr>
689N/A * <tr><td>visibility</td><td>Number</td>
689N/A * <td>1-4 where 1: always visible, 4: rarely visible.</td></tr>
689N/A * <tr><td>presentationString</td><td>String</td>
689N/A * <td>XML formatted string to allow presentation of data.</td></tr>
689N/A * </table>
689N/A *
689N/A * <p>The default descriptor contains the name, descriptorType and displayName
689N/A * fields. The default value of the name and displayName fields is the name of
689N/A * the attribute.</p>
0N/A *
0N/A * <p><b>Note:</b> because of inconsistencies in previous versions of
0N/A * this specification, it is recommended not to use negative or zero
0N/A * values for <code>currencyTimeLimit</code>. To indicate that a
0N/A * cached value is never valid, omit the
0N/A * <code>currencyTimeLimit</code> field. To indicate that it is
0N/A * always valid, use a very large number for this field.</p>
0N/A *
0N/A * <p>The <b>serialVersionUID</b> of this class is <code>6181543027787327345L</code>.
0N/A *
0N/A * @since 1.5
0N/A */
0N/A
0N/A@SuppressWarnings("serial") // serialVersionUID is not constant
0N/Apublic class ModelMBeanAttributeInfo
0N/A extends MBeanAttributeInfo
0N/A implements DescriptorAccess {
0N/A
0N/A // Serialization compatibility stuff:
0N/A // Two serial forms are supported in this class. The selected form depends
0N/A // on system property "jmx.serial.form":
0N/A // - "1.0" for JMX 1.0
0N/A // - any other value for JMX 1.1 and higher
0N/A //
0N/A // Serial version for old serial form
0N/A private static final long oldSerialVersionUID = 7098036920755973145L;
0N/A //
0N/A // Serial version for new serial form
0N/A private static final long newSerialVersionUID = 6181543027787327345L;
0N/A //
0N/A // Serializable fields in old serial form
0N/A private static final ObjectStreamField[] oldSerialPersistentFields =
0N/A {
0N/A new ObjectStreamField("attrDescriptor", Descriptor.class),
0N/A new ObjectStreamField("currClass", String.class)
0N/A };
0N/A //
0N/A // Serializable fields in new serial form
0N/A private static final ObjectStreamField[] newSerialPersistentFields =
0N/A {
0N/A new ObjectStreamField("attrDescriptor", Descriptor.class)
0N/A };
0N/A //
0N/A // Actual serial version and serial form
0N/A private static final long serialVersionUID;
0N/A /**
0N/A * @serialField attrDescriptor Descriptor The {@link Descriptor}
0N/A * containing the metadata corresponding to this attribute
0N/A */
0N/A private static final ObjectStreamField[] serialPersistentFields;
0N/A private static boolean compat = false;
0N/A static {
0N/A try {
0N/A GetPropertyAction act = new GetPropertyAction("jmx.serial.form");
0N/A String form = AccessController.doPrivileged(act);
0N/A compat = (form != null && form.equals("1.0"));
0N/A } catch (Exception e) {
0N/A // OK: No compat with 1.0
0N/A }
0N/A if (compat) {
0N/A serialPersistentFields = oldSerialPersistentFields;
0N/A serialVersionUID = oldSerialVersionUID;
0N/A } else {
0N/A serialPersistentFields = newSerialPersistentFields;
0N/A serialVersionUID = newSerialVersionUID;
0N/A }
0N/A }
0N/A //
0N/A // END Serialization compatibility stuff
0N/A
0N/A /**
0N/A * @serial The {@link Descriptor} containing the metadata corresponding to
0N/A * this attribute
0N/A */
0N/A private Descriptor attrDescriptor = validDescriptor(null);
0N/A
0N/A private final static String currClass = "ModelMBeanAttributeInfo";
0N/A
0N/A /**
0N/A * Constructs a ModelMBeanAttributeInfo object with a default
0N/A * descriptor. The {@link Descriptor} of the constructed
0N/A * object will include fields contributed by any annotations
0N/A * on the {@code Method} objects that contain the {@link
0N/A * DescriptorKey} meta-annotation.
0N/A *
0N/A * @param name The name of the attribute.
0N/A * @param description A human readable description of the attribute. Optional.
0N/A * @param getter The method used for reading the attribute value.
0N/A * May be null if the property is write-only.
0N/A * @param setter The method used for writing the attribute value.
0N/A * May be null if the attribute is read-only.
0N/A * @exception IntrospectionException There is a consistency
0N/A * problem in the definition of this attribute.
0N/A *
0N/A */
0N/A
0N/A public ModelMBeanAttributeInfo(String name,
0N/A String description,
0N/A Method getter,
0N/A Method setter)
0N/A throws javax.management.IntrospectionException {
0N/A super(name, description, getter, setter);
0N/A
0N/A if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
0N/A MODELMBEAN_LOGGER.logp(Level.FINER,
0N/A ModelMBeanAttributeInfo.class.getName(),
0N/A "ModelMBeanAttributeInfo(" +
0N/A "String,String,Method,Method)",
0N/A "Entry", name);
0N/A }
0N/A
0N/A attrDescriptor = validDescriptor(null);
0N/A // put getter and setter methods in operations list
0N/A // create default descriptor
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Constructs a ModelMBeanAttributeInfo object. The {@link
0N/A * Descriptor} of the constructed object will include fields
0N/A * contributed by any annotations on the {@code Method}
0N/A * objects that contain the {@link DescriptorKey}
0N/A * meta-annotation.
0N/A *
0N/A * @param name The name of the attribute.
0N/A * @param description A human readable description of the attribute. Optional.
0N/A * @param getter The method used for reading the attribute value.
0N/A * May be null if the property is write-only.
0N/A * @param setter The method used for writing the attribute value.
0N/A * May be null if the attribute is read-only.
0N/A * @param descriptor An instance of Descriptor containing the
0N/A * appropriate metadata for this instance of the Attribute. If
0N/A * it is null, then a default descriptor will be created. If
1790N/A * the descriptor does not contain the field "displayName" this field is added
1790N/A * in the descriptor with its default value.
0N/A * @exception IntrospectionException There is a consistency
0N/A * problem in the definition of this attribute.
0N/A * @exception RuntimeOperationsException Wraps an
1790N/A * IllegalArgumentException. The descriptor is invalid, or descriptor
1790N/A * field "name" is not equal to name parameter, or descriptor field
1790N/A * "descriptorType" is not equal to "attribute".
0N/A *
0N/A */
0N/A
0N/A public ModelMBeanAttributeInfo(String name,
0N/A String description,
0N/A Method getter,
0N/A Method setter,
0N/A Descriptor descriptor)
0N/A throws javax.management.IntrospectionException {
0N/A
0N/A super(name, description, getter, setter);
0N/A // put getter and setter methods in operations list
0N/A if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
0N/A MODELMBEAN_LOGGER.logp(Level.FINER,
0N/A ModelMBeanAttributeInfo.class.getName(),
0N/A "ModelMBeanAttributeInfo(" +
0N/A "String,String,Method,Method,Descriptor)",
0N/A "Entry", name);
0N/A }
0N/A attrDescriptor = validDescriptor(descriptor);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a ModelMBeanAttributeInfo object with a default descriptor.
0N/A *
0N/A * @param name The name of the attribute
0N/A * @param type The type or class name of the attribute
0N/A * @param description A human readable description of the attribute.
0N/A * @param isReadable True if the attribute has a getter method, false otherwise.
0N/A * @param isWritable True if the attribute has a setter method, false otherwise.
0N/A * @param isIs True if the attribute has an "is" getter, false otherwise.
0N/A *
0N/A */
0N/A public ModelMBeanAttributeInfo(String name,
0N/A String type,
0N/A String description,
0N/A boolean isReadable,
0N/A boolean isWritable,
0N/A boolean isIs)
0N/A {
0N/A
0N/A super(name, type, description, isReadable, isWritable, isIs);
0N/A // create default descriptor
0N/A if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
0N/A MODELMBEAN_LOGGER.logp(Level.FINER,
0N/A ModelMBeanAttributeInfo.class.getName(),
0N/A "ModelMBeanAttributeInfo(" +
0N/A "String,String,String,boolean,boolean,boolean)",
0N/A "Entry", name);
0N/A }
0N/A attrDescriptor = validDescriptor(null);
0N/A }
0N/A
0N/A /**
1790N/A * Constructs a ModelMBeanAttributeInfo object.
0N/A *
0N/A * @param name The name of the attribute
0N/A * @param type The type or class name of the attribute
0N/A * @param description A human readable description of the attribute.
0N/A * @param isReadable True if the attribute has a getter method, false otherwise.
0N/A * @param isWritable True if the attribute has a setter method, false otherwise.
0N/A * @param isIs True if the attribute has an "is" getter, false otherwise.
0N/A * @param descriptor An instance of Descriptor containing the
0N/A * appropriate metadata for this instance of the Attribute. If
0N/A * it is null then a default descriptor will be created. If
1790N/A * the descriptor does not contain the field "displayName" this field
1790N/A * is added in the descriptor with its default value.
0N/A * @exception RuntimeOperationsException Wraps an
1790N/A * IllegalArgumentException. The descriptor is invalid, or descriptor
1790N/A * field "name" is not equal to name parameter, or descriptor field
1790N/A * "descriptorType" is not equal to "attribute".
0N/A *
0N/A */
0N/A public ModelMBeanAttributeInfo(String name,
0N/A String type,
0N/A String description,
0N/A boolean isReadable,
0N/A boolean isWritable,
0N/A boolean isIs,
0N/A Descriptor descriptor)
0N/A {
0N/A super(name, type, description, isReadable, isWritable, isIs);
0N/A if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
0N/A MODELMBEAN_LOGGER.logp(Level.FINER,
0N/A ModelMBeanAttributeInfo.class.getName(),
0N/A "ModelMBeanAttributeInfo(String,String,String," +
0N/A "boolean,boolean,boolean,Descriptor)",
0N/A "Entry", name);
0N/A }
0N/A attrDescriptor = validDescriptor(descriptor);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new ModelMBeanAttributeInfo object from this
0N/A * ModelMBeanAttributeInfo Object. A default descriptor will
0N/A * be created.
0N/A *
0N/A * @param inInfo the ModelMBeanAttributeInfo to be duplicated
0N/A */
0N/A
0N/A public ModelMBeanAttributeInfo(ModelMBeanAttributeInfo inInfo)
0N/A {
0N/A super(inInfo.getName(),
0N/A inInfo.getType(),
0N/A inInfo.getDescription(),
0N/A inInfo.isReadable(),
0N/A inInfo.isWritable(),
0N/A inInfo.isIs());
0N/A if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
0N/A MODELMBEAN_LOGGER.logp(Level.FINER,
0N/A ModelMBeanAttributeInfo.class.getName(),
0N/A "ModelMBeanAttributeInfo(ModelMBeanAttributeInfo)",
0N/A "Entry");
0N/A }
0N/A Descriptor newDesc = inInfo.getDescriptor();
0N/A attrDescriptor = validDescriptor(newDesc);
0N/A }
0N/A
0N/A /**
0N/A * Gets a copy of the associated Descriptor for the
0N/A * ModelMBeanAttributeInfo.
0N/A *
0N/A * @return Descriptor associated with the
0N/A * ModelMBeanAttributeInfo object.
0N/A *
0N/A * @see #setDescriptor
0N/A */
0N/A
0N/A public Descriptor getDescriptor() {
0N/A if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
0N/A MODELMBEAN_LOGGER.logp(Level.FINER,
0N/A ModelMBeanAttributeInfo.class.getName(),
0N/A "getDescriptor()", "Entry");
0N/A }
0N/A if (attrDescriptor == null) {
0N/A attrDescriptor = validDescriptor(null);
0N/A }
0N/A return((Descriptor)attrDescriptor.clone());
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets associated Descriptor (full replace) for the
0N/A * ModelMBeanAttributeDescriptor. If the new Descriptor is
0N/A * null, then the associated Descriptor reverts to a default
0N/A * descriptor. The Descriptor is validated before it is
0N/A * assigned. If the new Descriptor is invalid, then a
0N/A * RuntimeOperationsException wrapping an
0N/A * IllegalArgumentException is thrown.
0N/A * @param inDescriptor replaces the Descriptor associated with the
0N/A * ModelMBeanAttributeInfo
0N/A *
0N/A * @exception RuntimeOperationsException Wraps an
0N/A * IllegalArgumentException for an invalid Descriptor
0N/A *
0N/A * @see #getDescriptor
0N/A */
0N/A public void setDescriptor(Descriptor inDescriptor) {
0N/A attrDescriptor = validDescriptor(inDescriptor);
0N/A }
0N/A
0N/A /**
0N/A * Creates and returns a new ModelMBeanAttributeInfo which is a duplicate of this ModelMBeanAttributeInfo.
0N/A *
0N/A * @exception RuntimeOperationsException for illegal value for
0N/A * field Names or field Values. If the descriptor construction
0N/A * fails for any reason, this exception will be thrown.
0N/A */
0N/A
1790N/A @Override
0N/A public Object clone()
0N/A {
0N/A if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
0N/A MODELMBEAN_LOGGER.logp(Level.FINER,
0N/A ModelMBeanAttributeInfo.class.getName(),
0N/A "clone()", "Entry");
0N/A }
0N/A return(new ModelMBeanAttributeInfo(this));
0N/A }
0N/A
0N/A /**
0N/A * Returns a human-readable version of the
0N/A * ModelMBeanAttributeInfo instance.
0N/A */
1790N/A @Override
0N/A public String toString()
0N/A {
0N/A return
0N/A "ModelMBeanAttributeInfo: " + this.getName() +
0N/A " ; Description: " + this.getDescription() +
0N/A " ; Types: " + this.getType() +
0N/A " ; isReadable: " + this.isReadable() +
0N/A " ; isWritable: " + this.isWritable() +
0N/A " ; Descriptor: " + this.getDescriptor();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Clones the passed in Descriptor, sets default values, and checks for validity.
0N/A * If the Descriptor is invalid (for instance by having the wrong "name"),
0N/A * this indicates programming error and a RuntimeOperationsException will be thrown.
0N/A *
0N/A * The following fields will be defaulted if they are not already set:
0N/A * displayName=this.getName(),name=this.getName(),descriptorType = "attribute"
0N/A *
0N/A * @param in Descriptor to be checked, or null which is equivalent to
0N/A * an empty Descriptor.
0N/A * @exception RuntimeOperationsException if Descriptor is invalid
0N/A */
0N/A private Descriptor validDescriptor(final Descriptor in) throws RuntimeOperationsException {
0N/A
277N/A Descriptor clone;
1790N/A boolean defaulted = (in == null);
1790N/A if (defaulted) {
0N/A clone = new DescriptorSupport();
0N/A MODELMBEAN_LOGGER.finer("Null Descriptor, creating new.");
0N/A } else {
0N/A clone = (Descriptor) in.clone();
0N/A }
0N/A
0N/A //Setting defaults.
1790N/A if (defaulted && clone.getFieldValue("name")==null) {
0N/A clone.setField("name", this.getName());
0N/A MODELMBEAN_LOGGER.finer("Defaulting Descriptor name to " + this.getName());
0N/A }
1790N/A if (defaulted && clone.getFieldValue("descriptorType")==null) {
0N/A clone.setField("descriptorType", "attribute");
0N/A MODELMBEAN_LOGGER.finer("Defaulting descriptorType to \"attribute\"");
0N/A }
0N/A if (clone.getFieldValue("displayName") == null) {
0N/A clone.setField("displayName",this.getName());
0N/A MODELMBEAN_LOGGER.finer("Defaulting Descriptor displayName to " + this.getName());
0N/A }
0N/A
0N/A //Checking validity
0N/A if (!clone.isValid()) {
0N/A throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"),
0N/A "The isValid() method of the Descriptor object itself returned false,"+
0N/A "one or more required fields are invalid. Descriptor:" + clone.toString());
0N/A }
1790N/A if (!getName().equalsIgnoreCase((String)clone.getFieldValue("name"))) {
0N/A throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"),
0N/A "The Descriptor \"name\" field does not match the object described. " +
0N/A " Expected: "+ this.getName() + " , was: " + clone.getFieldValue("name"));
0N/A }
0N/A
1790N/A if (!"attribute".equalsIgnoreCase((String)clone.getFieldValue("descriptorType"))) {
0N/A throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"),
0N/A "The Descriptor \"descriptorType\" field does not match the object described. " +
0N/A " Expected: \"attribute\" ," + " was: " + clone.getFieldValue("descriptorType"));
0N/A }
0N/A
0N/A return clone;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Deserializes a {@link ModelMBeanAttributeInfo} from an {@link ObjectInputStream}.
0N/A */
0N/A private void readObject(ObjectInputStream in)
0N/A throws IOException, ClassNotFoundException {
0N/A // New serial form ignores extra field "currClass"
0N/A in.defaultReadObject();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Serializes a {@link ModelMBeanAttributeInfo} to an {@link ObjectOutputStream}.
0N/A */
0N/A private void writeObject(ObjectOutputStream out)
0N/A throws IOException {
0N/A if (compat)
0N/A {
0N/A // Serializes this instance in the old serial form
0N/A //
0N/A ObjectOutputStream.PutField fields = out.putFields();
0N/A fields.put("attrDescriptor", attrDescriptor);
0N/A fields.put("currClass", currClass);
0N/A out.writeFields();
0N/A }
0N/A else
0N/A {
0N/A // Serializes this instance in the new serial form
0N/A //
0N/A out.defaultWriteObject();
0N/A }
0N/A }
0N/A
0N/A}