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.MBeanOperationInfo;
012: import javax.management.DescriptorAccess;
013: import javax.management.Descriptor;
014: import javax.management.RuntimeOperationsException;
015: import javax.management.MBeanParameterInfo;
016:
017: /**
018: * The ModelMBeanOperationInfo object describes a management operation of the ModelMBean.
019: * It is a subclass of MBeanOperationInfo 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:
024: * name : operation name
025: * descriptorType : must be "operation"
026: * class : class where method is defined (fully qualified)
027: * role : getter, setter, operation, constructor
028: * targetObject : object on which to execute this method
029: * targetType : type of object reference for targetObject:
030: * ObjectReference|EJBHandle|IOR|RMIReference
031: * lastReturnedValue : cached value for operation
032: * currencyTimeLimit : how long cached value is valid
033: * lastReturnedTimeStamp : when cached value was set
034: * visibility : 1-4 where 1: always visible 4: rarely visible
035: * presentationString : xml formatted string to describe how to present operation
036: * </PRE>
037: * The default descriptor will have name, descriptorType, and role fields set.
038: *
039: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
040: */
041:
042: public class ModelMBeanOperationInfo extends MBeanOperationInfo
043: implements DescriptorAccess {
044:
045: private Descriptor operationDescriptor;
046:
047: /**
048: * Constructs a ModelMBeanOperationInfo object with a default descriptor.
049: *
050: * @param method The java.lang.reflect.Method object describing the MBean operation.
051: * @param description A human readable description of the operation.
052: */
053: public ModelMBeanOperationInfo(String description, Method method) {
054: super (description, method);
055: operationDescriptor = createDefaultDescriptor();
056: }
057:
058: /**
059: * Constructs a ModelMBeanOperationInfo object.
060: *
061: * @param method The java.lang.reflect.Method object describing the MBean operation.
062: * @param description A human readable description of the operation.
063: * @param descriptor An instance of Descriptor containing the appropriate metadata
064: * for this instance of the MBeanNotificationInfo.If it is null or invalid then
065: * a default desriptor will be created.
066: */
067: public ModelMBeanOperationInfo(String description, Method method,
068: Descriptor descriptor) {
069: super (description, method);
070: setDescriptor(descriptor);
071: }
072:
073: /**
074: * Constructs a ModelMBeanOperationInfo object with a default descriptor.
075: *
076: * @param name The name of the method.
077: * @param description A human readable description of the operation.
078: * @param signature MBeanParameterInfo objects describing the parameters(arguments) of the method.
079: * @param type The type of the method's return value.
080: * @param impact The impact of the method, one of INFO, ACTION, ACTION_INFO, UNKNOWN.
081: */
082: public ModelMBeanOperationInfo(String name, String description,
083: MBeanParameterInfo[] signature, String type, int impact) {
084: super (name, description, signature, type, impact);
085: operationDescriptor = createDefaultDescriptor();
086: }
087:
088: /**
089: * Constructs a ModelMBeanOperationInfo object.
090: *
091: * @param name The name of the method.
092: * @param description A human readable description of the operation.
093: * @param signature MBeanParameterInfo objects describing the parameters(arguments) of the method.
094: * @param type The type of the method's return value.
095: * @param impact The impact of the method, one of INFO, ACTION, ACTION_INFO, UNKNOWN.
096: * @param descriptor An instance of Descriptor containing the appropriate metadata
097: * for this instance of the MBeanOperationInfo.If it is null or invalid then
098: * a default desriptor will be created.
099: */
100: public ModelMBeanOperationInfo(String name, String description,
101: MBeanParameterInfo[] signature, String type, int impact,
102: Descriptor descriptor) {
103: super (name, description, signature, type, impact);
104: setDescriptor(descriptor);
105: }
106:
107: /**
108: * Constructs a new ModelMBeanOperationInfo object from this ModelMBeanOperation Object.
109: *
110: * @param operInfo the ModelMBeanOperationInfo to be duplicated
111: *
112: */
113: public ModelMBeanOperationInfo(ModelMBeanOperationInfo operInfo) {
114: super (operInfo.getName(), operInfo.getDescription(), operInfo
115: .getSignature(), operInfo.getReturnType(), operInfo
116: .getImpact());
117: operationDescriptor = operInfo.getDescriptor();
118: }
119:
120: /**
121: * Creates and returns a new ModelMBeanOperationInfo which is a duplicate of this ModelMBeanOperationInfo.
122: *
123: */
124: public Object clone() {
125: return new ModelMBeanOperationInfo(this );
126: }
127:
128: /**
129: * Returns a copy of the associated Descriptor of the ModelMBeanOperationInfo
130: *
131: * @return Descriptor associated with the ModelMBeanOperationInfo object.
132: */
133: public Descriptor getDescriptor() {
134: return (Descriptor) operationDescriptor.clone();
135: }
136:
137: /**
138: * Sets associated Descriptor (full replace) for the ModelMBeanOperationInfo
139: * If the new Descriptor is null, then the associated Descriptor reverts to
140: * a default descriptor. The Descriptor is validated before it is assigned. If
141: * the new Descriptor is invalid, then an IllegalArgumentException is thrown.
142: *
143: * @param descriptor - replaces the Descriptor associated with the
144: * ModelMBeanOperation.
145: *
146: */
147: public void setDescriptor(Descriptor descriptor) {
148: if (descriptor == null) {
149: operationDescriptor = createDefaultDescriptor();
150: } else if (isValid(descriptor))
151: operationDescriptor = (Descriptor) descriptor.clone();
152: else
153: throw new RuntimeOperationsException(
154: new IllegalArgumentException(
155: "Invalid descriptor passed in parameter"),
156: "Exception occured in ModelMBeanOperationInfo setDescriptor");
157: }
158:
159: /**
160: * Returns a string containing the entire contents of the ModelMBeanOperationInfo in human readable form.
161: */
162: public String toString() {
163: String desc = new String("ModelMBeanOperationInfo: "
164: + getName() + " ; Description: " + getDescription()
165: + " ; Descriptor: " + getDescriptor()
166: + " ; ReturnType: " + getReturnType()
167: + " ; Signature: ");
168: MBeanParameterInfo[] signatures = getSignature();
169: for (int i = 0; i < signatures.length; i++)
170: desc += signatures[i].getType() + ", ";
171:
172: return desc;
173: }
174:
175: /**
176: * Creates default descriptor for operation as follows:
177: * descriptorType=operation,class=this.getClass,role=operation,
178: * name=this.getNameSpace(),displayname=this.getNameSpace(),persistPolicy=never,visibility=1
179: */
180: private Descriptor createDefaultDescriptor() {
181: return new DescriptorSupport(new String[] {
182: DescriptorConstants.DESCRIPTORTYPE + "="
183: + DescriptorConstants.OPERATION_TYPE,
184: DescriptorConstants.NAME + "=" + getName(),
185: DescriptorConstants.OPERATION_ROLE + "="
186: + DescriptorConstants.OPERATION_ROLE_OPERATION,
187: DescriptorConstants.DISPLAYNAME + "=" + getName() });
188: }
189:
190: /**
191: * Tests that the descriptor is valid and adds appropriate default fields not already
192: * specified. Field values must be correct for field names.
193: * Descriptor must have the same name as the operation,the descriptorType field must
194: * be "operation"
195: * The following fields will be defaulted if they are not already set:
196: * role=operation,displayName=this.getNameSpace(),persistPolicy=never,visibility=1
197: */
198: private boolean isValid(Descriptor descriptor) {
199: boolean isValid = true;
200: if (descriptor == null)
201: isValid = false;
202: else if (!descriptor.isValid()) {
203: isValid = false;
204: } else {
205: if (!((String) descriptor.getFieldValue("name"))
206: .equalsIgnoreCase(getName()))
207: isValid = false;
208: if (!((String) descriptor.getFieldValue("descriptorType"))
209: .equalsIgnoreCase("operation"))
210: isValid = false;
211: Object obj = descriptor.getFieldValue("role");
212: if (obj == null)
213: descriptor.setField("role", "operation");
214: else if (!((String) obj).equalsIgnoreCase("operation")
215: && !((String) obj).equalsIgnoreCase("getter")
216: && !((String) obj).equalsIgnoreCase("setter")) {
217: isValid = false;
218: }
219: Object targetValue = descriptor.getFieldValue("targetType");
220: if (targetValue != null)
221: if (!(targetValue instanceof String)) {
222: isValid = false;
223: } else if (!((String) targetValue)
224: .equalsIgnoreCase("ObjectReference")
225: && !((String) targetValue)
226: .equalsIgnoreCase("Handle")
227: && !((String) targetValue)
228: .equalsIgnoreCase("EJBHandle")
229: && !((String) targetValue)
230: .equalsIgnoreCase("IOR")
231: && !((String) targetValue)
232: .equalsIgnoreCase("RMIReference")) {
233: isValid = false;
234: }
235: if (descriptor.getFieldValue("displayName") == null)
236: descriptor.setField("displayName", getName());
237: }
238: return isValid;
239: }
240: }
|