| 1 17 18 19 package org.apache.tomcat.util.modeler; 20 21 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import java.util.Iterator ; 25 26 import javax.management.Attribute ; 27 import javax.management.AttributeChangeNotification ; 28 import javax.management.AttributeList ; 29 import javax.management.AttributeNotFoundException ; 30 import javax.management.DynamicMBean ; 31 import javax.management.InstanceNotFoundException ; 32 import javax.management.InvalidAttributeValueException ; 33 import javax.management.ListenerNotFoundException ; 34 import javax.management.MBeanException ; 35 import javax.management.MBeanInfo ; 36 import javax.management.MBeanNotificationInfo ; 37 import javax.management.MBeanRegistration ; 38 import javax.management.MBeanServer ; 39 import javax.management.Notification ; 40 import javax.management.NotificationFilter ; 41 import javax.management.NotificationListener ; 42 import javax.management.ObjectName ; 43 import javax.management.ReflectionException ; 44 import javax.management.RuntimeErrorException ; 45 import javax.management.RuntimeOperationsException ; 46 import javax.management.modelmbean.InvalidTargetObjectTypeException ; 47 import javax.management.modelmbean.ModelMBeanNotificationBroadcaster ; 48 49 import org.apache.commons.logging.Log; 50 import org.apache.commons.logging.LogFactory; 51 52 73 74 103 public class BaseModelMBean implements DynamicMBean , MBeanRegistration , ModelMBeanNotificationBroadcaster { 104 private static Log log = LogFactory.getLog(BaseModelMBean.class); 105 106 108 117 protected BaseModelMBean() throws MBeanException , RuntimeOperationsException { 118 super(); 119 } 120 121 123 protected ObjectName oname=null; 124 125 128 protected BaseNotificationBroadcaster attributeBroadcaster = null; 129 130 133 protected BaseNotificationBroadcaster generalBroadcaster = null; 134 135 137 protected ManagedBean managedBean = null; 138 139 142 protected Object resource = null; 143 144 static final Object [] NO_ARGS_PARAM=new Object [0]; 147 static final Class [] NO_ARGS_PARAM_SIG=new Class [0]; 148 149 protected String resourceType = null; 150 151 154 166 public Object getAttribute(String name) 167 throws AttributeNotFoundException , MBeanException , 168 ReflectionException { 169 if (name == null) 171 throw new RuntimeOperationsException  172 (new IllegalArgumentException ("Attribute name is null"), 173 "Attribute name is null"); 174 175 if( (resource instanceof DynamicMBean ) && 176 ! ( resource instanceof BaseModelMBean )) { 177 return ((DynamicMBean )resource).getAttribute(name); 178 } 179 180 Method m=managedBean.getGetter(name, this, resource); 181 Object result = null; 182 try { 183 Class declaring=m.getDeclaringClass(); 184 if( declaring.isAssignableFrom(this.getClass()) ) { 187 result = m.invoke(this, NO_ARGS_PARAM ); 188 } else { 189 result = m.invoke(resource, NO_ARGS_PARAM ); 190 } 191 } catch (InvocationTargetException e) { 192 Throwable t = e.getTargetException(); 193 if (t == null) 194 t = e; 195 if (t instanceof RuntimeException ) 196 throw new RuntimeOperationsException  197 ((RuntimeException ) t, "Exception invoking method " + name); 198 else if (t instanceof Error ) 199 throw new RuntimeErrorException  200 ((Error ) t, "Error invoking method " + name); 201 else 202 throw new MBeanException  203 (e, "Exception invoking method " + name); 204 } catch (Exception e) { 205 throw new MBeanException  206 (e, "Exception invoking method " + name); 207 } 208 209 return (result); 212 } 213 214 215 220 public AttributeList getAttributes(String names[]) { 221 222 if (names == null) 224 throw new RuntimeOperationsException  225 (new IllegalArgumentException ("Attribute names list is null"), 226 "Attribute names list is null"); 227 228 AttributeList response = new AttributeList (); 230 for (int i = 0; i < names.length; i++) { 231 try { 232 response.add(new Attribute (names[i],getAttribute(names[i]))); 233 } catch (Exception e) { 234 ; ; } 237 } 238 return (response); 239 240 } 241 242 public void setManagedBean(ManagedBean managedBean) { 243 this.managedBean = managedBean; 244 } 245 246 249 public MBeanInfo getMBeanInfo() { 250 return managedBean.getMBeanInfo(); 251 } 252 253 254 273 public Object invoke(String name, Object params[], String signature[]) 274 throws MBeanException , ReflectionException 275 { 276 if( (resource instanceof DynamicMBean ) && 277 ! ( resource instanceof BaseModelMBean )) { 278 return ((DynamicMBean )resource).invoke(name, params, signature); 279 } 280 281 if (name == null) 283 throw new RuntimeOperationsException  284 (new IllegalArgumentException ("Method name is null"), 285 "Method name is null"); 286 287 if( log.isDebugEnabled()) log.debug("Invoke " + name); 288 MethodKey mkey = new MethodKey(name, signature); 289 Method method= managedBean.getInvoke(name, params, signature, this, resource); 290 291 Object result = null; 293 try { 294 if( method.getDeclaringClass().isAssignableFrom( this.getClass()) ) { 295 result = method.invoke(this, params ); 296 } else { 297 result = method.invoke(resource, params); 298 } 299 } catch (InvocationTargetException e) { 300 Throwable t = e.getTargetException(); 301 log.error("Exception invoking method " + name , t ); 302 if (t == null) 303 t = e; 304 if (t instanceof RuntimeException ) 305 throw new RuntimeOperationsException  306 ((RuntimeException ) t, "Exception invoking method " + name); 307 else if (t instanceof Error ) 308 throw new RuntimeErrorException  309 ((Error ) t, "Error invoking method " + name); 310 else 311 throw new MBeanException  312 ((Exception )t, "Exception invoking method " + name); 313 } catch (Exception e) { 314 log.error("Exception invoking method " + name , e ); 315 throw new MBeanException  316 (e, "Exception invoking method " + name); 317 } 318 319 return (result); 322 323 } 324 325 static Class getAttributeClass(String signature) 326 throws ReflectionException  327 { 328 if (signature.equals(Boolean.TYPE.getName())) 329 return Boolean.TYPE; 330 else if (signature.equals(Byte.TYPE.getName())) 331 return Byte.TYPE; 332 else if (signature.equals(Character.TYPE.getName())) 333 return Character.TYPE; 334 else if (signature.equals(Double.TYPE.getName())) 335 return Double.TYPE; 336 else if (signature.equals(Float.TYPE.getName())) 337 return Float.TYPE; 338 else if (signature.equals(Integer.TYPE.getName())) 339 return Integer.TYPE; 340 else if (signature.equals(Long.TYPE.getName())) 341 return Long.TYPE; 342 else if (signature.equals(Short.TYPE.getName())) 343 return Short.TYPE; 344 else { 345 try { 346 ClassLoader cl=Thread.currentThread().getContextClassLoader(); 347 if( cl!=null ) 348 return cl.loadClass(signature); 349 } catch( ClassNotFoundException e ) { 350 } 351 try { 352 return Class.forName(signature); 353 } catch (ClassNotFoundException e) { 354 throw new ReflectionException  355 (e, "Cannot find Class for " + signature); 356 } 357 } 358 } 359 360 373 public void setAttribute(Attribute attribute) 374 throws AttributeNotFoundException , MBeanException , 375 ReflectionException  376 { 377 if( log.isDebugEnabled() ) 378 log.debug("Setting attribute " + this + " " + attribute ); 379 380 if( (resource instanceof DynamicMBean ) && 381 ! ( resource instanceof BaseModelMBean )) { 382 try { 383 ((DynamicMBean )resource).setAttribute(attribute); 384 } catch (InvalidAttributeValueException e) { 385 throw new MBeanException (e); 386 } 387 return; 388 } 389 390 if (attribute == null) 392 throw new RuntimeOperationsException  393 (new IllegalArgumentException ("Attribute is null"), 394 "Attribute is null"); 395 396 String name = attribute.getName(); 397 Object value = attribute.getValue(); 398 399 if (name == null) 400 throw new RuntimeOperationsException  401 (new IllegalArgumentException ("Attribute name is null"), 402 "Attribute name is null"); 403 404 Object oldValue=null; 405 408 Method m=managedBean.getSetter(name,this,resource); 409 410 try { 411 if( m.getDeclaringClass().isAssignableFrom( this.getClass()) ) { 412 m.invoke(this, new Object [] { value }); 413 } else { 414 m.invoke(resource, new Object [] { value }); 415 } 416 } catch (InvocationTargetException e) { 417 Throwable t = e.getTargetException(); 418 if (t == null) 419 t = e; 420 if (t instanceof RuntimeException ) 421 throw new RuntimeOperationsException  422 ((RuntimeException ) t, "Exception invoking method " + name); 423 else if (t instanceof Error ) 424 throw new RuntimeErrorException  425 ((Error ) t, "Error invoking method " + name); 426 else 427 throw new MBeanException  428 (e, "Exception invoking method " + name); 429 } catch (Exception e) { 430 log.error("Exception invoking method " + name , e ); 431 throw new MBeanException  432 (e, "Exception invoking method " + name); 433 } 434 try { 435 sendAttributeChangeNotification(new Attribute ( name, oldValue), 436 attribute); 437 } catch(Exception ex) { 438 log.error("Error sending notification " + name, ex); 439 } 440 } 446 447 public String toString() { 448 if( resource==null ) 449 return "BaseModelMbean[" + resourceType + "]"; 450 return resource.toString(); 451 } 452 453 460 public AttributeList setAttributes(AttributeList attributes) { 461 AttributeList response = new AttributeList (); 462 463 if (attributes == null) 465 return response; 466 467 String names[] = new String [attributes.size()]; 469 int n = 0; 470 Iterator items = attributes.iterator(); 471 while (items.hasNext()) { 472 Attribute item = (Attribute ) items.next(); 473 names[n++] = item.getName(); 474 try { 475 setAttribute(item); 476 } catch (Exception e) { 477 ; } 479 } 480 481 return (getAttributes(names)); 482 483 } 484 485 486 488 489 500 public Object getManagedResource() 501 throws InstanceNotFoundException , InvalidTargetObjectTypeException , 502 MBeanException , RuntimeOperationsException { 503 504 if (resource == null) 505 throw new RuntimeOperationsException  506 (new IllegalArgumentException ("Managed resource is null"), 507 "Managed resource is null"); 508 509 return resource; 510 511 } 512 513 514 539 public void setManagedResource(Object resource, String type) 540 throws InstanceNotFoundException , 541 MBeanException , RuntimeOperationsException  542 { 543 if (resource == null) 544 throw new RuntimeOperationsException  545 (new IllegalArgumentException ("Managed resource is null"), 546 "Managed resource is null"); 547 548 551 this.resource = resource; 552 this.resourceType = resource.getClass().getName(); 553 554 } 567 568 569 571 572 583 public void addAttributeChangeNotificationListener 584 (NotificationListener listener, String name, Object handback) 585 throws IllegalArgumentException { 586 587 if (listener == null) 588 throw new IllegalArgumentException ("Listener is null"); 589 if (attributeBroadcaster == null) 590 attributeBroadcaster = new BaseNotificationBroadcaster(); 591 592 if( log.isDebugEnabled() ) 593 log.debug("addAttributeNotificationListener " + listener); 594 595 BaseAttributeFilter filter = new BaseAttributeFilter(name); 596 attributeBroadcaster.addNotificationListener 597 (listener, filter, handback); 598 599 } 600 601 602 613 public void removeAttributeChangeNotificationListener 614 (NotificationListener listener, String name) 615 throws ListenerNotFoundException { 616 617 if (listener == null) 618 throw new IllegalArgumentException ("Listener is null"); 619 if (attributeBroadcaster == null) 620 attributeBroadcaster = new BaseNotificationBroadcaster(); 621 622 attributeBroadcaster.removeNotificationListener(listener); 624 625 } 626 627 628 641 public void removeAttributeChangeNotificationListener 642 (NotificationListener listener, String attributeName, Object handback) 643 throws ListenerNotFoundException { 644 645 removeAttributeChangeNotificationListener(listener, attributeName); 646 647 } 648 649 650 662 public void sendAttributeChangeNotification 663 (AttributeChangeNotification notification) 664 throws MBeanException , RuntimeOperationsException { 665 666 if (notification == null) 667 throw new RuntimeOperationsException  668 (new IllegalArgumentException ("Notification is null"), 669 "Notification is null"); 670 if (attributeBroadcaster == null) 671 return; if( log.isDebugEnabled() ) 673 log.debug( "AttributeChangeNotification " + notification ); 674 attributeBroadcaster.sendNotification(notification); 675 676 } 677 678 679 691 public void sendAttributeChangeNotification 692 (Attribute oldValue, Attribute newValue) 693 throws MBeanException , RuntimeOperationsException { 694 695 String type = null; 697 if (newValue.getValue() != null) 698 type = newValue.getValue().getClass().getName(); 699 else if (oldValue.getValue() != null) 700 type = oldValue.getValue().getClass().getName(); 701 else 702 return; 704 AttributeChangeNotification notification = 705 new AttributeChangeNotification  706 (this, 1, System.currentTimeMillis(), 707 "Attribute value has changed", 708 oldValue.getName(), type, 709 oldValue.getValue(), newValue.getValue()); 710 sendAttributeChangeNotification(notification); 711 712 } 713 714 715 716 717 728 public void sendNotification(Notification notification) 729 throws MBeanException , RuntimeOperationsException { 730 731 if (notification == null) 732 throw new RuntimeOperationsException  |