KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > modeler > AttributeInfo


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 JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23
24 import javax.management.Descriptor JavaDoc;
25 import javax.management.modelmbean.ModelMBeanAttributeInfo JavaDoc;
26
27
28 /**
29  * <p>Internal configuration information for an <code>Attribute</code>
30  * descriptor.</p>
31  *
32  * @author Craig R. McClanahan
33  * @version $Revision$ $Date: 2005-02-26 05:12:25 -0800 (Sat, 26 Feb 2005) $
34  */

35
36 public class AttributeInfo extends FeatureInfo implements Serializable JavaDoc {
37     static final long serialVersionUID = -2511626862303972143L;
38
39     // ----------------------------------------------------- Instance Variables
40

41
42     /**
43      * The <code>ModelMBeanAttributeInfo</code> object that corresponds
44      * to this <code>AttributeInfo</code> instance.
45      */

46     protected transient ModelMBeanAttributeInfo JavaDoc info = null;
47     protected String JavaDoc displayName = null;
48     protected String JavaDoc getMethod = null;
49     protected String JavaDoc setMethod = null;
50
51     protected transient Method JavaDoc getMethodObj = null;
52     protected transient Method JavaDoc setMethodObj = null;
53
54     protected boolean readable = true;
55     protected boolean writeable = true;
56
57     protected boolean is = false;
58     protected String JavaDoc type = null;
59
60     protected String JavaDoc persist;
61     protected String JavaDoc defaultStringValue;
62     // ------------------------------------------------------------- Properties
63

64
65     /**
66      * Override the <code>description</code> property setter.
67      *
68      * @param description The new description
69      */

70     public void setDescription(String JavaDoc description) {
71         super.setDescription(description);
72         this.info = null;
73     }
74
75     /**
76      * Override the <code>name</code> property setter.
77      *
78      * @param name The new name
79      */

80     public void setName(String JavaDoc name) {
81         super.setName(name);
82         this.info = null;
83     }
84
85     /**
86      * The display name of this attribute.
87      */

88     public String JavaDoc getDisplayName() {
89         return (this.displayName);
90     }
91
92     public void setDisplayName(String JavaDoc displayName) {
93         this.displayName = displayName;
94     }
95
96     /**
97      * The name of the property getter method, if non-standard.
98      */

99     public String JavaDoc getGetMethod() {
100         return (this.getMethod);
101     }
102
103     public void setGetMethod(String JavaDoc getMethod) {
104         this.getMethod = getMethod;
105         this.info = null;
106     }
107
108     public Method JavaDoc getGetMethodObj() {
109         return getMethodObj;
110     }
111
112     public void setGetMethodObj(Method JavaDoc getMethodObj) {
113         this.getMethodObj = getMethodObj;
114     }
115
116     public Method JavaDoc getSetMethodObj() {
117         return setMethodObj;
118     }
119
120     public void setSetMethodObj(Method JavaDoc setMethodObj) {
121         this.setMethodObj = setMethodObj;
122     }
123
124     /**
125      * Is this a boolean attribute with an "is" getter?
126      */

127     public boolean isIs() {
128         return (this.is);
129     }
130
131     public void setIs(boolean is) {
132         this.is = is;
133         this.info = null;
134     }
135
136
137     /**
138      * Is this attribute readable by management applications?
139      */

140     public boolean isReadable() {
141         return (this.readable);
142     }
143
144     public void setReadable(boolean readable) {
145         this.readable = readable;
146         this.info = null;
147     }
148
149
150     /**
151      * The name of the property setter method, if non-standard.
152      */

153     public String JavaDoc getSetMethod() {
154         return (this.setMethod);
155     }
156
157     public void setSetMethod(String JavaDoc setMethod) {
158         this.setMethod = setMethod;
159         this.info = null;
160     }
161
162
163     /**
164      * The fully qualified Java class name of this attribute.
165      */

166     public String JavaDoc getType() {
167         return (this.type);
168     }
169
170     public void setType(String JavaDoc type) {
171         this.type = type;
172         this.info = null;
173     }
174
175
176     /**
177      * Is this attribute writeable by management applications?
178      */

179     public boolean isWriteable() {
180         return (this.writeable);
181     }
182
183     public void setWriteable(boolean writeable) {
184         this.writeable = writeable;
185         this.info = null;
186     }
187
188     /** Persistence policy.
189      * All persistent attributes should have this attribute set.
190      * Valid values:
191      * ???
192      */

193     public String JavaDoc getPersist() {
194         return persist;
195     }
196
197     public void setPersist(String JavaDoc persist) {
198         this.persist = persist;
199     }
200
201     /** Default value. If set, it can provide info to the user and
202      * it can be used by persistence mechanism to generate a more compact
203      * representation ( a value may not be saved if it's default )
204      */

205     public String JavaDoc getDefault() {
206         return defaultStringValue;
207     }
208
209     public void setDefault(String JavaDoc defaultStringValue) {
210         this.defaultStringValue = defaultStringValue;
211     }
212
213
214     // --------------------------------------------------------- Public Methods
215

216
217     /**
218      * Create and return a <code>ModelMBeanAttributeInfo</code> object that
219      * corresponds to the attribute described by this instance.
220      */

221     public ModelMBeanAttributeInfo JavaDoc createAttributeInfo() {
222         // Return our cached information (if any)
223
if (info != null)
224             return (info);
225         if((getMethodObj != null) || (setMethodObj != null) ) {
226             try {
227                 info=new ModelMBeanAttributeInfo JavaDoc(getName(), getDescription(),
228                                         getMethodObj, setMethodObj);
229                 return info;
230             } catch( Exception JavaDoc ex) {
231                 ex.printStackTrace();
232             }
233         }
234
235         // Create and return a new information object
236
info = new ModelMBeanAttributeInfo JavaDoc
237             (getName(), getType(), getDescription(),
238              isReadable(), isWriteable(), false);
239         Descriptor JavaDoc descriptor = info.getDescriptor();
240         if (getDisplayName() != null)
241             descriptor.setField("displayName", getDisplayName());
242         if (isReadable()) {
243             if (getGetMethod() != null)
244                 descriptor.setField("getMethod", getGetMethod());
245             else
246                 descriptor.setField("getMethod",
247                                     getMethodName(getName(), true, isIs()));
248         }
249         if (isWriteable()) {
250             if (getSetMethod() != null)
251                 descriptor.setField("setMethod", getSetMethod());
252             else
253                 descriptor.setField("setMethod",
254                                     getMethodName(getName(), false, false));
255         }
256         addFields(descriptor);
257         info.setDescriptor(descriptor);
258         return (info);
259
260     }
261
262
263     /**
264      * Return a string representation of this attribute descriptor.
265      */

266     public String JavaDoc toString() {
267
268         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("AttributeInfo[");
269         sb.append("name=");
270         sb.append(name);
271         sb.append(", description=");
272         sb.append(description);
273         if (!readable) {
274             sb.append(", readable=");
275             sb.append(readable);
276         }
277         sb.append(", type=");
278         sb.append(type);
279         if (!writeable) {
280             sb.append(", writeable=");
281             sb.append(writeable);
282         }
283         sb.append("]");
284         return (sb.toString());
285
286     }
287
288
289     // -------------------------------------------------------- Private Methods
290

291
292     /**
293      * Create and return the name of a default property getter or setter
294      * method, according to the specified values.
295      *
296      * @param name Name of the property itself
297      * @param getter Do we want a get method (versus a set method)?
298      * @param is If returning a getter, do we want the "is" form?
299      */

300     private String JavaDoc getMethodName(String JavaDoc name, boolean getter, boolean is) {
301
302         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
303         if (getter) {
304             if (is)
305                 sb.append("is");
306             else
307                 sb.append("get");
308         } else
309             sb.append("set");
310         sb.append(Character.toUpperCase(name.charAt(0)));
311         sb.append(name.substring(1));
312         return (sb.toString());
313
314     }
315
316
317 }
318
Popular Tags