001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.modeler;
019:
020: import java.io.Serializable;
021:
022: import javax.management.Descriptor;
023: import javax.management.MBeanParameterInfo;
024: import javax.management.modelmbean.ModelMBeanOperationInfo;
025:
026: /**
027: * <p>Internal configuration information for an <code>Operation</code>
028: * descriptor.</p>
029: *
030: * @author Craig R. McClanahan
031: * @version $Revision: 480402 $ $Date: 2006-11-29 04:43:23 +0000 (Wed, 29 Nov 2006) $
032: */
033:
034: public class OperationInfo extends FeatureInfo implements Serializable {
035: static final long serialVersionUID = 4418342922072614875L;
036:
037: // ----------------------------------------------------------- Constructors
038:
039: /**
040: * Standard zero-arguments constructor.
041: */
042: public OperationInfo() {
043:
044: super ();
045:
046: }
047:
048: /**
049: * Special constructor for setting up getter and setter operations.
050: *
051: * @param name Name of this operation
052: * @param getter Is this a getter (as opposed to a setter)?
053: * @param type Data type of the return value (if this is a getter)
054: * or the parameter (if this is a setter)
055: *
056: */
057: public OperationInfo(String name, boolean getter, String type) {
058:
059: super ();
060: setName(name);
061: if (getter) {
062: setDescription("Attribute getter method");
063: setImpact("INFO");
064: setReturnType(type);
065: setRole("getter");
066: } else {
067: setDescription("Attribute setter method");
068: setImpact("ACTION");
069: setReturnType("void");
070: setRole("setter");
071: addParameter(new ParameterInfo("value", type,
072: "New attribute value"));
073: }
074:
075: }
076:
077: // ----------------------------------------------------- Instance Variables
078:
079: /**
080: * The <code>ModelMBeanOperationInfo</code> object that corresponds
081: * to this <code>OperationInfo</code> instance.
082: */
083: transient ModelMBeanOperationInfo info = null;
084: protected String impact = "UNKNOWN";
085: protected String role = "operation";
086: protected String returnType = "void"; // FIXME - Validate
087: protected ParameterInfo parameters[] = new ParameterInfo[0];
088:
089: // ------------------------------------------------------------- Properties
090:
091: /**
092: * Override the <code>description</code> property setter.
093: *
094: * @param description The new description
095: */
096: public void setDescription(String description) {
097: super .setDescription(description);
098: this .info = null;
099: }
100:
101: /**
102: * Override the <code>name</code> property setter.
103: *
104: * @param name The new name
105: */
106: public void setName(String name) {
107: super .setName(name);
108: this .info = null;
109: }
110:
111: /**
112: * The "impact" of this operation, which should be a (case-insensitive)
113: * string value "ACTION", "ACTION_INFO", "INFO", or "UNKNOWN".
114: */
115: public String getImpact() {
116: return (this .impact);
117: }
118:
119: public void setImpact(String impact) {
120: if (impact == null)
121: this .impact = null;
122: else
123: this .impact = impact.toUpperCase();
124: }
125:
126: /**
127: * The role of this operation ("getter", "setter", "operation", or
128: * "constructor").
129: */
130: public String getRole() {
131: return (this .role);
132: }
133:
134: public void setRole(String role) {
135: this .role = role;
136: }
137:
138: /**
139: * The fully qualified Java class name of the return type for this
140: * operation.
141: */
142: public String getReturnType() {
143: return (this .returnType);
144: }
145:
146: public void setReturnType(String returnType) {
147: this .returnType = returnType;
148: }
149:
150: /**
151: * The set of parameters for this operation.
152: */
153: public ParameterInfo[] getSignature() {
154: return (this .parameters);
155: }
156:
157: // --------------------------------------------------------- Public Methods
158:
159: /**
160: * Add a new parameter to the set of arguments for this operation.
161: *
162: * @param parameter The new parameter descriptor
163: */
164: public void addParameter(ParameterInfo parameter) {
165:
166: synchronized (parameters) {
167: ParameterInfo results[] = new ParameterInfo[parameters.length + 1];
168: System.arraycopy(parameters, 0, results, 0,
169: parameters.length);
170: results[parameters.length] = parameter;
171: parameters = results;
172: this .info = null;
173: }
174:
175: }
176:
177: /**
178: * Create and return a <code>ModelMBeanOperationInfo</code> object that
179: * corresponds to the attribute described by this instance.
180: */
181: public ModelMBeanOperationInfo createOperationInfo() {
182:
183: // Return our cached information (if any)
184: if (info != null)
185: return (info);
186:
187: // Create and return a new information object
188: ParameterInfo params[] = getSignature();
189: MBeanParameterInfo parameters[] = new MBeanParameterInfo[params.length];
190: for (int i = 0; i < params.length; i++)
191: parameters[i] = params[i].createParameterInfo();
192: int impact = ModelMBeanOperationInfo.UNKNOWN;
193: if ("ACTION".equals(getImpact()))
194: impact = ModelMBeanOperationInfo.ACTION;
195: else if ("ACTION_INFO".equals(getImpact()))
196: impact = ModelMBeanOperationInfo.ACTION_INFO;
197: else if ("INFO".equals(getImpact()))
198: impact = ModelMBeanOperationInfo.INFO;
199:
200: info = new ModelMBeanOperationInfo(getName(), getDescription(),
201: parameters, getReturnType(), impact);
202: Descriptor descriptor = info.getDescriptor();
203: descriptor.removeField("class");
204: descriptor.setField("role", getRole());
205: addFields(descriptor);
206: info.setDescriptor(descriptor);
207: return (info);
208:
209: }
210:
211: /**
212: * Return a string representation of this operation descriptor.
213: */
214: public String toString() {
215:
216: StringBuffer sb = new StringBuffer("OperationInfo[");
217: sb.append("name=");
218: sb.append(name);
219: sb.append(", description=");
220: sb.append(description);
221: sb.append(", returnType=");
222: sb.append(returnType);
223: sb.append(", parameters=");
224: sb.append(parameters.length);
225: sb.append("]");
226: return (sb.toString());
227:
228: }
229:
230: }
|