View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  
19  package org.apache.commons.modeler;
20  
21  
22  import java.io.Serializable;
23  
24  import javax.management.Descriptor;
25  import javax.management.MBeanParameterInfo;
26  import javax.management.modelmbean.ModelMBeanOperationInfo;
27  
28  
29  /***
30   * <p>Internal configuration information for an <code>Operation</code>
31   * descriptor.</p>
32   *
33   * @author Craig R. McClanahan
34   * @version $Revision: 480402 $ $Date: 2006-11-29 04:43:23 +0000 (Wed, 29 Nov 2006) $
35   */
36  
37  public class OperationInfo extends FeatureInfo implements Serializable {
38      static final long serialVersionUID = 4418342922072614875L;
39      // ----------------------------------------------------------- Constructors
40  
41  
42      /***
43       * Standard zero-arguments constructor.
44       */
45      public OperationInfo() {
46  
47          super();
48  
49      }
50  
51  
52      /***
53       * Special constructor for setting up getter and setter operations.
54       *
55       * @param name Name of this operation
56       * @param getter Is this a getter (as opposed to a setter)?
57       * @param type Data type of the return value (if this is a getter)
58       *  or the parameter (if this is a setter)
59       * 
60       */
61      public OperationInfo(String name, boolean getter, String type) {
62  
63          super();
64          setName(name);
65          if (getter) {
66              setDescription("Attribute getter method");
67              setImpact("INFO");
68              setReturnType(type);
69              setRole("getter");
70          } else {
71              setDescription("Attribute setter method");
72              setImpact("ACTION");
73              setReturnType("void");
74              setRole("setter");
75              addParameter(new ParameterInfo("value", type,
76                                             "New attribute value"));
77          }
78  
79      }
80  
81  
82      // ----------------------------------------------------- Instance Variables
83  
84  
85      /***
86       * The <code>ModelMBeanOperationInfo</code> object that corresponds
87       * to this <code>OperationInfo</code> instance.
88       */
89      transient ModelMBeanOperationInfo info = null;
90      protected String impact = "UNKNOWN";
91      protected String role = "operation";
92      protected String returnType = "void";    // FIXME - Validate
93      protected ParameterInfo parameters[] = new ParameterInfo[0];
94  
95  
96      // ------------------------------------------------------------- Properties
97  
98  
99      /***
100      * Override the <code>description</code> property setter.
101      *
102      * @param description The new description
103      */
104     public void setDescription(String description) {
105         super.setDescription(description);
106         this.info = null;
107     }
108 
109 
110     /***
111      * Override the <code>name</code> property setter.
112      *
113      * @param name The new name
114      */
115     public void setName(String name) {
116         super.setName(name);
117         this.info = null;
118     }
119 
120 
121     /***
122      * The "impact" of this operation, which should be a (case-insensitive)
123      * string value "ACTION", "ACTION_INFO", "INFO", or "UNKNOWN".
124      */
125     public String getImpact() {
126         return (this.impact);
127     }
128 
129     public void setImpact(String impact) {
130         if (impact == null)
131             this.impact = null;
132         else
133             this.impact = impact.toUpperCase();
134     }
135 
136 
137     /***
138      * The role of this operation ("getter", "setter", "operation", or
139      * "constructor").
140      */
141     public String getRole() {
142         return (this.role);
143     }
144 
145     public void setRole(String role) {
146         this.role = role;
147     }
148 
149 
150     /***
151      * The fully qualified Java class name of the return type for this
152      * operation.
153      */
154     public String getReturnType() {
155         return (this.returnType);
156     }
157 
158     public void setReturnType(String returnType) {
159         this.returnType = returnType;
160     }
161 
162     /***
163      * The set of parameters for this operation.
164      */
165     public ParameterInfo[] getSignature() {
166         return (this.parameters);
167     }
168 
169     // --------------------------------------------------------- Public Methods
170 
171 
172     /***
173      * Add a new parameter to the set of arguments for this operation.
174      *
175      * @param parameter The new parameter descriptor
176      */
177     public void addParameter(ParameterInfo parameter) {
178 
179         synchronized (parameters) {
180             ParameterInfo results[] = new ParameterInfo[parameters.length + 1];
181             System.arraycopy(parameters, 0, results, 0, parameters.length);
182             results[parameters.length] = parameter;
183             parameters = results;
184             this.info = null;
185         }
186 
187     }
188 
189 
190     /***
191      * Create and return a <code>ModelMBeanOperationInfo</code> object that
192      * corresponds to the attribute described by this instance.
193      */
194     public ModelMBeanOperationInfo createOperationInfo() {
195 
196         // Return our cached information (if any)
197         if (info != null)
198             return (info);
199 
200         // Create and return a new information object
201         ParameterInfo params[] = getSignature();
202         MBeanParameterInfo parameters[] =
203             new MBeanParameterInfo[params.length];
204         for (int i = 0; i < params.length; i++)
205             parameters[i] = params[i].createParameterInfo();
206         int impact = ModelMBeanOperationInfo.UNKNOWN;
207         if ("ACTION".equals(getImpact()))
208             impact = ModelMBeanOperationInfo.ACTION;
209         else if ("ACTION_INFO".equals(getImpact()))
210             impact = ModelMBeanOperationInfo.ACTION_INFO;
211         else if ("INFO".equals(getImpact()))
212             impact = ModelMBeanOperationInfo.INFO;
213 
214         info = new ModelMBeanOperationInfo
215             (getName(), getDescription(), parameters,
216              getReturnType(), impact);
217         Descriptor descriptor = info.getDescriptor();
218         descriptor.removeField("class");
219         descriptor.setField("role", getRole());
220         addFields(descriptor);
221         info.setDescriptor(descriptor);
222         return (info);
223 
224     }
225 
226 
227     /***
228      * Return a string representation of this operation descriptor.
229      */
230     public String toString() {
231 
232         StringBuffer sb = new StringBuffer("OperationInfo[");
233         sb.append("name=");
234         sb.append(name);
235         sb.append(", description=");
236         sb.append(description);
237         sb.append(", returnType=");
238         sb.append(returnType);
239         sb.append(", parameters=");
240         sb.append(parameters.length);
241         sb.append("]");
242         return (sb.toString());
243 
244     }
245 
246 
247 }