001: /**
002: * The XMOJO Project 5
003: * Copyright © 2003 XMOJO.org. All rights reserved.
004:
005: * NO WARRANTY
006:
007: * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
008: * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
009: * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
010: * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
011: * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
012: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
013: * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
014: * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
015: * REPAIR OR CORRECTION.
016:
017: * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
018: * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
019: * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
020: * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
021: * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
022: * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
023: * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
024: * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
025: * SUCH DAMAGES.
026: **/package javax.management;
027:
028: import java.lang.reflect.Method;
029: import java.io.Serializable;
030: import java.io.ObjectInputStream;
031: import java.io.ObjectInputStream.GetField;
032: import java.io.ObjectOutputStream;
033: import java.io.ObjectOutputStream.PutField;
034: import java.io.ObjectStreamField;
035: import java.io.IOException;
036:
037: /**
038: * Describes an MBean attribute exposed for management.
039: */
040: public class MBeanAttributeInfo extends MBeanFeatureInfo implements
041: Serializable, Cloneable {
042: /**
043: * The actual attribute type.
044: */
045: private String type = null;
046:
047: /**
048: * The attribute read right.
049: */
050: private boolean isReadable = true;
051:
052: /**
053: * Indicates if this method is a "is"
054: */
055: private boolean isIs = false;
056:
057: /**
058: * The attribute write right.
059: */
060: private boolean isWritable = true;
061:
062: /**
063: * The attribute read method.
064: */
065: private transient Method getter = null;
066:
067: /**
068: * The attribute write method.
069: */
070: private transient Method setter = null;
071:
072: /* Serial version UID */
073: private static final long serialVersionUID = 7043855487133450673L;
074:
075: private static final ObjectStreamField[] serialPersistentFields = {
076: new ObjectStreamField("attributeType",
077: java.lang.String.class),
078: new ObjectStreamField("isWrite", java.lang.Boolean.TYPE),
079: new ObjectStreamField("isRead", java.lang.Boolean.TYPE),
080: new ObjectStreamField("is", java.lang.Boolean.TYPE), };
081:
082: /**
083: * Constructs an <CODE>MBeanAttributeInfo</CODE> object.
084: *
085: * @param name The name of the attribute.
086: * @param type The type or class name of the attribute.
087: * @param description A human readable description of the attribute.
088: * @param isReadable True if the attribute has a getter method, false otherwise.
089: * @param isWritable True if the attribute has a setter method, false otherwise.
090: * @param is True if this attribute has an "is" getter, false otherwise.
091: */
092: public MBeanAttributeInfo(String name, String type,
093: String description, boolean isReadable, boolean isWritable,
094: boolean isIs) {
095: super (name, description);
096: this .type = type;
097: this .isReadable = isReadable;
098: this .isWritable = isWritable;
099: this .isIs = isIs;
100: }
101:
102: /**
103: * This constructor takes the name of a simple attribute, and Method
104: * objects for reading and writing the attribute.
105: *
106: * @param name The programmatic name of the attribute.
107: *
108: * @param description A human readable description of the attribute.
109: *
110: * @param getter The method used for reading the attribute value.
111: * May be null if the property is write-only.
112: *
113: * @param setter The method used for writing the attribute value.
114: * May be null if the attribute is read-only.
115: *
116: * @exception IntrospectionException There is a consistency problem in
117: * the definition of this attribute.
118: */
119: public MBeanAttributeInfo(String name, String description,
120: Method getter, Method setter) throws IntrospectionException {
121: super (name, description);
122:
123: if (getter == null)
124: isReadable = false;
125: else if (getter.getName().startsWith("is"))
126: isIs = true;
127:
128: if (setter == null)
129: isWritable = false;
130:
131: this .getter = getter;
132: this .setter = setter;
133:
134: getAttributeType();
135: }
136:
137: private void getAttributeType() throws IntrospectionException {
138: Class type = null;
139: String gettername = null;
140: String settername = null;
141: String newgettername = null;
142: String newsettername = null;
143:
144: if (getter != null) {
145: if (getter.getParameterTypes().length != 0) {
146: throw new IntrospectionException("Invalid getter"
147: + getter.getName());
148: }
149: type = getter.getReturnType();
150: if (type == Void.TYPE) {
151: throw new IntrospectionException("Invalid getter"
152: + getter.getName());
153: }
154:
155: gettername = getter.getName();
156:
157: if (isIs)
158: newgettername = gettername.substring(2);
159: else
160: newgettername = gettername.substring(3);
161: }
162:
163: if (setter != null) {
164: Class params[] = setter.getParameterTypes();
165:
166: if (params.length != 1) {
167: throw new IntrospectionException("Invalid setter"
168: + setter.getName());
169: }
170: if (type != null && type != params[0]) {
171: throw new IntrospectionException("Invalid setter"
172: + setter.getName());
173: }
174:
175: type = params[0];
176: if (name != null) {
177: settername = setter.getName();
178: newsettername = settername.substring(3);
179:
180: if (getter != null) {
181: if (!newgettername.equals(newsettername)) {
182: throw new IntrospectionException(
183: "Difference in getter and setter name");
184: }
185: }
186: }
187: }
188:
189: if (type != null) {
190: this .type = type.getName();
191: }
192: }
193:
194: /**
195: * Returns the class name of the attribute.
196: *
197: * @return The class name is returned as String
198: */
199: public String getType() {
200: return type;
201: }
202:
203: /**
204: * Whether the value of the attribute can be read.
205: *
206: * @return True if the attribute can be read, false otherwise.
207: */
208:
209: public boolean isReadable() {
210: return isReadable;
211: }
212:
213: /**
214: * Whether new values can be written to the attribute.
215: *
216: * @return True if the attribute can be written, false otherwise.
217: */
218:
219: public boolean isWritable() {
220: return isWritable;
221: }
222:
223: /**
224: * Indicates if this attribute has an "is" getter
225: * @return True if the attribute has an "is" getter otherwise false
226: */
227: public boolean isIs() {
228: return isIs;
229: }
230:
231: /**
232: * Creates and returns a copy of this object.
233: *
234: * @return Creates a duplicate copy of the object
235: */
236: public Object clone() {
237: return new MBeanAttributeInfo(getName(), type,
238: getDescription(), isReadable, isWritable, isIs);
239: }
240:
241: /**
242: * Returns a human readable version of the MBeanAttributeInfo instance.
243: *
244: * @return MBeanAttributeInfo is returned as a String
245: */
246: public String toString() {
247: return (super .toString() + "\n Type = " + type
248: + "\n IsReadable = "
249: + new Boolean(isReadable).toString()
250: + "\n IsWritable = "
251: + new Boolean(isWritable).toString() + "\n IsIs = " + new Boolean(
252: isIs).toString());
253: }
254:
255: //------------------------- Private methods ---------------------------//
256:
257: private void readObject(ObjectInputStream objectinputstream)
258: throws IOException, ClassNotFoundException {
259: ObjectInputStream.GetField getfield = objectinputstream
260: .readFields();
261:
262: try {
263: type = (String) getfield.get("attributeType", "");
264: isWritable = getfield.get("isWrite", false);
265: isReadable = getfield.get("isRead", false);
266: isIs = getfield.get("is", false);
267: } catch (Exception exception) {
268: }
269: }
270:
271: private void writeObject(ObjectOutputStream objectoutputstream)
272: throws IOException {
273: ObjectOutputStream.PutField putfield = objectoutputstream
274: .putFields();
275: putfield.put("attributeType", type);
276: putfield.put("isWrite", isWritable);
277: putfield.put("isRead", isReadable);
278: putfield.put("is", isIs);
279: objectoutputstream.writeFields();
280: }
281: }
|