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
31
32
33
34
35
36
37 public class AttributeInfo extends FeatureInfo implements Serializable {
38 static final long serialVersionUID = -2511626862303972143L;
39
40
41
42
43
44
45
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
68
69
70
71 public void setDescription(String description) {
72 super.setDescription(description);
73 this.info = null;
74 }
75
76
77
78
79
80
81 public void setName(String name) {
82 super.setName(name);
83 this.info = null;
84 }
85
86
87
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
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
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
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
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
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
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
190
191
192
193
194 public String getPersist() {
195 return persist;
196 }
197
198 public void setPersist(String persist) {
199 this.persist = persist;
200 }
201
202
203
204
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
220
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
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
295
296
297
298
299
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 }