KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > MBeanAttributeInfo


1 /*
2  * @(#)MBeanAttributeInfo.java 1.36 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.management;
9
10 import java.lang.reflect.Method JavaDoc;
11 import java.security.AccessController JavaDoc;
12 import java.security.PrivilegedAction JavaDoc;
13
14 import com.sun.jmx.mbeanserver.GetPropertyAction;
15
16 /**
17  * Describes an MBean attribute exposed for management. Instances of
18  * this class are immutable. Subclasses may be mutable but this is
19  * not recommended.
20  *
21  * @since 1.5
22  */

23 public class MBeanAttributeInfo extends MBeanFeatureInfo JavaDoc implements java.io.Serializable JavaDoc, Cloneable JavaDoc {
24
25     /* Serial version */
26     private static final long serialVersionUID;
27     static {
28     /* For complicated reasons, the serialVersionUID changed
29        between JMX 1.0 and JMX 1.1, even though JMX 1.1 did not
30        have compatibility code for this class. So the
31        serialization produced by this class with JMX 1.2 and
32        jmx.serial.form=1.0 is not the same as that produced by
33        this class with JMX 1.1 and jmx.serial.form=1.0. However,
34        the serialization without that property is the same, and
35        that is the only form required by JMX 1.2.
36     */

37     long uid = 8644704819898565848L;
38     try {
39         PrivilegedAction JavaDoc act = new GetPropertyAction("jmx.serial.form");
40         String JavaDoc form = (String JavaDoc) AccessController.doPrivileged(act);
41         if ("1.0".equals(form))
42         uid = 7043855487133450673L;
43     } catch (Exception JavaDoc e) {
44         // OK: exception means no compat with 1.0, too bad
45
}
46     serialVersionUID = uid;
47     }
48
49     static final MBeanAttributeInfo JavaDoc[] NO_ATTRIBUTES =
50     new MBeanAttributeInfo JavaDoc[0];
51
52     /**
53      * @serial The actual attribute type.
54      */

55     private final String JavaDoc attributeType;
56
57     /**
58      * @serial The attribute write right.
59      */

60     private final boolean isWrite;
61
62     /**
63      * @serial The attribute read right.
64      */

65     private final boolean isRead;
66
67     /**
68      * @serial Indicates if this method is a "is"
69      */

70     private final boolean is;
71
72
73     /**
74      * Constructs an <CODE>MBeanAttributeInfo</CODE> object.
75      *
76      * @param name The name of the attribute.
77      * @param type The type or class name of the attribute.
78      * @param description A human readable description of the attribute.
79      * @param isReadable True if the attribute has a getter method, false otherwise.
80      * @param isWritable True if the attribute has a setter method, false otherwise.
81      * @param isIs True if this attribute has an "is" getter, false otherwise.
82      */

83     public MBeanAttributeInfo(String JavaDoc name,
84                   String JavaDoc type,
85                   String JavaDoc description,
86                   boolean isReadable,
87                   boolean isWritable,
88                   boolean isIs)
89         throws IllegalArgumentException JavaDoc {
90
91     super(name, description);
92
93     this.attributeType = type;
94     this.isRead= isReadable;
95     this.isWrite = isWritable;
96     if (isIs && !isReadable) {
97         throw new IllegalArgumentException JavaDoc("Cannot have an \"is\" getter for a non-readable attribute.");
98     }
99     if (isIs && (!type.equals("java.lang.Boolean") && (!type.equals("boolean")))) {
100         throw new IllegalArgumentException JavaDoc("Cannot have an \"is\" getter for a non-boolean attribute.");
101     }
102     this.is = isIs;
103     }
104
105     /**
106      * This constructor takes the name of a simple attribute, and Method
107      * objects for reading and writing the attribute.
108      *
109      * @param name The programmatic name of the attribute.
110      * @param description A human readable description of the attribute.
111      * @param getter The method used for reading the attribute value.
112      * May be null if the property is write-only.
113      * @param setter The method used for writing the attribute value.
114      * May be null if the attribute is read-only.
115      * @exception IntrospectionException There is a consistency
116      * problem in the definition of this attribute.
117      */

118     public MBeanAttributeInfo(String JavaDoc name,
119                   String JavaDoc description,
120                   Method JavaDoc getter,
121                   Method JavaDoc setter) throws IntrospectionException JavaDoc {
122     this(name,
123          attributeType(getter, setter),
124          description,
125          (getter != null),
126          (setter != null),
127          isIs(getter));
128     }
129
130     /**
131      * <p>Returns a shallow clone of this instance.
132      * The clone is obtained by simply calling <tt>super.clone()</tt>,
133      * thus calling the default native shallow cloning mechanism
134      * implemented by <tt>Object.clone()</tt>.
135      * No deeper cloning of any internal field is made.</p>
136      *
137      * <p>Since this class is immutable, cloning is chiefly of
138      * interest to subclasses.</p>
139      */

140      public Object JavaDoc clone () {
141      try {
142          return super.clone() ;
143      } catch (CloneNotSupportedException JavaDoc e) {
144          // should not happen as this class is cloneable
145
return null;
146      }
147      }
148
149     /**
150      * Returns the class name of the attribute.
151      *
152      * @return the class name.
153      */

154     public String JavaDoc getType() {
155     return attributeType;
156     }
157
158     /**
159      * Whether the value of the attribute can be read.
160      *
161      * @return True if the attribute can be read, false otherwise.
162      */

163     public boolean isReadable() {
164     return isRead;
165     }
166
167     /**
168      * Whether new values can be written to the attribute.
169      *
170      * @return True if the attribute can be written to, false otherwise.
171      */

172     public boolean isWritable() {
173     return isWrite;
174     }
175
176     /**
177      * Indicates if this attribute has an "is" getter.
178      *
179      * @return true if this attribute has an "is" getter.
180      */

181     public boolean isIs() {
182     return is;
183     }
184
185     /**
186      * Compare this MBeanAttributeInfo to another.
187      *
188      * @param o the object to compare to.
189      *
190      * @return true iff <code>o</code> is an MBeanAttributeInfo such
191      * that its {@link #getName()}, {@link #getType()}, {@link
192      * #getDescription()}, {@link #isReadable()}, {@link
193      * #isWritable()}, and {@link #isIs()} values are equal (not
194      * necessarily identical) to those of this MBeanAttributeInfo.
195      */

196     public boolean equals(Object JavaDoc o) {
197     if (o == this)
198         return true;
199     if (!(o instanceof MBeanAttributeInfo JavaDoc))
200         return false;
201     MBeanAttributeInfo JavaDoc p = (MBeanAttributeInfo JavaDoc) o;
202     return (p.getName().equals(getName()) &&
203         p.getType().equals(getType()) &&
204         p.getDescription().equals(getDescription()) &&
205         p.isReadable() == isReadable() &&
206         p.isWritable() == isWritable() &&
207         p.isIs() == isIs());
208     }
209
210     /* We do not include everything in the hashcode. We assume that
211        if two operations are different they'll probably have different
212        names or types. The penalty we pay when this assumption is
213        wrong should be less than the penalty we would pay if it were
214        right and we needlessly hashed in the description and parameter
215        array. */

216     public int hashCode() {
217     return getName().hashCode() ^ getType().hashCode();
218     }
219
220     private static boolean isIs(Method JavaDoc getter) {
221     return (getter != null &&
222         getter.getName().startsWith("is") &&
223         (getter.getReturnType().equals(Boolean.TYPE) ||
224                  getter.getReturnType().equals(Boolean JavaDoc.class)));
225     }
226
227     /**
228      * Finds the type of the attribute.
229      */

230     private static String JavaDoc attributeType(Method JavaDoc getter, Method JavaDoc setter)
231         throws IntrospectionException JavaDoc {
232     Class JavaDoc type = null;
233
234     if (getter != null) {
235         if (getter.getParameterTypes().length != 0) {
236         throw new IntrospectionException JavaDoc("bad getter arg count");
237         }
238         type = getter.getReturnType();
239         if (type == Void.TYPE) {
240         throw new IntrospectionException JavaDoc("getter " + getter.getName() +
241                          " returns void");
242         }
243     }
244
245     if (setter != null) {
246         Class JavaDoc params[] = setter.getParameterTypes();
247         if (params.length != 1) {
248         throw new IntrospectionException JavaDoc("bad setter arg count");
249         }
250         if (type == null)
251         type = params[0];
252         else if (type != params[0]) {
253         throw new IntrospectionException JavaDoc("type mismatch between " +
254                          "getter and setter");
255         }
256     }
257
258     if (type == null) {
259         throw new IntrospectionException JavaDoc("getter and setter cannot " +
260                          "both be null");
261     }
262
263     return type.getName();
264     }
265
266 }
267
Popular Tags