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 package org.apache.tomcat.util.modeler;
18
19 import java.util.Locale;
20 import java.util.concurrent.locks.Lock;
21 import java.util.concurrent.locks.ReadWriteLock;
22 import java.util.concurrent.locks.ReentrantReadWriteLock;
23
24 import javax.management.MBeanOperationInfo;
25 import javax.management.MBeanParameterInfo;
26
27 /**
28  * <p>Internal configuration information for an <code>Operation</code>
29  * descriptor.</p>
30  *
31  * @author Craig R. McClanahan
32  */

33 public class OperationInfo extends FeatureInfo {
34
35     static final long serialVersionUID = 4418342922072614875L;
36
37     // ----------------------------------------------------------- Constructors
38
39     /**
40      * Standard zero-arguments constructor.
41      */

42     public OperationInfo() {
43         super();
44     }
45
46
47     // ----------------------------------------------------- Instance Variables
48
49     protected String impact = "UNKNOWN";
50     protected String role = "operation";
51     protected final ReadWriteLock parametersLock = new ReentrantReadWriteLock();
52     protected ParameterInfo parameters[] = new ParameterInfo[0];
53
54
55     // ------------------------------------------------------------- Properties
56
57     /**
58      * @return the "impact" of this operation, which should be
59      *  a (case-insensitive) string value "ACTION""ACTION_INFO",
60      *  "INFO", or "UNKNOWN".
61      */

62     public String getImpact() {
63         return this.impact;
64     }
65
66     public void setImpact(String impact) {
67         if (impact == null)
68             this.impact = null;
69         else
70             this.impact = impact.toUpperCase(Locale.ENGLISH);
71     }
72
73
74     /**
75      * @return the role of this operation ("getter""setter""operation", or
76      * "constructor").
77      */

78     public String getRole() {
79         return this.role;
80     }
81
82     public void setRole(String role) {
83         this.role = role;
84     }
85
86
87     /**
88      * @return the fully qualified Java class name of the return type for this
89      * operation.
90      */

91     public String getReturnType() {
92         if(type == null) {
93             type = "void";
94         }
95         return type;
96     }
97
98     public void setReturnType(String returnType) {
99         this.type = returnType;
100     }
101
102     /**
103      * @return the set of parameters for this operation.
104      */

105     public ParameterInfo[] getSignature() {
106         Lock readLock = parametersLock.readLock();
107         readLock.lock();
108         try {
109             return this.parameters;
110         } finally {
111             readLock.unlock();
112         }
113     }
114
115     // --------------------------------------------------------- Public Methods
116
117
118     /**
119      * Add a new parameter to the set of arguments for this operation.
120      *
121      * @param parameter The new parameter descriptor
122      */

123     public void addParameter(ParameterInfo parameter) {
124
125         Lock writeLock = parametersLock.writeLock();
126         writeLock.lock();
127         try {
128             ParameterInfo results[] = new ParameterInfo[parameters.length + 1];
129             System.arraycopy(parameters, 0, results, 0, parameters.length);
130             results[parameters.length] = parameter;
131             parameters = results;
132             this.info = null;
133         } finally {
134             writeLock.unlock();
135         }
136     }
137
138
139     /**
140      * Create and return a <code>ModelMBeanOperationInfo</code> object that
141      * corresponds to the attribute described by this instance.
142      * @return the operation info
143      */

144     MBeanOperationInfo createOperationInfo() {
145
146         // Return our cached information (if any)
147         if (info == null) {
148             // Create and return a new information object
149             int impact = MBeanOperationInfo.UNKNOWN;
150             if ("ACTION".equals(getImpact()))
151                 impact = MBeanOperationInfo.ACTION;
152             else if ("ACTION_INFO".equals(getImpact()))
153                 impact = MBeanOperationInfo.ACTION_INFO;
154             else if ("INFO".equals(getImpact()))
155                 impact = MBeanOperationInfo.INFO;
156
157             info = new MBeanOperationInfo(getName(), getDescription(),
158                                           getMBeanParameterInfo(),
159                                           getReturnType(), impact);
160         }
161         return (MBeanOperationInfo)info;
162     }
163
164     protected MBeanParameterInfo[] getMBeanParameterInfo() {
165         ParameterInfo params[] = getSignature();
166         MBeanParameterInfo parameters[] =
167             new MBeanParameterInfo[params.length];
168         for (int i = 0; i < params.length; i++)
169             parameters[i] = params[i].createParameterInfo();
170         return parameters;
171     }
172 }
173