001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package javax.management.modelmbean;
010:
011: import java.lang.reflect.Method;
012: import javax.management.Descriptor;
013: import javax.management.DescriptorAccess;
014: import javax.management.IntrospectionException;
015: import javax.management.MBeanAttributeInfo;
016: import javax.management.RuntimeOperationsException;
017:
018: /**
019: * @version $Revision: 1.15 $
020: */
021: // Change not needed, workaround to a TCK bug only to achieve TCK compliance
022: // public class ModelMBeanAttributeInfo extends MBeanAttributeInfo implements DescriptorAccess
023: public class ModelMBeanAttributeInfo extends MBeanAttributeInfo
024: implements DescriptorAccess, Cloneable {
025: private static final long serialVersionUID = 6181543027787327345L;
026:
027: private Descriptor attrDescriptor;
028:
029: public ModelMBeanAttributeInfo(String name, String description,
030: Method getter, Method setter) throws IntrospectionException {
031: this (name, description, getter, setter, null);
032: }
033:
034: public ModelMBeanAttributeInfo(String name, String description,
035: Method getter, Method setter, Descriptor descriptor)
036: throws IntrospectionException {
037: super (name, description, getter, setter);
038: checkAndSetDescriptor(descriptor);
039: }
040:
041: public ModelMBeanAttributeInfo(String name, String type,
042: String description, boolean isReadable, boolean isWritable,
043: boolean isIs) {
044: this (name, type, description, isReadable, isWritable, isIs,
045: null);
046: }
047:
048: public ModelMBeanAttributeInfo(String name, String type,
049: String description, boolean isReadable, boolean isWritable,
050: boolean isIs, Descriptor descriptor) {
051: super (name, type, description, isReadable, isWritable, isIs);
052: checkAndSetDescriptor(descriptor);
053: }
054:
055: public ModelMBeanAttributeInfo(ModelMBeanAttributeInfo copy) {
056: super (copy.getName(), copy.getType(), copy.getDescription(),
057: copy.isReadable(), copy.isWritable(), copy.isIs());
058: checkAndSetDescriptor(copy.getDescriptor());
059: }
060:
061: public Object clone() {
062: return new ModelMBeanAttributeInfo(this );
063: }
064:
065: public Descriptor getDescriptor() {
066: return (Descriptor) attrDescriptor.clone();
067: }
068:
069: public void setDescriptor(Descriptor descriptor) {
070: if (descriptor == null) {
071: attrDescriptor = createDefaultDescriptor();
072: } else {
073: if (isDescriptorValid(descriptor)) {
074: attrDescriptor = (Descriptor) descriptor.clone();
075: } else {
076: // Not sure what to do here: javadoc says IllegalArgument, but for example ModelMBeanInfo throws RuntimeOperations
077: // which is consistent with the fact that all exception thrown by the JMX implementation should be JMX exceptions
078: throw new RuntimeOperationsException(
079: new IllegalArgumentException(
080: "Invalid descriptor"));
081: }
082: }
083: }
084:
085: private void checkAndSetDescriptor(Descriptor descriptor) {
086: if (descriptor == null) {
087: attrDescriptor = createDefaultDescriptor();
088: } else if (isDescriptorValid(descriptor)) {
089: attrDescriptor = (Descriptor) descriptor.clone();
090: if (attrDescriptor.getFieldValue("displayname") == null) {
091: attrDescriptor.setField("displayname", this .getName());
092: }
093: } else {
094: throw new RuntimeOperationsException(
095: new IllegalArgumentException("Invalid Descriptor"));
096: }
097: }
098:
099: private boolean isDescriptorValid(Descriptor descriptor) {
100: if (!descriptor.isValid()) {
101: return false;
102: }
103:
104: // Spec compliance checks
105:
106: // Mandatory fields are: name, descriptorType
107: String[] names = descriptor.getFieldNames();
108:
109: if (!ModelMBeanInfoSupport.containsIgnoreCase(names, "name")
110: || !ModelMBeanInfoSupport.containsIgnoreCase(names,
111: "descriptortype")) {
112: return false;
113: }
114:
115: // Case sensitive name
116: String name = getName();
117: if (name == null) {
118: return false;
119: }
120: if (!name.equals(descriptor.getFieldValue("name"))) {
121: return false;
122: }
123: // Descriptor type must be 'attribute'
124: String desctype = (String) descriptor
125: .getFieldValue("descriptortype");
126: if (desctype.compareToIgnoreCase("attribute") != 0)
127: return false;
128:
129: return true;
130: }
131:
132: private Descriptor createDefaultDescriptor() {
133: String[] names = new String[] { "name", "descriptorType",
134: "displayName" };
135: Object[] values = new Object[] { getName(), "attribute",
136: getName() };
137: return new DescriptorSupport(names, values);
138: }
139: }
|