1. /*
  2. * $Header: /home/cvspublic/jakarta-commons/modeler/src/java/org/apache/commons/modeler/AttributeInfo.java,v 1.7 2003/07/20 07:35:12 ggregory Exp $
  3. * $Revision: 1.7 $
  4. * $Date: 2003/07/20 07:35:12 $
  5. *
  6. * ====================================================================
  7. *
  8. * The Apache Software License, Version 1.1
  9. *
  10. * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
  11. * reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in
  22. * the documentation and/or other materials provided with the
  23. * distribution.
  24. *
  25. * 3. The end-user documentation included with the redistribution, if
  26. * any, must include the following acknowlegement:
  27. * "This product includes software developed by the
  28. * Apache Software Foundation (http://www.apache.org/)."
  29. * Alternately, this acknowlegement may appear in the software itself,
  30. * if and wherever such third-party acknowlegements normally appear.
  31. *
  32. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  33. * Foundation" must not be used to endorse or promote products derived
  34. * from this software without prior written permission. For written
  35. * permission, please contact apache@apache.org.
  36. *
  37. * 5. Products derived from this software may not be called "Apache"
  38. * nor may "Apache" appear in their names without prior written
  39. * permission of the Apache Group.
  40. *
  41. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  42. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  43. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  45. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  46. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  47. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  48. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  49. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  50. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  51. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  52. * SUCH DAMAGE.
  53. * ====================================================================
  54. *
  55. * This software consists of voluntary contributions made by many
  56. * individuals on behalf of the Apache Software Foundation. For more
  57. * information on the Apache Software Foundation, please see
  58. * <http://www.apache.org/>.
  59. *
  60. * [Additional notices, if required by prior licensing conditions]
  61. *
  62. */
  63. package org.apache.commons.modeler;
  64. import java.io.Serializable;
  65. import java.lang.reflect.Method;
  66. import javax.management.Descriptor;
  67. import javax.management.modelmbean.ModelMBeanAttributeInfo;
  68. /**
  69. * <p>Internal configuration information for an <code>Attribute</code>
  70. * descriptor.</p>
  71. *
  72. * @author Craig R. McClanahan
  73. * @version $Revision: 1.7 $ $Date: 2003/07/20 07:35:12 $
  74. */
  75. public class AttributeInfo extends FeatureInfo implements Serializable {
  76. static final long serialVersionUID = -2511626862303972143L;
  77. // ----------------------------------------------------- Instance Variables
  78. /**
  79. * The <code>ModelMBeanAttributeInfo</code> object that corresponds
  80. * to this <code>AttributeInfo</code> instance.
  81. */
  82. protected transient ModelMBeanAttributeInfo info = null;
  83. protected String displayName = null;
  84. protected String getMethod = null;
  85. protected String setMethod = null;
  86. protected transient Method getMethodObj = null;
  87. protected transient Method setMethodObj = null;
  88. protected boolean readable = true;
  89. protected boolean writeable = true;
  90. protected boolean is = false;
  91. protected String type = null;
  92. protected String persist;
  93. protected String defaultStringValue;
  94. // ------------------------------------------------------------- Properties
  95. /**
  96. * Override the <code>description</code> property setter.
  97. *
  98. * @param description The new description
  99. */
  100. public void setDescription(String description) {
  101. super.setDescription(description);
  102. this.info = null;
  103. }
  104. /**
  105. * Override the <code>name</code> property setter.
  106. *
  107. * @param name The new name
  108. */
  109. public void setName(String name) {
  110. super.setName(name);
  111. this.info = null;
  112. }
  113. /**
  114. * The display name of this attribute.
  115. */
  116. public String getDisplayName() {
  117. return (this.displayName);
  118. }
  119. public void setDisplayName(String displayName) {
  120. this.displayName = displayName;
  121. }
  122. /**
  123. * The name of the property getter method, if non-standard.
  124. */
  125. public String getGetMethod() {
  126. return (this.getMethod);
  127. }
  128. public void setGetMethod(String getMethod) {
  129. this.getMethod = getMethod;
  130. this.info = null;
  131. }
  132. public Method getGetMethodObj() {
  133. return getMethodObj;
  134. }
  135. public void setGetMethodObj(Method getMethodObj) {
  136. this.getMethodObj = getMethodObj;
  137. }
  138. public Method getSetMethodObj() {
  139. return setMethodObj;
  140. }
  141. public void setSetMethodObj(Method setMethodObj) {
  142. this.setMethodObj = setMethodObj;
  143. }
  144. /**
  145. * Is this a boolean attribute with an "is" getter?
  146. */
  147. public boolean isIs() {
  148. return (this.is);
  149. }
  150. public void setIs(boolean is) {
  151. this.is = is;
  152. this.info = null;
  153. }
  154. /**
  155. * Is this attribute readable by management applications?
  156. */
  157. public boolean isReadable() {
  158. return (this.readable);
  159. }
  160. public void setReadable(boolean readable) {
  161. this.readable = readable;
  162. this.info = null;
  163. }
  164. /**
  165. * The name of the property setter method, if non-standard.
  166. */
  167. public String getSetMethod() {
  168. return (this.setMethod);
  169. }
  170. public void setSetMethod(String setMethod) {
  171. this.setMethod = setMethod;
  172. this.info = null;
  173. }
  174. /**
  175. * The fully qualified Java class name of this attribute.
  176. */
  177. public String getType() {
  178. return (this.type);
  179. }
  180. public void setType(String type) {
  181. this.type = type;
  182. this.info = null;
  183. }
  184. /**
  185. * Is this attribute writeable by management applications?
  186. */
  187. public boolean isWriteable() {
  188. return (this.writeable);
  189. }
  190. public void setWriteable(boolean writeable) {
  191. this.writeable = writeable;
  192. this.info = null;
  193. }
  194. /** Persistence policy.
  195. * All persistent attributes should have this attribute set.
  196. * Valid values:
  197. * ???
  198. */
  199. public String getPersist() {
  200. return persist;
  201. }
  202. public void setPersist(String persist) {
  203. this.persist = persist;
  204. }
  205. /** Default value. If set, it can provide info to the user and
  206. * it can be used by persistence mechanism to generate a more compact
  207. * representation ( a value may not be saved if it's default )
  208. */
  209. public String getDefault() {
  210. return defaultStringValue;
  211. }
  212. public void setDefault(String defaultStringValue) {
  213. this.defaultStringValue = defaultStringValue;
  214. }
  215. // --------------------------------------------------------- Public Methods
  216. /**
  217. * Create and return a <code>ModelMBeanAttributeInfo</code> object that
  218. * corresponds to the attribute described by this instance.
  219. */
  220. public ModelMBeanAttributeInfo createAttributeInfo() {
  221. // Return our cached information (if any)
  222. if (info != null)
  223. return (info);
  224. if((getMethodObj != null) || (setMethodObj != null) ) {
  225. try {
  226. info=new ModelMBeanAttributeInfo(getName(), getDescription(),
  227. getMethodObj, setMethodObj);
  228. return info;
  229. } catch( Exception ex) {
  230. ex.printStackTrace();
  231. }
  232. }
  233. // Create and return a new information object
  234. info = new ModelMBeanAttributeInfo
  235. (getName(), getType(), getDescription(),
  236. isReadable(), isWriteable(), false);
  237. Descriptor descriptor = info.getDescriptor();
  238. if (getDisplayName() != null)
  239. descriptor.setField("displayName", getDisplayName());
  240. if (isReadable()) {
  241. if (getGetMethod() != null)
  242. descriptor.setField("getMethod", getGetMethod());
  243. else
  244. descriptor.setField("getMethod",
  245. getMethodName(getName(), true, isIs()));
  246. }
  247. if (isWriteable()) {
  248. if (getSetMethod() != null)
  249. descriptor.setField("setMethod", getSetMethod());
  250. else
  251. descriptor.setField("setMethod",
  252. getMethodName(getName(), false, false));
  253. }
  254. addFields(descriptor);
  255. info.setDescriptor(descriptor);
  256. return (info);
  257. }
  258. /**
  259. * Return a string representation of this attribute descriptor.
  260. */
  261. public String toString() {
  262. StringBuffer sb = new StringBuffer("AttributeInfo[");
  263. sb.append("name=");
  264. sb.append(name);
  265. sb.append(", description=");
  266. sb.append(description);
  267. if (!readable) {
  268. sb.append(", readable=");
  269. sb.append(readable);
  270. }
  271. sb.append(", type=");
  272. sb.append(type);
  273. if (!writeable) {
  274. sb.append(", writeable=");
  275. sb.append(writeable);
  276. }
  277. sb.append("]");
  278. return (sb.toString());
  279. }
  280. // -------------------------------------------------------- Private Methods
  281. /**
  282. * Create and return the name of a default property getter or setter
  283. * method, according to the specified values.
  284. *
  285. * @param name Name of the property itself
  286. * @param getter Do we want a get method (versus a set method)?
  287. * @param is If returning a getter, do we want the "is" form?
  288. */
  289. private String getMethodName(String name, boolean getter, boolean is) {
  290. StringBuffer sb = new StringBuffer();
  291. if (getter) {
  292. if (is)
  293. sb.append("is");
  294. else
  295. sb.append("get");
  296. } else
  297. sb.append("set");
  298. sb.append(Character.toUpperCase(name.charAt(0)));
  299. sb.append(name.substring(1));
  300. return (sb.toString());
  301. }
  302. }