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.Constructor;
012: import javax.management.Descriptor;
013: import javax.management.DescriptorAccess;
014: import javax.management.MBeanConstructorInfo;
015: import javax.management.MBeanParameterInfo;
016: import javax.management.RuntimeOperationsException;
017:
018: /**
019: * @version $Revision: 1.14 $
020: */
021: // Change not needed, workaround to a TCK bug only to achieve TCK compliance
022: // public class ModelMBeanConstructorInfo extends MBeanConstructorInfo implements DescriptorAccess
023: public class ModelMBeanConstructorInfo extends MBeanConstructorInfo
024: implements DescriptorAccess, Cloneable {
025: private static final long serialVersionUID = 3862947819818064362L;
026:
027: private Descriptor consDescriptor;
028:
029: public ModelMBeanConstructorInfo(String description,
030: Constructor constructor) {
031: this (description, constructor, null);
032: }
033:
034: public ModelMBeanConstructorInfo(String description,
035: Constructor constructor, Descriptor descriptor) {
036: super (description, constructor);
037: checkAndSetDescriptor(descriptor);
038: }
039:
040: public ModelMBeanConstructorInfo(String name, String description,
041: MBeanParameterInfo[] params) {
042: this (name, description, params, null);
043: }
044:
045: public ModelMBeanConstructorInfo(String name, String description,
046: MBeanParameterInfo[] params, Descriptor descriptor) {
047: super (name, description, params);
048: checkAndSetDescriptor(descriptor);
049: }
050:
051: ModelMBeanConstructorInfo(ModelMBeanConstructorInfo copy) {
052: super (copy.getName(), copy.getDescription(), copy
053: .getSignature());
054: checkAndSetDescriptor(copy.getDescriptor());
055: }
056:
057: public Object clone() {
058: return new ModelMBeanConstructorInfo(this );
059: }
060:
061: public Descriptor getDescriptor() {
062: return (Descriptor) consDescriptor.clone();
063: }
064:
065: public void setDescriptor(Descriptor descriptor) {
066: if (descriptor == null) {
067: consDescriptor = createDefaultDescriptor();
068: } else {
069: if (isDescriptorValid(descriptor)) {
070: consDescriptor = (Descriptor) descriptor.clone();
071: } else {
072: // Not sure what to do here: javadoc says IllegalArgument, but for example ModelMBeanInfo throws RuntimeOperations
073: // which is consistent with the fact that all exception thrown by the JMX implementation should be JMX exceptions
074: throw new RuntimeOperationsException(
075: new IllegalArgumentException(
076: "Invalid descriptor"));
077: }
078: }
079: }
080:
081: private void checkAndSetDescriptor(Descriptor descriptor) {
082: if (descriptor == null) {
083: consDescriptor = createDefaultDescriptor();
084: } else if (isDescriptorValid(descriptor)) {
085: consDescriptor = (Descriptor) descriptor.clone();
086: if (consDescriptor.getFieldValue("displayName") == null) {
087: consDescriptor.setField("displayName", getName());
088: }
089: } else {
090: throw new RuntimeOperationsException(
091: new IllegalArgumentException("Invalid descriptor"));
092: }
093: }
094:
095: private boolean isDescriptorValid(Descriptor descriptor) {
096: if (!descriptor.isValid()) {
097: return false;
098: }
099:
100: // Spec compliance checks
101:
102: // Mandatory fields are: name, descriptorType, role
103: String[] names = descriptor.getFieldNames();
104:
105: if (!ModelMBeanInfoSupport.containsIgnoreCase(names, "name")
106: || !ModelMBeanInfoSupport.containsIgnoreCase(names,
107: "descriptortype")
108: || !ModelMBeanInfoSupport.containsIgnoreCase(names,
109: "role")) {
110: return false;
111: }
112:
113: if (ModelMBeanInfoSupport.containsIgnoreCase(names,
114: "persistpolicy")
115: || ModelMBeanInfoSupport.containsIgnoreCase(names,
116: "currencytimelimit")) {
117: return false;
118: }
119:
120: // Case sensitive name
121: String name = getName();
122: if (name == null) {
123: return false;
124: }
125: if (!name.equals(descriptor.getFieldValue("name"))) {
126: return false;
127: }
128: // Descriptor type must be 'operation'
129: String desctype = (String) descriptor
130: .getFieldValue("descriptortype");
131: if (desctype.compareToIgnoreCase("operation") != 0)
132: return false;
133: // Role must be 'constructor'
134: String role = (String) descriptor.getFieldValue("role");
135: if (role.compareTo("constructor") != 0)
136: return false;
137:
138: return true;
139: }
140:
141: private Descriptor createDefaultDescriptor() {
142: String[] names = new String[] { "name", "descriptorType",
143: "role", "displayName"/*, "lastReturnedTimeStamp"*/};
144: Object[] values = new Object[] { getName(), "operation",
145: "constructor", getName() /*, "0"*/};
146: return new DescriptorSupport(names, values);
147: }
148: }
|