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 733 (new IllegalArgumentException ("Notification is null"), 734 "Notification is null"); 735 if (generalBroadcaster == null) 736 return; generalBroadcaster.sendNotification(notification); 738 739 } 740 741 742 753 public void sendNotification(String message) 754 throws MBeanException , RuntimeOperationsException { 755 756 if (message == null) 757 throw new RuntimeOperationsException 758 (new IllegalArgumentException ("Message is null"), 759 "Message is null"); 760 Notification notification = new Notification 761 ("jmx.modelmbean.generic", this, 1, message); 762 sendNotification(notification); 763 764 } 765 766 767 768 769 771 772 783 public void addNotificationListener(NotificationListener listener, 784 NotificationFilter filter, 785 Object handback) 786 throws IllegalArgumentException { 787 788 if (listener == null) 789 throw new IllegalArgumentException ("Listener is null"); 790 791 if( log.isDebugEnabled() ) log.debug("addNotificationListener " + listener); 792 793 if (generalBroadcaster == null) 794 generalBroadcaster = new BaseNotificationBroadcaster(); 795 generalBroadcaster.addNotificationListener 796 (listener, filter, handback); 797 798 if (attributeBroadcaster == null) 803 attributeBroadcaster = new BaseNotificationBroadcaster(); 804 805 if( log.isDebugEnabled() ) 806 log.debug("addAttributeNotificationListener " + listener); 807 808 attributeBroadcaster.addNotificationListener 809 (listener, filter, handback); 810 } 811 812 813 817 public MBeanNotificationInfo [] getNotificationInfo() { 818 819 MBeanNotificationInfo current[] = getMBeanInfo().getNotifications(); 821 if (current == null) 822 current = new MBeanNotificationInfo [0]; 823 MBeanNotificationInfo response[] = 824 new MBeanNotificationInfo [current.length + 2]; 825 827 response[0] = new MBeanNotificationInfo 835 (new String [] { "jmx.modelmbean.generic" }, 836 "GENERIC", 837 "Text message notification from the managed resource"); 838 840 response[1] = new MBeanNotificationInfo 848 (new String [] { "jmx.attribute.change" }, 849 "ATTRIBUTE_CHANGE", 850 "Observed MBean attribute value has changed"); 851 853 System.arraycopy(current, 0, response, 2, current.length); 855 return (response); 856 857 } 858 859 860 869 public void removeNotificationListener(NotificationListener listener) 870 throws ListenerNotFoundException { 871 872 if (listener == null) 873 throw new IllegalArgumentException ("Listener is null"); 874 if (generalBroadcaster == null) 875 generalBroadcaster = new BaseNotificationBroadcaster(); 876 generalBroadcaster.removeNotificationListener(listener); 877 878 879 } 880 881 882 893 public void removeNotificationListener(NotificationListener listener, 894 Object handback) 895 throws ListenerNotFoundException { 896 897 removeNotificationListener(listener); 898 899 } 900 901 902 915 public void removeNotificationListener(NotificationListener listener, 916 NotificationFilter filter, 917 Object handback) 918 throws ListenerNotFoundException { 919 920 removeNotificationListener(listener); 921 922 } 923 924 925 927 928 945 954 955 971 981 983 988 997 1026 1029 1045 1046 public String getModelerType() { 1047 return resourceType; 1048 } 1049 1050 public String getClassName() { 1051 return getModelerType(); 1052 } 1053 1054 public ObjectName getJmxName() { 1055 return oname; 1056 } 1057 1058 public String getObjectName() { 1059 if (oname != null) { 1060 return oname.toString(); 1061 } else { 1062 return null; 1063 } 1064 } 1065 1066 1078 1080 1081 1084 1092 1101 1105 1109 public ObjectName preRegister(MBeanServer server, 1110 ObjectName name) 1111 throws Exception 1112 { 1113 if( log.isDebugEnabled()) 1114 log.debug("preRegister " + resource + " " + name ); 1115 oname=name; 1116 if( resource instanceof MBeanRegistration ) { 1117 oname = ((MBeanRegistration )resource).preRegister(server, name ); 1118 } 1119 return oname; 1120 } 1121 1122 public void postRegister(Boolean registrationDone) { 1123 if( resource instanceof MBeanRegistration ) { 1124 ((MBeanRegistration )resource).postRegister(registrationDone); 1125 } 1126 } 1127 1128 public void preDeregister() throws Exception { 1129 if( resource instanceof MBeanRegistration ) { 1130 ((MBeanRegistration )resource).preDeregister(); 1131 } 1132 } 1133 1134 public void postDeregister() { 1135 if( resource instanceof MBeanRegistration ) { 1136 ((MBeanRegistration )resource).postDeregister(); 1137 } 1138 } 1139 1140 static class MethodKey { 1141 private String name; 1142 private String [] signature; 1143 1144 MethodKey(String name, String [] signature) { 1145 this.name = name; 1146 if(signature == null) { 1147 signature = new String [0]; 1148 } 1149 this.signature = signature; 1150 } 1151 1152 public boolean equals(Object other) { 1153 if(!(other instanceof MethodKey)) { 1154 return false; 1155 } 1156 MethodKey omk = (MethodKey)other; 1157 if(!name.equals(omk.name)) { 1158 return false; 1159 } 1160 if(signature.length != omk.signature.length) { 1161 return false; 1162 } 1163 for(int i=0; i < signature.length; i++) { 1164 if(!signature[i].equals(omk.signature[i])) { 1165 return false; 1166 } 1167 } 1168 return true; 1169 } 1170 1171 public int hashCode() { 1172 return name.hashCode(); 1173 } 1174 } 1175} 1176 | Popular Tags |