View Javadoc

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;
22  import java.lang.reflect.Method;
23  
24  import javax.management.Descriptor;
25  import javax.management.modelmbean.ModelMBeanAttributeInfo;
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: 155428 $ $Date: 2005-02-26 08:12:25 -0500 (Sat, 26 Feb 2005) $
34   */
35  
36  public class AttributeInfo extends FeatureInfo implements Serializable {
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 info = null;
47      protected String displayName = null;
48      protected String getMethod = null;
49      protected String setMethod = null;
50  
51      protected transient Method getMethodObj = null;
52      protected transient Method setMethodObj = null;
53  
54      protected boolean readable = true;
55      protected boolean writeable = true;
56  
57      protected boolean is = false;
58      protected String type = null;
59  
60      protected String persist;
61      protected String 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 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 name) {
81          super.setName(name);
82          this.info = null;
83      }
84  
85      /***
86       * The display name of this attribute.
87       */
88      public String getDisplayName() {
89          return (this.displayName);
90      }
91  
92      public void setDisplayName(String displayName) {
93          this.displayName = displayName;
94      }
95  
96      /***
97       * The name of the property getter method, if non-standard.
98       */
99      public String getGetMethod() {
100         return (this.getMethod);
101     }
102 
103     public void setGetMethod(String getMethod) {
104         this.getMethod = getMethod;
105         this.info = null;
106     }
107 
108     public Method getGetMethodObj() {
109         return getMethodObj;
110     }
111 
112     public void setGetMethodObj(Method getMethodObj) {
113         this.getMethodObj = getMethodObj;
114     }
115 
116     public Method getSetMethodObj() {
117         return setMethodObj;
118     }
119 
120     public void setSetMethodObj(Method 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 getSetMethod() {
154         return (this.setMethod);
155     }
156 
157     public void setSetMethod(String 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 getType() {
167         return (this.type);
168     }
169 
170     public void setType(String 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 getPersist() {
194         return persist;
195     }
196 
197     public void setPersist(String 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 getDefault() {
206         return defaultStringValue;
207     }
208 
209     public void setDefault(String 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 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(getName(), getDescription(),
228                                         getMethodObj,  setMethodObj);
229                 return info;
230             } catch( Exception ex) {
231                 ex.printStackTrace();
232             }
233         }
234 
235         // Create and return a new information object
236         info = new ModelMBeanAttributeInfo
237             (getName(), getType(), getDescription(),
238              isReadable(), isWriteable(), false);
239         Descriptor 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 toString() {
267 
268         StringBuffer sb = new StringBuffer("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 getMethodName(String name, boolean getter, boolean is) {
301 
302         StringBuffer sb = new StringBuffer();
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 }