View Javadoc

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