KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > jmx > AttributeDescriptor


1 package org.sapia.soto.jmx;
2
3 import org.sapia.soto.util.Type;
4
5 import java.lang.reflect.Method JavaDoc;
6
7 import javax.management.IntrospectionException JavaDoc;
8 import javax.management.MBeanAttributeInfo JavaDoc;
9
10
11 /**
12  * An instance of this class wraps read and write <code>Method</code> objects
13  * corresponding to an attribute of a MBean. An instance of this class generates
14  * the <code>MBeanAttributeInfo</code> corresponding to its attribute.
15  * <b>
16  * The class provides a setter to define the description of the attribute.
17  * If no description is specified, a default one is used.
18  *
19  * @author Yanick Duchesne
20  * <dl>
21  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
22  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
23  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
24  * </dl>
25  */

26 public class AttributeDescriptor {
27   private String JavaDoc _desc = MBeanDescriptor.DEFAULT_DESC;
28   private String JavaDoc _name;
29   private boolean _isIs;
30   private Method JavaDoc _readable;
31   private Method JavaDoc _writable;
32
33   /**
34    * Constructor for AttributeDescriptor.
35    */

36   public AttributeDescriptor() {
37     super();
38   }
39
40   /**
41    * Sets this instance's description.
42    *
43    * @param desc a description.
44    */

45   public void setDescription(String JavaDoc desc) {
46     _desc = desc;
47   }
48
49   /**
50    * Returns the name of the MBean attribute to which this
51    * instance corresponds.
52    *
53    * @return an attribute name.
54    */

55   public String JavaDoc getAttributeName() {
56     return _name;
57   }
58
59   /**
60    * Return the method object corresponding to the "getter" of this
61    * instance's corresponding MBean attribute.
62    *
63    * @return a <code>Method</code> object, or <code>null</code> if this
64    * instance has no getter defined.
65    */

66   public Method JavaDoc getReadMethod() {
67     return _readable;
68   }
69
70   /**
71    * Return the method object corresponding to the "setter" of this
72    * instance's corresponding MBean attribute.
73    *
74    * @return a <code>Method</code> object, or <code>null</code> if this
75    * instance has no setter defined.
76    */

77   public Method JavaDoc getWriteMethod() {
78     return _writable;
79   }
80
81   public void setWritable(boolean writable) {
82     if (!writable) {
83       _writable = null;
84     }
85   }
86
87   /**
88    * Returns the <code>MBeanAttributeInfo</code> corresponding to this
89    * instance's MBean attribute.
90    *
91    * @return a <code>MBeanAttribute</code>.
92    */

93   public MBeanAttributeInfo JavaDoc getInfo() throws IntrospectionException JavaDoc {
94     return new MBeanAttributeInfo JavaDoc(_name, _desc, _readable, _writable);
95   }
96
97   /**
98    * Sets the name of this instance's corresponding MBean attribute.
99    *
100    * @param name a name.
101    */

102   void setAttributeName(String JavaDoc name) {
103     _name = name;
104   }
105
106   /**
107    * Specifies if this instance corresponds to an "is" method.
108    *
109    * @param bool if <code>true</code>, specifies that this instance
110    * corresponds to a "is" method.
111    */

112   void setBoolean(boolean bool) {
113     _isIs = bool;
114   }
115
116   /**
117    * Sets the method object corresponding to the setter of this
118    * instance's MBean attribute.
119    *
120    * @param writable a <code>Method</code> object.
121    */

122   void setWritable(Method JavaDoc writable) {
123     _writable = writable;
124   }
125
126   /**
127    * Returns the type name of the attribute to which this instance corresponds.
128    *
129    * @return a type name.
130    */

131   public String JavaDoc getType() {
132     if (_readable != null) {
133       if (Type.hasTypeForTypeName(_readable.getReturnType().getName())) {
134         return Type.getTypeForTypeName(_readable.getReturnType().getName())
135                    .getName();
136       } else {
137         return _readable.getReturnType().getName();
138       }
139     } else {
140       if (Type.hasTypeForTypeName(_writable.getParameterTypes()[0].getName())) {
141         return Type.getTypeForTypeName(_writable.getParameterTypes()[0].getName())
142                    .getName();
143       } else {
144         return _writable.getParameterTypes()[0].getName();
145       }
146     }
147   }
148
149   /**
150    * Sets the method object corresponding to the getter of this
151    * instance's MBean attribute.
152    *
153    * @param readable a <code>Method</code> object.
154    */

155   void setReadable(Method JavaDoc readable) {
156     _readable = readable;
157   }
158
159   public String JavaDoc toString() {
160     return "[ name=" + _name + ", description=" + _desc + ", isIs=" + _isIs +
161     ", readable=" + _readable + ", writable=" + _writable + " ]";
162   }
163 }
164
Popular Tags