001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package javax.management;
009:
010: import java.io.Serializable;
011: import java.lang.reflect.Method;
012:
013: /**
014: * Describes an MBean attribute exposed for management.
015: *
016: * @author <a href="mailto:young_yy@hotmail.org">Young Yang</a>
017: */
018:
019: public class MBeanAttributeInfo extends MBeanFeatureInfo implements
020: Serializable, Cloneable {
021:
022: /**
023: * The attribute read method.
024: */
025: private transient Method readMethod = null;
026:
027: /**
028: * The attribute write method.
029: */
030: private transient Method writeMethod = null;
031:
032: /**
033: * The actual attribute type.
034: */
035: private String attributeType = null;
036:
037: /**
038: * The attribute write right.
039: */
040: private boolean isWrite = false;
041:
042: /**
043: * The attribute read right.
044: */
045: private boolean isRead = false;
046:
047: /**
048: * Indicates if this method is a "is"
049: */
050: private boolean is = false;
051:
052: /**
053: * Constructs an <CODE>MBeanAttributeInfo</CODE> object.
054: *
055: * @param name The name of the attribute.
056: * @param type The type or class name of the attribute.
057: * @param description A human readable description of the attribute.
058: * @param isReadable True if the attribute has a getter method, false otherwise.
059: * @param isWritable True if the attribute has a setter method, false otherwise.
060: * @param isIs True if this attribute has an "is" getter, false otherwise.
061: *
062: */
063: public MBeanAttributeInfo(String name, String type,
064: String description, boolean isReadable, boolean isWritable,
065: boolean isIs) {
066:
067: super (name, description);
068: this .attributeType = type;
069: this .isRead = isReadable;
070: this .isWrite = isWritable;
071: if (isIs && !isReadable) {
072: throw new IllegalArgumentException(
073: "Cannot have an \"is\" getter for a non-readable attribute.");
074: }
075: if (isIs
076: && (!type.equals("java.lang.Boolean") && (!type
077: .equals("boolean")))) {
078: throw new IllegalArgumentException(
079: "Cannot have an \"is\" getter for a non-boolean attribute.");
080: }
081: this .is = isIs;
082:
083: }
084:
085: /**
086: * This constructor takes the name of a simple attribute, and Method
087: * objects for reading and writing the attribute.
088: *
089: * @param name The programmatic name of the attribute.
090: * @param description A human readable description of the attribute.
091: * @param getter The method used for reading the attribute value.
092: * May be null if the property is write-only.
093: * @param setter The method used for writing the attribute value.
094: * May be null if the attribute is read-only.
095: * @exception IntrospectionException There is a consistency problem in the definition of this attribute.
096: */
097: public MBeanAttributeInfo(String name, String description,
098: Method getter, Method setter) throws IntrospectionException {
099: super (name, description);
100: this .readMethod = getter;
101: this .writeMethod = setter;
102: findAttributeType();
103: findAttributeRights();
104: }
105:
106: /**
107: * Package-private duplicate constructor.
108: * <P>
109: * This must isolate the new object from any changes to the mbeanAttributeInfo object.
110: */
111: MBeanAttributeInfo(MBeanAttributeInfo mbeanAttributeInfo) {
112: super (mbeanAttributeInfo.name, mbeanAttributeInfo.description);
113: attributeType = mbeanAttributeInfo.attributeType;
114: isRead = mbeanAttributeInfo.isRead;
115: isWrite = mbeanAttributeInfo.isWrite;
116: is = mbeanAttributeInfo.is;
117: }
118:
119: /**
120: * Creates and returns a copy of this object.
121: */
122: public Object clone() {
123: return (new MBeanAttributeInfo(this ));
124: }
125:
126: /**
127: * Returns the class name of the attribute.
128: */
129: public String getType() {
130: return attributeType;
131: }
132:
133: /**
134: * Whether the value of the attribute can be read.
135: *
136: * @return True if the attribute can be read, false otherwise.
137: */
138: public boolean isReadable() {
139: return isRead;
140: }
141:
142: /**
143: * Whether new values can be written to the attribute.
144: *
145: * @return True if the attribute can be written to, false otherwise.
146: */
147: public boolean isWritable() {
148: return isWrite;
149: }
150:
151: /**
152: * Indicates if this attribute has an "is" getter
153: */
154: public boolean isIs() {
155: return is;
156: }
157:
158: /**
159: * Sets the read/write rights of the attribute.
160: */
161: private void findAttributeRights() {
162: if (readMethod != null) {
163: isRead = true;
164: if (readMethod.getName().startsWith("is")) {
165: is = true;
166: }
167: }
168: if (writeMethod != null) {
169: isWrite = true;
170: }
171: }
172:
173: /**
174: * Finds the type of the attribute.
175: */
176:
177: private void findAttributeType() throws IntrospectionException {
178: Class type = null;
179: try {
180: attributeType = null;
181:
182: if (readMethod != null) {
183: if (readMethod.getParameterTypes().length != 0) {
184: throw new IntrospectionException(
185: "bad read method arg count");
186: }
187: type = readMethod.getReturnType();
188: if (type == Void.TYPE) {
189: throw new IntrospectionException("read method "
190: + readMethod.getName() + " returns void");
191: }
192: }
193:
194: if (writeMethod != null) {
195: Class params[] = writeMethod.getParameterTypes();
196: if (params.length != 1) {
197: throw new IntrospectionException(
198: "bad write method arg count");
199: }
200: if (type != null && type != params[0]) {
201: throw new IntrospectionException(
202: "type mismatch between read and write methods");
203: }
204: type = params[0];
205: }
206:
207: } catch (IntrospectionException ex) {
208: throw ex;
209: }
210: attributeType = type.getName();
211: }
212:
213: }
|