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    
019    package org.apache.commons.modeler;
020    
021    
022    import java.io.Serializable;
023    
024    import javax.management.Descriptor;
025    import javax.management.MBeanParameterInfo;
026    import javax.management.modelmbean.ModelMBeanOperationInfo;
027    
028    
029    /**
030     * <p>Internal configuration information for an <code>Operation</code>
031     * descriptor.</p>
032     *
033     * @author Craig R. McClanahan
034     * @version $Revision: 480402 $ $Date: 2006-11-29 05:43:23 +0100 (Wed, 29 Nov 2006) $
035     */
036    
037    public class OperationInfo extends FeatureInfo implements Serializable {
038        static final long serialVersionUID = 4418342922072614875L;
039        // ----------------------------------------------------------- Constructors
040    
041    
042        /**
043         * Standard zero-arguments constructor.
044         */
045        public OperationInfo() {
046    
047            super();
048    
049        }
050    
051    
052        /**
053         * Special constructor for setting up getter and setter operations.
054         *
055         * @param name Name of this operation
056         * @param getter Is this a getter (as opposed to a setter)?
057         * @param type Data type of the return value (if this is a getter)
058         *  or the parameter (if this is a setter)
059         * 
060         */
061        public OperationInfo(String name, boolean getter, String type) {
062    
063            super();
064            setName(name);
065            if (getter) {
066                setDescription("Attribute getter method");
067                setImpact("INFO");
068                setReturnType(type);
069                setRole("getter");
070            } else {
071                setDescription("Attribute setter method");
072                setImpact("ACTION");
073                setReturnType("void");
074                setRole("setter");
075                addParameter(new ParameterInfo("value", type,
076                                               "New attribute value"));
077            }
078    
079        }
080    
081    
082        // ----------------------------------------------------- Instance Variables
083    
084    
085        /**
086         * The <code>ModelMBeanOperationInfo</code> object that corresponds
087         * to this <code>OperationInfo</code> instance.
088         */
089        transient ModelMBeanOperationInfo info = null;
090        protected String impact = "UNKNOWN";
091        protected String role = "operation";
092        protected String returnType = "void";    // FIXME - Validate
093        protected ParameterInfo parameters[] = new ParameterInfo[0];
094    
095    
096        // ------------------------------------------------------------- Properties
097    
098    
099        /**
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    }