1 16 17 18 package org.apache.commons.modeler; 19 20 21 import java.io.Serializable ; 22 import java.lang.reflect.Method ; 23 24 import javax.management.Descriptor ; 25 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 26 27 28 35 36 public class AttributeInfo extends FeatureInfo implements Serializable { 37 static final long serialVersionUID = -2511626862303972143L; 38 39 41 42 46 protected transient ModelMBeanAttributeInfo info = null; 47 protected String displayName = null; 48 protected String getMethod = null; 49 protected String setMethod = null; 50 51 protected transient Method getMethodObj = null; 52 protected transient Method setMethodObj = null; 53 54 protected boolean readable = true; 55 protected boolean writeable = true; 56 57 protected boolean is = false; 58 protected String type = null; 59 60 protected String persist; 61 protected String defaultStringValue; 62 64 65 70 public void setDescription(String description) { 71 super.setDescription(description); 72 this.info = null; 73 } 74 75 80 public void setName(String name) { 81 super.setName(name); 82 this.info = null; 83 } 84 85 88 public String getDisplayName() { 89 return (this.displayName); 90 } 91 92 public void setDisplayName(String displayName) { 93 this.displayName = displayName; 94 } 95 96 99 public String getGetMethod() { 100 return (this.getMethod); 101 } 102 103 public void setGetMethod(String getMethod) { 104 this.getMethod = getMethod; 105 this.info = null; 106 } 107 108 public Method getGetMethodObj() { 109 return getMethodObj; 110 } 111 112 public void setGetMethodObj(Method getMethodObj) { 113 this.getMethodObj = getMethodObj; 114 } 115 116 public Method getSetMethodObj() { 117 return setMethodObj; 118 } 119 120 public void setSetMethodObj(Method setMethodObj) { 121 this.setMethodObj = setMethodObj; 122 } 123 124 127 public boolean isIs() { 128 return (this.is); 129 } 130 131 public void setIs(boolean is) { 132 this.is = is; 133 this.info = null; 134 } 135 136 137 140 public boolean isReadable() { 141 return (this.readable); 142 } 143 144 public void setReadable(boolean readable) { 145 this.readable = readable; 146 this.info = null; 147 } 148 149 150 153 public String getSetMethod() { 154 return (this.setMethod); 155 } 156 157 public void setSetMethod(String setMethod) { 158 this.setMethod = setMethod; 159 this.info = null; 160 } 161 162 163 166 public String getType() { 167 return (this.type); 168 } 169 170 public void setType(String type) { 171 this.type = type; 172 this.info = null; 173 } 174 175 176 179 public boolean isWriteable() { 180 return (this.writeable); 181 } 182 183 public void setWriteable(boolean writeable) { 184 this.writeable = writeable; 185 this.info = null; 186 } 187 188 193 public String getPersist() { 194 return persist; 195 } 196 197 public void setPersist(String persist) { 198 this.persist = persist; 199 } 200 201 205 public String getDefault() { 206 return defaultStringValue; 207 } 208 209 public void setDefault(String defaultStringValue) { 210 this.defaultStringValue = defaultStringValue; 211 } 212 213 214 216 217 221 public ModelMBeanAttributeInfo createAttributeInfo() { 222 if (info != null) 224 return (info); 225 if((getMethodObj != null) || (setMethodObj != null) ) { 226 try { 227 info=new ModelMBeanAttributeInfo (getName(), getDescription(), 228 getMethodObj, setMethodObj); 229 return info; 230 } catch( Exception ex) { 231 ex.printStackTrace(); 232 } 233 } 234 235 info = new ModelMBeanAttributeInfo 237 (getName(), getType(), getDescription(), 238 isReadable(), isWriteable(), false); 239 Descriptor descriptor = info.getDescriptor(); 240 if (getDisplayName() != null) 241 descriptor.setField("displayName", getDisplayName()); 242 if (isReadable()) { 243 if (getGetMethod() != null) 244 descriptor.setField("getMethod", getGetMethod()); 245 else 246 descriptor.setField("getMethod", 247 getMethodName(getName(), true, isIs())); 248 } 249 if (isWriteable()) { 250 if (getSetMethod() != null) 251 descriptor.setField("setMethod", getSetMethod()); 252 else 253 descriptor.setField("setMethod", 254 getMethodName(getName(), false, false)); 255 } 256 addFields(descriptor); 257 info.setDescriptor(descriptor); 258 return (info); 259 260 } 261 262 263 266 public String toString() { 267 268 StringBuffer sb = new StringBuffer ("AttributeInfo["); 269 sb.append("name="); 270 sb.append(name); 271 sb.append(", description="); 272 sb.append(description); 273 if (!readable) { 274 sb.append(", readable="); 275 sb.append(readable); 276 } 277 sb.append(", type="); 278 sb.append(type); 279 if (!writeable) { 280 sb.append(", writeable="); 281 sb.append(writeable); 282 } 283 sb.append("]"); 284 return (sb.toString()); 285 286 } 287 288 289 291 292 300 private String getMethodName(String name, boolean getter, boolean is) { 301 302 StringBuffer sb = new StringBuffer (); 303 if (getter) { 304 if (is) 305 sb.append("is"); 306 else 307 sb.append("get"); 308 } else 309 sb.append("set"); 310 sb.append(Character.toUpperCase(name.charAt(0))); 311 sb.append(name.substring(1)); 312 return (sb.toString()); 313 314 } 315 316 317 } 318 | Popular Tags |