1 16 17 18 package org.apache.commons.modeler; 19 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import javax.management.Descriptor ; 26 import javax.management.InstanceNotFoundException ; 27 import javax.management.MBeanException ; 28 import javax.management.RuntimeOperationsException ; 29 import javax.management.modelmbean.InvalidTargetObjectTypeException ; 30 import javax.management.modelmbean.ModelMBean ; 31 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 32 import javax.management.modelmbean.ModelMBeanConstructorInfo ; 33 import javax.management.modelmbean.ModelMBeanInfo ; 34 import javax.management.modelmbean.ModelMBeanInfoSupport ; 35 import javax.management.modelmbean.ModelMBeanNotificationInfo ; 36 import javax.management.modelmbean.ModelMBeanOperationInfo ; 37 38 39 46 47 public class ManagedBean implements java.io.Serializable 48 { 49 51 52 56 transient ModelMBeanInfo info = null; 57 protected AttributeInfo attributes[] = new AttributeInfo[0]; 58 protected String className = 59 "org.apache.commons.modeler.BaseModelMBean"; 60 protected ConstructorInfo constructors[] = new ConstructorInfo[0]; 61 protected String description = null; 62 protected String domain = null; 63 protected String group = null; 64 protected String name = null; 65 66 protected List fields = new ArrayList (); 67 protected NotificationInfo notifications[] = new NotificationInfo[0]; 68 protected OperationInfo operations[] = new OperationInfo[0]; 69 protected String type = null; 70 71 74 public ManagedBean() { 75 AttributeInfo ai=new AttributeInfo(); 76 ai.setName("modelerType"); 77 ai.setDescription("Type of the modeled resource. Can be set only once"); 78 ai.setType("java.lang.String"); 79 ai.setWriteable(false); 80 addAttribute(ai); 81 } 82 83 85 86 89 public AttributeInfo[] getAttributes() { 90 return (this.attributes); 91 } 92 93 94 100 public String getClassName() { 101 return (this.className); 102 } 103 104 public void setClassName(String className) { 105 this.className = className; 106 this.info = null; 107 } 108 109 110 113 public ConstructorInfo[] getConstructors() { 114 return (this.constructors); 115 } 116 117 118 121 public String getDescription() { 122 return (this.description); 123 } 124 125 public void setDescription(String description) { 126 this.description = description; 127 this.info = null; 128 } 129 130 131 135 public String getDomain() { 136 return (this.domain); 137 } 138 139 public void setDomain(String domain) { 140 this.domain = domain; 141 } 142 143 144 149 public List getFields() { 150 return (this.fields); 151 } 152 153 154 157 public String getGroup() { 158 return (this.group); 159 } 160 161 public void setGroup(String group) { 162 this.group = group; 163 } 164 165 166 170 public String getName() { 171 return (this.name); 172 } 173 174 public void setName(String name) { 175 this.name = name; 176 this.info = null; 177 } 178 179 180 183 public NotificationInfo[] getNotifications() { 184 return (this.notifications); 185 } 186 187 188 191 public OperationInfo[] getOperations() { 192 return (this.operations); 193 } 194 195 196 201 public String getType() { 202 return (this.type); 203 } 204 205 public void setType(String type) { 206 this.type = type; 207 this.info = null; 208 } 209 210 211 213 214 219 public void addAttribute(AttributeInfo attribute) { 220 221 synchronized (attributes) { 222 AttributeInfo results[] = 223 new AttributeInfo[attributes.length + 1]; 224 System.arraycopy(attributes, 0, results, 0, attributes.length); 225 results[attributes.length] = attribute; 226 attributes = results; 227 this.info = null; 228 } 229 230 } 231 232 233 238 public void addConstructor(ConstructorInfo constructor) { 239 240 synchronized (constructors) { 241 ConstructorInfo results[] = 242 new ConstructorInfo[constructors.length + 1]; 243 System.arraycopy(constructors, 0, results, 0, constructors.length); 244 results[constructors.length] = constructor; 245 constructors = results; 246 this.info = null; 247 } 248 249 } 250 251 252 258 public void addField(FieldInfo field) { 259 fields.add(field); 260 } 261 262 263 268 public void addNotification(NotificationInfo notification) { 269 270 synchronized (notifications) { 271 NotificationInfo results[] = 272 new NotificationInfo[notifications.length + 1]; 273 System.arraycopy(notifications, 0, results, 0, 274 notifications.length); 275 results[notifications.length] = notification; 276 notifications = results; 277 this.info = null; 278 } 279 280 } 281 282 283 288 public void addOperation(OperationInfo operation) { 289 synchronized (operations) { 290 OperationInfo results[] = 291 new OperationInfo[operations.length + 1]; 292 System.arraycopy(operations, 0, results, 0, operations.length); 293 results[operations.length] = operation; 294 operations = results; 295 this.info = null; 296 } 297 298 } 299 300 301 317 public ModelMBean createMBean() 318 throws InstanceNotFoundException , 319 InvalidTargetObjectTypeException , 320 MBeanException , RuntimeOperationsException { 321 322 return (createMBean(null)); 323 324 } 325 326 327 346 public ModelMBean createMBean(Object instance) 347 throws InstanceNotFoundException , 348 InvalidTargetObjectTypeException , 349 MBeanException , RuntimeOperationsException { 350 351 Class clazz = null; 353 Exception ex = null; 354 try { 355 clazz = Class.forName(getClassName()); 356 } catch (Exception e) { 357 } 358 359 if( clazz==null ) { 360 try { 361 ClassLoader cl= Thread.currentThread().getContextClassLoader(); 362 if ( cl != null) 363 clazz= cl.loadClass(getClassName()); 364 } catch (Exception e) { 365 ex=e; 366 } 367 } 368 369 if( clazz==null) { 370 throw new MBeanException 371 (ex, "Cannot load ModelMBean class " + getClassName()); 372 } 373 374 ModelMBean mbean = null; 376 try { 377 mbean = (ModelMBean ) clazz.newInstance(); 378 mbean.setModelMBeanInfo(createMBeanInfo()); 379 } catch (MBeanException e) { 380 throw e; 381 } catch (RuntimeOperationsException e) { 382 throw e; 383 } catch (Exception e) { 384 throw new MBeanException 385 (e, "Cannot instantiate ModelMBean of class " + 386 getClassName()); 387 } 388 389 try { 391 if (instance != null) 392 mbean.setManagedResource(instance, "objectReference"); 393 } catch (InstanceNotFoundException e) { 394 throw e; 395 } catch (InvalidTargetObjectTypeException e) { 396 throw e; 397 } 398 return (mbean); 399 400 } 401 402 403 407 public ModelMBeanInfo createMBeanInfo() { 408 409 if (info != null) 411 return (info); 412 413 AttributeInfo attrs[] = getAttributes(); 415 ModelMBeanAttributeInfo attributes[] = 416 new ModelMBeanAttributeInfo [attrs.length]; 417 for (int i = 0; i < attrs.length; i++) 418 attributes[i] = attrs[i].createAttributeInfo(); 419 420 ConstructorInfo consts[] = getConstructors(); 421 ModelMBeanConstructorInfo constructors[] = 422 new ModelMBeanConstructorInfo [consts.length]; 423 for (int i = 0; i < consts.length; i++) 424 constructors[i] = consts[i].createConstructorInfo(); 425 NotificationInfo notifs[] = getNotifications(); 426 ModelMBeanNotificationInfo notifications[] = 427 new ModelMBeanNotificationInfo [notifs.length]; 428 for (int i = 0; i < notifs.length; i++) 429 notifications[i] = notifs[i].createNotificationInfo(); 430 OperationInfo opers[] = getOperations(); 431 ModelMBeanOperationInfo operations[] = 432 new ModelMBeanOperationInfo [opers.length]; 433 for (int i = 0; i < opers.length; i++) 434 operations[i] = opers[i].createOperationInfo(); 435 436 462 463 info = new ModelMBeanInfoSupport 465 (getClassName(), getDescription(), 466 attributes, constructors, operations, notifications); 467 try { 468 Descriptor descriptor = info.getMBeanDescriptor(); 469 Iterator fields = getFields().iterator(); 470 while (fields.hasNext()) { 471 FieldInfo field = (FieldInfo) fields.next(); 472 descriptor.setField(field.getName(), field.getValue()); 473 } 474 info.setMBeanDescriptor(descriptor); 475 } catch (MBeanException e) { 476 ; 477 } 478 479 return (info); 480 481 } 482 483 484 487 public String toString() { 488 489 StringBuffer sb = new StringBuffer ("ManagedBean["); 490 sb.append("name="); 491 sb.append(name); 492 sb.append(", className="); 493 sb.append(className); 494 sb.append(", description="); 495 sb.append(description); 496 if (group != null) { 497 sb.append(", group="); 498 sb.append(group); 499 } 500 sb.append(", type="); 501 sb.append(type); 502 sb.append("]"); 503 return (sb.toString()); 504 505 } 506 507 508 } 509 | Popular Tags |