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