001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package javax.management.modelmbean;
009:
010: import java.lang.reflect.Constructor;
011: import javax.management.Descriptor;
012: import javax.management.MBeanParameterInfo;
013: import javax.management.RuntimeOperationsException;
014: import javax.management.DescriptorAccess;
015: import javax.management.MBeanConstructorInfo;
016:
017: /**
018: * The ModelMBeanConstructorInfo object describes a constructor of the ModelMBean.
019: * It is a subclass of MBeanConstructorInfo with the addition of an associated Descriptor
020: * and an implementation of the DescriptorAccess interface.
021: * <P>
022: * <PRE>
023: * The fields in the descriptor are defined, but not limited to, the following: <P>
024: * name : operation name
025: * descriptorType : must be "operation"
026: * role : must be "constructor"
027: * displayName : human readable name of constructor
028: * class : class where method is defined (fully qualified)
029: * visibility : 1-4 where 1: always visible 4: rarely visible
030: * presentationString : xml formatted string to describe how to present operation
031: *</PRE>
032: * PersistencePolicy and CurrencyTimeLimit fields are not valid for the constructor.
033: * The default constructo will have the name, descriptorType, and role fields.
034: *
035: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
036: */
037:
038: public class ModelMBeanConstructorInfo extends MBeanConstructorInfo
039: implements DescriptorAccess, Cloneable {
040:
041: private Descriptor consDescriptor;
042:
043: /**
044: * Constructs a MBeanConstructorInfo object with a default descriptor.
045: *
046: * @param description A human readable description of the constructor.
047: * @param constructor The java.lang.reflect.Method object describing the MBean constructor.
048: */
049: public ModelMBeanConstructorInfo(String description,
050: Constructor constructor) {
051: super (description, constructor);
052: consDescriptor = createDefaultDescriptor();
053: }
054:
055: /**
056: * Constructs a MBeanConstructorInfo object.
057: *
058: * @param description A human readable description of the constructor.
059: * @param constructor The java.lang.reflect.Method object describing the ModelMBean constructor.
060: * @param descriptor An instance of Descriptor containing the appropriate metadata
061: * for this instance of the ModelMBeanConstructorInfo. If it is null or invalid then
062: * a default desriptor will be created.
063: */
064: public ModelMBeanConstructorInfo(String description,
065: Constructor constructor, Descriptor descriptor) {
066: super (description, constructor);
067: setDescriptor(descriptor);
068: }
069:
070: /**
071: * Constructs a ModelMBeanConstructorInfo object with a default descriptor.
072: *
073: * @param name The name of the constructor.
074: * @param description A human readable description of the constructor.
075: * @param signature MBeanParameterInfo object array describing the parameters(arguments) of the constructor.
076: */
077: public ModelMBeanConstructorInfo(String name, String description,
078: MBeanParameterInfo[] signature) {
079: super (name, description, signature);
080: consDescriptor = createDefaultDescriptor();
081: }
082:
083: /**
084: * Constructs a MBeanConstructorInfo object.
085: *
086: * @param name The name of the constructor.
087: * @param description A human readable description of the constructor.
088: * @param signature MBeanParameterInfo objects describing the parameters(arguments) of the constructor.
089: * @param descriptor An instance of Descriptor containing the appropriate metadata
090: * for this instance of the MBeanConstructorInfo. If it is null or invalid then
091: * a default desriptor will be created.
092: */
093: public ModelMBeanConstructorInfo(String name, String description,
094: MBeanParameterInfo[] signature, Descriptor descriptor) {
095: super (name, description, signature);
096: setDescriptor(descriptor);
097: }
098:
099: /**
100: * Constructs a new ModelMBeanConstructorInfo object from this ModelMBeanConstructor Object.
101: *
102: * @param constrInfo the ModelMBeanConstructorInfo to be duplicated
103: *
104: */
105: ModelMBeanConstructorInfo(ModelMBeanConstructorInfo constrInfo) {
106: super (constrInfo.getName(), constrInfo.getDescription(),
107: constrInfo.getSignature());
108: consDescriptor = constrInfo.getDescriptor();
109: }
110:
111: /**
112: * Creates and returns a new ModelMBeanConstructorInfo which is a duplicate of this ModelMBeanConstructorInfo.
113: *
114: */
115: public Object clone() {
116: return new ModelMBeanConstructorInfo(this );
117: }
118:
119: /**
120: * Returns a copy of the associated Descriptor
121: *
122: * @return Descriptor associated with the ModelMBeanConstructorInfo object.
123: */
124: public Descriptor getDescriptor() {
125: return (Descriptor) consDescriptor.clone();
126: }
127:
128: /**
129: * Sets associated Descriptor (full replace) of ModelMBeanConstructorInfo.
130: * If the new Descriptor is null, then the associated Descriptor reverts to
131: * a default descriptor. The Descriptor is validated before it is assigned. If
132: * the new Descriptor is invalid, then an IllegalArgumentException is thrown.
133: *
134: * @param descriptor - replaces the Descriptor associated with the
135: * ModelMBeanConstructor.
136: *
137: */
138: public void setDescriptor(Descriptor descriptor) {
139: // System.out.println("ModelMbeanConstructorInfo:61 " + descriptor.getFieldValue(DescriptorConstants.NAME) + " " +getNameSpace());
140: if (descriptor == null) {
141: consDescriptor = createDefaultDescriptor();
142: } else if (isValid(descriptor))
143: consDescriptor = (Descriptor) descriptor.clone();
144: else
145: throw new RuntimeOperationsException(
146: new IllegalArgumentException(
147: "Invalid descriptor passed in parameter"),
148: "Exception occured in ModelMBeanConstructorInfo setDescriptor");
149: }
150:
151: /**
152: * Returns a string containing the entire contents of the ModelMBeanConstructorInfo in human readable form.
153: */
154: public String toString() {
155: String desc = new String("ModelMBeanConstructorInfo: "
156: + getName() + " ; Description: " + getDescription()
157: + " ; Descriptor: " + getDescriptor()
158: + " ; Signature: ");
159: MBeanParameterInfo[] signatures = getSignature();
160: for (int i = 0; i < signatures.length; i++) {
161: desc += desc.concat(signatures[i].getType() + ", ");
162: }
163:
164: return desc;
165: }
166:
167: /**
168: * Creates default descriptor for constructor as follows:
169: * descriptorType=operation,role=constructor,
170: * name=this.getNameSpace(),displayname=this.getNameSpace(),visibility=1
171: */
172: private Descriptor createDefaultDescriptor() {
173: return new DescriptorSupport(
174: new String[] {
175: DescriptorConstants.DESCRIPTORTYPE + "="
176: + DescriptorConstants.OPERATION_TYPE,
177: DescriptorConstants.OPERATION_ROLE
178: + "="
179: + DescriptorConstants.OPERATION_ROLE_CONSTRUCTOR,
180: DescriptorConstants.NAME + "=" + getName(),
181: DescriptorConstants.DISPLAYNAME + "="
182: + getName() });
183: }
184:
185: /**
186: * Tests that the descriptor is valid and adds appropriate default fields not already
187: * specified. Field values must be correct for field names.
188: * Descriptor must have the same name as the operation,the descriptorType field must
189: * be "operation", the role field must be set to "constructor".
190: * The following fields will be defaulted if they are not already set:
191: * displayName=this.getNameSpace(),persistPolicy=never,visibility=1
192: */
193: private boolean isValid(Descriptor descriptor) {
194: boolean isValid = true;
195: if (descriptor == null) {
196: isValid = false;
197: } else if (!descriptor.isValid()) {
198: isValid = false;
199: } else {
200: if (!((String) descriptor
201: .getFieldValue(DescriptorConstants.NAME))
202: .equalsIgnoreCase(getName())) {
203: isValid = false;
204: }
205: if (!((String) descriptor
206: .getFieldValue(DescriptorConstants.DESCRIPTORTYPE))
207: .equalsIgnoreCase(DescriptorConstants.OPERATION_TYPE)) {
208: isValid = false;
209: }
210: if (descriptor
211: .getFieldValue(DescriptorConstants.OPERATION_ROLE) == null)
212: descriptor.setField(DescriptorConstants.OPERATION_ROLE,
213: DescriptorConstants.OPERATION_ROLE_CONSTRUCTOR);
214: if (!((String) descriptor
215: .getFieldValue(DescriptorConstants.OPERATION_ROLE))
216: .equalsIgnoreCase(DescriptorConstants.OPERATION_ROLE_CONSTRUCTOR)) {
217: isValid = false;
218: } else if (descriptor
219: .getFieldValue(DescriptorConstants.DISPLAYNAME) == null)
220: descriptor.setField(DescriptorConstants.DISPLAYNAME,
221: getName());
222: }
223: return isValid;
224: }
225:
226: }
|