001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.modeler;
019:
020: import java.io.Serializable;
021: import java.lang.reflect.Method;
022:
023: import javax.management.Descriptor;
024: import javax.management.modelmbean.ModelMBeanAttributeInfo;
025:
026: /**
027: * <p>Internal configuration information for an <code>Attribute</code>
028: * descriptor.</p>
029: *
030: * @author Craig R. McClanahan
031: * @version $Revision: 480402 $ $Date: 2006-11-29 04:43:23 +0000 (Wed, 29 Nov 2006) $
032: */
033:
034: public class AttributeInfo extends FeatureInfo implements Serializable {
035: static final long serialVersionUID = -2511626862303972143L;
036:
037: // ----------------------------------------------------- Instance Variables
038:
039: /**
040: * The <code>ModelMBeanAttributeInfo</code> object that corresponds
041: * to this <code>AttributeInfo</code> instance.
042: */
043: protected transient ModelMBeanAttributeInfo info = null;
044: protected String displayName = null;
045: protected String getMethod = null;
046: protected String setMethod = null;
047:
048: protected transient Method getMethodObj = null;
049: protected transient Method setMethodObj = null;
050:
051: protected boolean readable = true;
052: protected boolean writeable = true;
053:
054: protected boolean is = false;
055: protected String type = null;
056:
057: protected String persist;
058: protected String defaultStringValue;
059:
060: // ------------------------------------------------------------- Properties
061:
062: /**
063: * Override the <code>description</code> property setter.
064: *
065: * @param description The new description
066: */
067: public void setDescription(String description) {
068: super .setDescription(description);
069: this .info = null;
070: }
071:
072: /**
073: * Override the <code>name</code> property setter.
074: *
075: * @param name The new name
076: */
077: public void setName(String name) {
078: super .setName(name);
079: this .info = null;
080: }
081:
082: /**
083: * The display name of this attribute.
084: */
085: public String getDisplayName() {
086: return (this .displayName);
087: }
088:
089: public void setDisplayName(String displayName) {
090: this .displayName = displayName;
091: }
092:
093: /**
094: * The name of the property getter method, if non-standard.
095: */
096: public String getGetMethod() {
097: return (this .getMethod);
098: }
099:
100: public void setGetMethod(String getMethod) {
101: this .getMethod = getMethod;
102: this .info = null;
103: }
104:
105: public Method getGetMethodObj() {
106: return getMethodObj;
107: }
108:
109: public void setGetMethodObj(Method getMethodObj) {
110: this .getMethodObj = getMethodObj;
111: }
112:
113: public Method getSetMethodObj() {
114: return setMethodObj;
115: }
116:
117: public void setSetMethodObj(Method setMethodObj) {
118: this .setMethodObj = setMethodObj;
119: }
120:
121: /**
122: * Is this a boolean attribute with an "is" getter?
123: */
124: public boolean isIs() {
125: return (this .is);
126: }
127:
128: public void setIs(boolean is) {
129: this .is = is;
130: this .info = null;
131: }
132:
133: /**
134: * Is this attribute readable by management applications?
135: */
136: public boolean isReadable() {
137: return (this .readable);
138: }
139:
140: public void setReadable(boolean readable) {
141: this .readable = readable;
142: this .info = null;
143: }
144:
145: /**
146: * The name of the property setter method, if non-standard.
147: */
148: public String getSetMethod() {
149: return (this .setMethod);
150: }
151:
152: public void setSetMethod(String setMethod) {
153: this .setMethod = setMethod;
154: this .info = null;
155: }
156:
157: /**
158: * The fully qualified Java class name of this attribute.
159: */
160: public String getType() {
161: return (this .type);
162: }
163:
164: public void setType(String type) {
165: this .type = type;
166: this .info = null;
167: }
168:
169: /**
170: * Is this attribute writeable by management applications?
171: */
172: public boolean isWriteable() {
173: return (this .writeable);
174: }
175:
176: public void setWriteable(boolean writeable) {
177: this .writeable = writeable;
178: this .info = null;
179: }
180:
181: /** Persistence policy.
182: * All persistent attributes should have this attribute set.
183: * Valid values:
184: * ???
185: */
186: public String getPersist() {
187: return persist;
188: }
189:
190: public void setPersist(String persist) {
191: this .persist = persist;
192: }
193:
194: /** Default value. If set, it can provide info to the user and
195: * it can be used by persistence mechanism to generate a more compact
196: * representation ( a value may not be saved if it's default )
197: */
198: public String getDefault() {
199: return defaultStringValue;
200: }
201:
202: public void setDefault(String defaultStringValue) {
203: this .defaultStringValue = defaultStringValue;
204: }
205:
206: // --------------------------------------------------------- Public Methods
207:
208: /**
209: * Create and return a <code>ModelMBeanAttributeInfo</code> object that
210: * corresponds to the attribute described by this instance.
211: */
212: public ModelMBeanAttributeInfo createAttributeInfo() {
213: // Return our cached information (if any)
214: if (info != null)
215: return (info);
216: if ((getMethodObj != null) || (setMethodObj != null)) {
217: try {
218: info = new ModelMBeanAttributeInfo(getName(),
219: getDescription(), getMethodObj, setMethodObj);
220: return info;
221: } catch (Exception ex) {
222: ex.printStackTrace();
223: }
224: }
225:
226: // Create and return a new information object
227: info = new ModelMBeanAttributeInfo(getName(), getType(),
228: getDescription(), isReadable(), isWriteable(), false);
229: Descriptor descriptor = info.getDescriptor();
230: if (getDisplayName() != null)
231: descriptor.setField("displayName", getDisplayName());
232: if (isReadable()) {
233: if (getGetMethod() != null)
234: descriptor.setField("getMethod", getGetMethod());
235: else
236: descriptor.setField("getMethod", getMethodName(
237: getName(), true, isIs()));
238: }
239: if (isWriteable()) {
240: if (getSetMethod() != null)
241: descriptor.setField("setMethod", getSetMethod());
242: else
243: descriptor.setField("setMethod", getMethodName(
244: getName(), false, false));
245: }
246: addFields(descriptor);
247: info.setDescriptor(descriptor);
248: return (info);
249:
250: }
251:
252: /**
253: * Return a string representation of this attribute descriptor.
254: */
255: public String toString() {
256:
257: StringBuffer sb = new StringBuffer("AttributeInfo[");
258: sb.append("name=");
259: sb.append(name);
260: sb.append(", description=");
261: sb.append(description);
262: if (!readable) {
263: sb.append(", readable=");
264: sb.append(readable);
265: }
266: sb.append(", type=");
267: sb.append(type);
268: if (!writeable) {
269: sb.append(", writeable=");
270: sb.append(writeable);
271: }
272: sb.append("]");
273: return (sb.toString());
274:
275: }
276:
277: // -------------------------------------------------------- Private Methods
278:
279: /**
280: * Create and return the name of a default property getter or setter
281: * method, according to the specified values.
282: *
283: * @param name Name of the property itself
284: * @param getter Do we want a get method (versus a set method)?
285: * @param is If returning a getter, do we want the "is" form?
286: */
287: private String getMethodName(String name, boolean getter, boolean is) {
288:
289: StringBuffer sb = new StringBuffer();
290: if (getter) {
291: if (is)
292: sb.append("is");
293: else
294: sb.append("get");
295: } else
296: sb.append("set");
297: sb.append(Character.toUpperCase(name.charAt(0)));
298: sb.append(name.substring(1));
299: return (sb.toString());
300:
301: }
302:
303: }
|