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.Method;
011: import javax.management.RuntimeOperationsException;
012: import javax.management.Descriptor;
013: import javax.management.IntrospectionException;
014: import javax.management.DescriptorAccess;
015: import javax.management.MBeanAttributeInfo;
016:
017: /**
018: * The ModelMBeanAttributeInfo object describes an attribute of the ModelMBean.
019: * It is a subclass of MBeanAttributeInfo with the addition of an associated Descriptor
020: * and an implementation of the DescriptorAccess interface.
021: * <P>
022: * The fields in the descriptor are defined, but not limited to, the following: <P>
023: * <PRE>
024: * name : attribute name
025: * descriptorType : must be "attribute"
026: * value : current value for attribute
027: * default : default value for attribute
028: * displayName : name of attribute to be used in displays
029: * getMethod : name of operation descriptor for get method
030: * setMethod : name of operation descriptor for set method
031: * protocolMap : object which implements the ProtocolMap interface: map of protocol names and protocol hints
032: * persistPolicy : Update|OnTime|NoMoreOftenThan|Always|Never
033: * persistPeriod : seconds - frequency of persist cycle. Used when persistPolicy is"OnTime" or "NoMoreOftenThan".
034: * currencyTimeLimit : how long value is valid, <0 never, =0 always, >0 seconds
035: * lastUpdatedTimeStamp : when value was set
036: * iterable : T - object value supports Iterable interface, F - does not support Iterable interface
037: * visibility : 1-4 where 1: always visible 4: rarely visible
038: * presentationString : xml formatted string to allow presentation of data
039: * </PRE>
040: * The default descriptor contains the name and descriptorType fields.
041: *
042: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
043: */
044:
045: public class ModelMBeanAttributeInfo extends MBeanAttributeInfo
046: implements DescriptorAccess, Cloneable {
047:
048: private Descriptor attrDescriptor;
049:
050: /**
051: * Constructs a ModelMBeanAttributeInfo object with a default descriptor.
052: *
053: * @param name The name of the attribute.
054: * @param description A human readable description of the attribute. Optional.
055: * @param getter The method used for reading the attribute value.
056: * May be null if the property is write-only.
057: * @param setter The method used for writing the attribute value.
058: * May be null if the attribute is read-only.
059: * @exception IntrospectionException There is a consistency problem in the definition of this attribute.
060: *
061: * The descriptor must be set before this
062: * ModelMBeanAttributeInfo instance can be used.
063: */
064: public ModelMBeanAttributeInfo(String name, String description,
065: Method getter, Method setter) throws IntrospectionException {
066: super (name, description, getter, setter);
067: attrDescriptor = createDefaultDescriptor();
068: }
069:
070: /**
071: * Constructs a ModelMBeanAttributeInfo object.
072: *
073: * @param name The name of the attribute.
074: * @param description A human readable description of the attribute. Optional.
075: * @param getter The method used for reading the attribute value.
076: * May be null if the property is write-only.
077: * @param setter The method used for writing the attribute value.
078: * May be null if the attribute is read-only.
079: * @param descriptor An instance of Descriptor containing the appropriate metadata
080: * for this instance of the Attribute. If it is null or invalid then
081: * a default desriptor will be created.
082: *
083: * @exception IntrospectionException There is a consistency problem in the definition of this attribute.
084: *
085: * The descriptor must be set before this
086: * ModelMBeanAttributeInfo instance can be used.
087: */
088: public ModelMBeanAttributeInfo(String name, String description,
089: Method getter, Method setter, Descriptor descriptor)
090: throws IntrospectionException {
091: super (name, description, getter, setter);
092: setDescriptor(descriptor);
093: }
094:
095: /**
096: * Constructs a ModelMBeanAttributeInfo object with a default descriptor.
097: *
098: * @param name The name of the attribute
099: * @param type The type or class name of the attribute
100: * @param description A human readable description of the attribute.
101: * @param isReadable True if the attribute has a getter method, false otherwise.
102: * @param isWritable True if the attribute has a setter method, false otherwise.
103: * @param isIs True if the attribute has an "is" getter, false otherwise.
104: *
105: */
106: public ModelMBeanAttributeInfo(String name, String type,
107: String description, boolean isReadable, boolean isWritable,
108: boolean isIs) {
109: super (name, type, description, isReadable, isWritable, isIs);
110: attrDescriptor = createDefaultDescriptor();
111: }
112:
113: /**
114: * Constructs a ModelMBeanAttributeInfo object with a default descriptor.
115: *
116: * @param name The name of the attribute
117: * @param type The type or class name of the attribute
118: * @param description A human readable description of the attribute.
119: * @param isReadable True if the attribute has a getter method, false otherwise.
120: * @param isWritable True if the attribute has a setter method, false otherwise.
121: * @param isIs True if the attribute has an "is" getter, false otherwise.
122: * @param descriptor An instance of Descriptor containing the appropriate metadata
123: * for this instance of the Attribute. If it is null or invalid then
124: * a default desriptor will be created.
125: *
126: */
127: public ModelMBeanAttributeInfo(String name, String type,
128: String description, boolean isReadable, boolean isWritable,
129: boolean isIs, Descriptor descriptor) {
130: super (name, type, description, isReadable, isWritable, isIs);
131: setDescriptor(descriptor);
132: }
133:
134: /**
135: * Constructs a new ModelMBeanAttributeInfo object from this ModelMBeanAttributeInfo Object.
136: * A default descriptor will be created.
137: *
138: * @param attributeInfo the ModelMBeanAttributeInfo to be duplicated
139: */
140: public ModelMBeanAttributeInfo(ModelMBeanAttributeInfo attributeInfo) {
141: super (attributeInfo.getName(), attributeInfo.getType(),
142: attributeInfo.getDescription(), attributeInfo
143: .isReadable(), attributeInfo.isWritable(),
144: attributeInfo.isIs());
145: Descriptor descriptor = attributeInfo.getDescriptor();
146: setDescriptor(descriptor);
147: }
148:
149: /**
150: * Gets a copy of the associated Descriptor for the ModelMBeanAttributeInfo
151: *
152: * @return Descriptor associated with the ModelMBeanAttributeInfo object.
153: */
154: public Descriptor getDescriptor() {
155: return (Descriptor) attrDescriptor.clone();
156: }
157:
158: /**
159: * Sets associated Descriptor (full replace) for the ModelMBeanAttributeDescriptor.
160: * If the new Descriptor is null, then the associated Descriptor reverts to
161: * a default descriptor. The Descriptor is validated before it is assigned. If
162: * the new Descriptor is invalid, then an IllegalArgumentException is thrown.
163: *
164: * @param descriptor - replaces the Descriptor associated with the
165: * ModelMBeanAttributeInfo
166: *
167: */
168: public void setDescriptor(Descriptor descriptor) {
169: if (descriptor == null) {
170: attrDescriptor = createDefaultDescriptor();
171: } else if (isValid(descriptor))
172: attrDescriptor = (Descriptor) descriptor.clone();
173: else
174: throw new RuntimeOperationsException(
175: new IllegalArgumentException(
176: "Invalid descriptor passed in parameter"),
177: "Exception occured in ModelMBeanAttributeInfo setDescriptor");
178: }
179:
180: /**
181: * Creates and returns a new ModelMBeanAttributeInfo which is a duplicate of this ModelMBeanAttributeInfo.
182: *
183: * @exception RuntimeOperationsException for illegal value for field URLName or field Values.
184: * If the descriptor construction fails for any reason, this exception will be thrown.
185: */
186: public Object clone() {
187: return new ModelMBeanAttributeInfo(this );
188: }
189:
190: /**
191: * Returns a human readable version of the ModelMBeanAttributeInfo instance
192: */
193: public String toString() {
194: return "ModelMBeanAttributeInfo: " + getName()
195: + " ; Description: " + getDescription() + " ; Types: "
196: + getType() + " ; isReadable: " + isReadable()
197: + " ; isWritable: " + isWritable() + " ; Descriptor: "
198: + getDescriptor();
199: }
200:
201: /**
202: * Creates and returns a Descriptor with default values set:
203: * descriptorType=attribute,name=this.getNameSpace(),displayName=this.getNameSpace(),
204: * persistPolicy=never,visibility=1,iterable=F
205: */
206: private Descriptor createDefaultDescriptor() {
207: return new DescriptorSupport(new String[] {
208: DescriptorConstants.DESCRIPTORTYPE + "="
209: + DescriptorConstants.ATTRIBUTE_TYPE,
210: DescriptorConstants.NAME + "=" + getName(),
211: DescriptorConstants.DISPLAYNAME + "=" + getName(),
212: DescriptorConstants.ATTRIBUTE_ITERABLE + "="
213: + DescriptorConstants.FALSE });
214: }
215:
216: /**
217: * Tests that the descriptor is valid and adds appropriate default fields not already
218: * specified. Field values must be correct for field names.
219: * Descriptor must have the same name as the attribute,the descriptorType field must be "attribute",
220: * The following fields will be defaulted if they are not already set:
221: * displayName=this.getNameSpace(),persistPolicy=never,visibility=1,iterable=F
222: */
223: private boolean isValid(Descriptor descriptor) {
224: boolean isValid = true;
225: if (descriptor == null) {
226: isValid = false;
227: } else if (!descriptor.isValid()) {
228: isValid = false;
229: } else if (!((String) descriptor
230: .getFieldValue(DescriptorConstants.NAME))
231: .equalsIgnoreCase(getName())) {
232: isValid = false;
233: } else if (!((String) descriptor
234: .getFieldValue(DescriptorConstants.DESCRIPTORTYPE))
235: .equalsIgnoreCase(DescriptorConstants.ATTRIBUTE_TYPE)) {
236: isValid = false;
237: } else if (descriptor
238: .getFieldValue(DescriptorConstants.DISPLAYNAME) == null)
239: descriptor.setField(DescriptorConstants.DISPLAYNAME,
240: getName());
241: else if (descriptor
242: .getFieldValue(DescriptorConstants.ATTRIBUTE_ITERABLE) == null)
243: descriptor.setField(DescriptorConstants.ATTRIBUTE_ITERABLE,
244: DescriptorConstants.FALSE);
245:
246: return isValid;
247: }
248:
249: }
|