1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.modeler;
20
21
22 import java.io.Serializable;
23 import java.lang.reflect.Method;
24
25 import javax.management.Descriptor;
26 import javax.management.modelmbean.ModelMBeanAttributeInfo;
27
28
29 /***
30 * <p>Internal configuration information for an <code>Attribute</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 AttributeInfo extends FeatureInfo implements Serializable {
38 static final long serialVersionUID = -2511626862303972143L;
39
40
41
42
43 /***
44 * The <code>ModelMBeanAttributeInfo</code> object that corresponds
45 * to this <code>AttributeInfo</code> instance.
46 */
47 protected transient ModelMBeanAttributeInfo info = null;
48 protected String displayName = null;
49 protected String getMethod = null;
50 protected String setMethod = null;
51
52 protected transient Method getMethodObj = null;
53 protected transient Method setMethodObj = null;
54
55 protected boolean readable = true;
56 protected boolean writeable = true;
57
58 protected boolean is = false;
59 protected String type = null;
60
61 protected String persist;
62 protected String defaultStringValue;
63
64
65
66 /***
67 * Override the <code>description</code> property setter.
68 *
69 * @param description The new description
70 */
71 public void setDescription(String description) {
72 super.setDescription(description);
73 this.info = null;
74 }
75
76 /***
77 * Override the <code>name</code> property setter.
78 *
79 * @param name The new name
80 */
81 public void setName(String name) {
82 super.setName(name);
83 this.info = null;
84 }
85
86 /***
87 * The display name of this attribute.
88 */
89 public String getDisplayName() {
90 return (this.displayName);
91 }
92
93 public void setDisplayName(String displayName) {
94 this.displayName = displayName;
95 }
96
97 /***
98 * The name of the property getter method, if non-standard.
99 */
100 public String getGetMethod() {
101 return (this.getMethod);
102 }
103
104 public void setGetMethod(String getMethod) {
105 this.getMethod = getMethod;
106 this.info = null;
107 }
108
109 public Method getGetMethodObj() {
110 return getMethodObj;
111 }
112
113 public void setGetMethodObj(Method getMethodObj) {
114 this.getMethodObj = getMethodObj;
115 }
116
117 public Method getSetMethodObj() {
118 return setMethodObj;
119 }
120
121 public void setSetMethodObj(Method setMethodObj) {
122 this.setMethodObj = setMethodObj;
123 }
124
125 /***
126 * Is this a boolean attribute with an "is" getter?
127 */
128 public boolean isIs() {
129 return (this.is);
130 }
131
132 public void setIs(boolean is) {
133 this.is = is;
134 this.info = null;
135 }
136
137
138 /***
139 * Is this attribute readable by management applications?
140 */
141 public boolean isReadable() {
142 return (this.readable);
143 }
144
145 public void setReadable(boolean readable) {
146 this.readable = readable;
147 this.info = null;
148 }
149
150
151 /***
152 * The name of the property setter method, if non-standard.
153 */
154 public String getSetMethod() {
155 return (this.setMethod);
156 }
157
158 public void setSetMethod(String setMethod) {
159 this.setMethod = setMethod;
160 this.info = null;
161 }
162
163
164 /***
165 * The fully qualified Java class name of this attribute.
166 */
167 public String getType() {
168 return (this.type);
169 }
170
171 public void setType(String type) {
172 this.type = type;
173 this.info = null;
174 }
175
176
177 /***
178 * Is this attribute writeable by management applications?
179 */
180 public boolean isWriteable() {
181 return (this.writeable);
182 }
183
184 public void setWriteable(boolean writeable) {
185 this.writeable = writeable;
186 this.info = null;
187 }
188
189 /*** Persistence policy.
190 * All persistent attributes should have this attribute set.
191 * Valid values:
192 * ???
193 */
194 public String getPersist() {
195 return persist;
196 }
197
198 public void setPersist(String persist) {
199 this.persist = persist;
200 }
201
202 /*** Default value. If set, it can provide info to the user and
203 * it can be used by persistence mechanism to generate a more compact
204 * representation ( a value may not be saved if it's default )
205 */
206 public String getDefault() {
207 return defaultStringValue;
208 }
209
210 public void setDefault(String defaultStringValue) {
211 this.defaultStringValue = defaultStringValue;
212 }
213
214
215
216
217
218 /***
219 * Create and return a <code>ModelMBeanAttributeInfo</code> object that
220 * corresponds to the attribute described by this instance.
221 */
222 public ModelMBeanAttributeInfo createAttributeInfo() {
223
224 if (info != null)
225 return (info);
226 if((getMethodObj != null) || (setMethodObj != null) ) {
227 try {
228 info=new ModelMBeanAttributeInfo(getName(), getDescription(),
229 getMethodObj, setMethodObj);
230 return info;
231 } catch( Exception ex) {
232 ex.printStackTrace();
233 }
234 }
235
236
237 info = new ModelMBeanAttributeInfo
238 (getName(), getType(), getDescription(),
239 isReadable(), isWriteable(), false);
240 Descriptor descriptor = info.getDescriptor();
241 if (getDisplayName() != null)
242 descriptor.setField("displayName", getDisplayName());
243 if (isReadable()) {
244 if (getGetMethod() != null)
245 descriptor.setField("getMethod", getGetMethod());
246 else
247 descriptor.setField("getMethod",
248 getMethodName(getName(), true, isIs()));
249 }
250 if (isWriteable()) {
251 if (getSetMethod() != null)
252 descriptor.setField("setMethod", getSetMethod());
253 else
254 descriptor.setField("setMethod",
255 getMethodName(getName(), false, false));
256 }
257 addFields(descriptor);
258 info.setDescriptor(descriptor);
259 return (info);
260
261 }
262
263
264 /***
265 * Return a string representation of this attribute descriptor.
266 */
267 public String toString() {
268
269 StringBuffer sb = new StringBuffer("AttributeInfo[");
270 sb.append("name=");
271 sb.append(name);
272 sb.append(", description=");
273 sb.append(description);
274 if (!readable) {
275 sb.append(", readable=");
276 sb.append(readable);
277 }
278 sb.append(", type=");
279 sb.append(type);
280 if (!writeable) {
281 sb.append(", writeable=");
282 sb.append(writeable);
283 }
284 sb.append("]");
285 return (sb.toString());
286
287 }
288
289
290
291
292
293 /***
294 * Create and return the name of a default property getter or setter
295 * method, according to the specified values.
296 *
297 * @param name Name of the property itself
298 * @param getter Do we want a get method (versus a set method)?
299 * @param is If returning a getter, do we want the "is" form?
300 */
301 private String getMethodName(String name, boolean getter, boolean is) {
302
303 StringBuffer sb = new StringBuffer();
304 if (getter) {
305 if (is)
306 sb.append("is");
307 else
308 sb.append("get");
309 } else
310 sb.append("set");
311 sb.append(Character.toUpperCase(name.charAt(0)));
312 sb.append(name.substring(1));
313 return (sb.toString());
314
315 }
316
317
318 }