1 16 17 18 package org.apache.commons.modeler; 19 20 21 import java.lang.reflect.InvocationTargetException ; 22 import java.lang.reflect.Method ; 23 import java.util.HashMap ; 24 import java.util.Hashtable ; 25 import java.util.Iterator ; 26 27 import javax.management.Attribute ; 28 import javax.management.AttributeChangeNotification ; 29 import javax.management.AttributeList ; 30 import javax.management.AttributeNotFoundException ; 31 import javax.management.Descriptor ; 32 import javax.management.DynamicMBean ; 33 import javax.management.InstanceNotFoundException ; 34 import javax.management.InvalidAttributeValueException ; 35 import javax.management.ListenerNotFoundException ; 36 import javax.management.MBeanException ; 37 import javax.management.MBeanInfo ; 38 import javax.management.MBeanNotificationInfo ; 39 import javax.management.MBeanRegistration ; 40 import javax.management.MBeanServer ; 41 import javax.management.Notification ; 42 import javax.management.NotificationFilter ; 43 import javax.management.NotificationListener ; 44 import javax.management.ObjectName ; 45 import javax.management.ReflectionException ; 46 import javax.management.RuntimeErrorException ; 47 import javax.management.RuntimeOperationsException ; 48 import javax.management.ServiceNotFoundException ; 49 import javax.management.modelmbean.DescriptorSupport ; 50 import javax.management.modelmbean.InvalidTargetObjectTypeException ; 51 import javax.management.modelmbean.ModelMBean ; 52 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 53 import javax.management.modelmbean.ModelMBeanInfo ; 54 import javax.management.modelmbean.ModelMBeanInfoSupport ; 55 import javax.management.modelmbean.ModelMBeanNotificationInfo ; 56 import javax.management.modelmbean.ModelMBeanOperationInfo ; 57 58 import org.apache.commons.logging.Log; 59 import org.apache.commons.logging.LogFactory; 60 import org.apache.commons.modeler.modules.ModelerSource; 61 62 64 97 98 public class BaseModelMBean implements ModelMBean , MBeanRegistration { 99 private static Log log = LogFactory.getLog(BaseModelMBean.class); 100 101 103 112 public BaseModelMBean() throws MBeanException , RuntimeOperationsException { 113 114 super(); 115 if( log.isDebugEnabled()) log.debug("default constructor"); 116 setModelMBeanInfo(createDefaultModelMBeanInfo()); 117 } 118 119 120 131 public BaseModelMBean(ModelMBeanInfo info) 132 throws MBeanException , RuntimeOperationsException { 133 super(); 135 setModelMBeanInfo(info); 136 if( log.isDebugEnabled()) log.debug("ModelMBeanInfo constructor"); 137 } 138 139 151 public BaseModelMBean( String type ) 152 throws MBeanException , RuntimeOperationsException 153 { 154 try { 155 setModeledType(type); 158 } catch( Throwable ex ) { 159 log.error( "Error creating mbean ", ex); 160 } 161 } 162 163 public BaseModelMBean( String type, ModelerSource source ) 164 throws MBeanException , RuntimeOperationsException 165 { 166 try { 167 setModeledType(type); 168 } catch( Throwable ex ) { 169 log.error( "Error creating mbean ", ex); 170 } 171 this.source=source; 172 } 173 174 176 177 180 protected BaseNotificationBroadcaster attributeBroadcaster = null; 181 182 184 protected Registry registry=null; 185 186 189 protected BaseNotificationBroadcaster generalBroadcaster = null; 190 191 protected ObjectName oname=null; 192 193 196 protected ModelMBeanInfo info = null; 197 198 199 202 protected Object resource = null; 203 protected String resourceType = null; 204 205 208 protected ModelerSource source=null; 209 210 212 protected HashMap attributes=new HashMap (); 213 214 static final Object [] NO_ARGS_PARAM=new Object [0]; 216 static final Class [] NO_ARGS_PARAM_SIG=new Class [0]; 217 private Hashtable getAttMap=new Hashtable (); 219 220 private Hashtable setAttMap=new Hashtable (); 222 223 private Hashtable invokeAttMap=new Hashtable (); 225 226 238 public Object getAttribute(String name) 239 throws AttributeNotFoundException , MBeanException , 240 ReflectionException { 241 if (name == null) 243 throw new RuntimeOperationsException 244 (new IllegalArgumentException ("Attribute name is null"), 245 "Attribute name is null"); 246 247 if( (resource instanceof DynamicMBean ) && 248 ! ( resource instanceof BaseModelMBean )) { 249 return ((DynamicMBean )resource).getAttribute(name); 250 } 251 252 Method m=(Method )getAttMap.get( name ); 254 255 if( m==null ) { 256 ModelMBeanAttributeInfo attrInfo = info.getAttribute(name); 258 if (attrInfo == null) 259 throw new AttributeNotFoundException (" Cannot find attribute " + name); 260 Descriptor attrDesc = attrInfo.getDescriptor(); 261 if (attrDesc == null) 262 throw new AttributeNotFoundException ("Cannot find attribute " + name + " descriptor"); 263 String getMethod = (String ) attrDesc.getFieldValue("getMethod"); 264 265 if (getMethod == null) 266 throw new AttributeNotFoundException ("Cannot find attribute " + name + " get method name"); 267 268 Object object = null; 269 NoSuchMethodException exception = null; 270 try { 271 object = this; 272 m = object.getClass().getMethod(getMethod, NO_ARGS_PARAM_SIG); 273 } catch (NoSuchMethodException e) { 274 exception = e;; 275 } 276 if( m== null && resource != null ) { 277 try { 278 object = resource; 279 m = object.getClass().getMethod(getMethod, NO_ARGS_PARAM_SIG); 280 exception=null; 281 } catch (NoSuchMethodException e) { 282 exception = e; 283 } 284 } 285 if( exception != null ) 286 throw new ReflectionException (exception, 287 "Cannot find getter method " + getMethod); 288 getAttMap.put( name, m ); 289 } 290 291 Object result = null; 292 try { 293 Class declaring=m.getDeclaringClass(); 294 if( declaring.isAssignableFrom(this.getClass()) ) { 297 result = m.invoke(this, NO_ARGS_PARAM ); 298 } else { 299 result = m.invoke(resource, NO_ARGS_PARAM ); 300 } 301 } catch (InvocationTargetException e) { 302 Throwable t = e.getTargetException(); 303 if (t == null) 304 t = e; 305 if (t instanceof RuntimeException ) 306 throw new RuntimeOperationsException 307 ((RuntimeException ) t, "Exception invoking method " + name); 308 else if (t instanceof Error ) 309 throw new RuntimeErrorException 310 ((Error ) t, "Error invoking method " + name); 311 else 312 throw new MBeanException 313 (e, "Exception invoking method " + name); 314 } catch (Exception e) { 315 throw new MBeanException 316 (e, "Exception invoking method " + name); 317 } 318 319 return (result); 322 } 323 324 325 330 public AttributeList getAttributes(String names[]) { 331 332 if (names == null) 334 throw new RuntimeOperationsException 335 (new IllegalArgumentException ("Attribute names list is null"), 336 "Attribute names list is null"); 337 338 AttributeList response = new AttributeList (); 340 for (int i = 0; i < names.length; i++) { 341 try { 342 response.add(new Attribute (names[i],getAttribute(names[i]))); 343 } catch (Exception e) { 344 ; ; } 347 } 348 return (response); 349 350 } 351 352 353 356 public MBeanInfo getMBeanInfo() { 357 if( info== null ) return null; 359 return ((MBeanInfo ) info.clone()); 360 } 361 362 363 382 public Object invoke(String name, Object params[], String signature[]) 383 throws MBeanException , ReflectionException 384 { 385 if( (resource instanceof DynamicMBean ) && 386 ! ( resource instanceof BaseModelMBean )) { 387 return ((DynamicMBean )resource).invoke(name, params, signature); 388 } 389 390 if (name == null) 392 throw new RuntimeOperationsException 393 (new IllegalArgumentException ("Method name is null"), 394 "Method name is null"); 395 396 if( log.isDebugEnabled()) log.debug("Invoke " + name); 397 Method method=(Method )invokeAttMap.get(name); 398 if( method==null ) { 399 if (params == null) 400 params = new Object [0]; 401 if (signature == null) 402 signature = new String [0]; 403 if (params.length != signature.length) 404 throw new RuntimeOperationsException 405 (new IllegalArgumentException ("Inconsistent arguments and signature"), 406 "Inconsistent arguments and signature"); 407 408 ModelMBeanOperationInfo opInfo = info.getOperation(name); 411 if (opInfo == null) 412 throw new MBeanException 413 (new ServiceNotFoundException ("Cannot find operation " + name), 414 "Cannot find operation " + name); 415 416 Class types[] = new Class [signature.length]; 419 for (int i = 0; i < signature.length; i++) { 420 types[i]=getAttributeClass( signature[i] ); 421 } 422 423 Object object = null; 427 Exception exception = null; 428 try { 429 object = this; 430 method = object.getClass().getMethod(name, types); 431 } catch (NoSuchMethodException e) { 432 exception = e;; 433 } 434 try { 435 if ((method == null) && (resource != null)) { 436 object = resource; 437 method = object.getClass().getMethod(name, types); 438 } 439 } catch (NoSuchMethodException e) { 440 exception = e; 441 } 442 if (method == null) { 443 throw new ReflectionException (exception, 444 "Cannot find method " + name + 445 " with this signature"); 446 } 447 invokeAttMap.put( name, method ); 448 } 449 450 Object result = null; 452 try { 453 if( method.getDeclaringClass().isAssignableFrom( this.getClass()) ) { 454 result = method.invoke(this, params ); 455 } else { 456 result = method.invoke(resource, params); 457 } 458 } catch (InvocationTargetException e) { 459 Throwable t = e.getTargetException(); 460 log.error("Exception invoking method " + name , t ); 461 if (t == null) 462 t = e; 463 if (t instanceof RuntimeException ) 464 throw new RuntimeOperationsException 465 ((RuntimeException ) t, "Exception invoking method " + name); 466 else if (t instanceof Error ) 467 throw new RuntimeErrorException 468 ((Error ) t, "Error invoking method " + name); 469 else 470 throw new MBeanException 471 ((Exception )t, "Exception invoking method " + name); 472 } catch (Exception e) { 473 log.error("Exception invoking method " + name , e ); 474 throw new MBeanException 475 (e, "Exception invoking method " + name); 476 } 477 478 return (result); 481 482 } 483 484 private Class getAttributeClass(String signature) 485 throws ReflectionException 486 { 487 if (signature.equals(Boolean.TYPE.getName())) 488 return Boolean.TYPE; 489 else if (signature.equals(Byte.TYPE.getName())) 490 return Byte.TYPE; 491 else if (signature.equals(Character.TYPE.getName())) 492 return Character.TYPE; 493 else if (signature.equals(Double.TYPE.getName())) 494 return Double.TYPE; 495 else if (signature.equals(Float.TYPE.getName())) 496 return Float.TYPE; 497 else if (signature.equals(Integer.TYPE.getName())) 498 return Integer.TYPE; 499 else if (signature.equals(Long.TYPE.getName())) 500 return Long.TYPE; 501 else if (signature.equals(Short.TYPE.getName())) 502 return Short.TYPE; 503 else { 504 try { 505 ClassLoader cl=Thread.currentThread().getContextClassLoader(); 506 if( cl!=null ) 507 return cl.loadClass(signature); 508 } catch( ClassNotFoundException e ) { 509 } 510 try { 511 return Class.forName(signature); 512 } catch (ClassNotFoundException e) { 513 throw new ReflectionException 514 (e, "Cannot find Class for " + signature); 515 } 516 } 517 } 518 519 532 public void setAttribute(Attribute attribute) 533 throws AttributeNotFoundException , MBeanException , 534 ReflectionException 535 { 536 if( log.isDebugEnabled() ) 537 log.debug("Setting attribute " + this + " " + attribute ); 538 539 if( (resource instanceof DynamicMBean ) && 540 ! ( resource instanceof BaseModelMBean )) { 541 try { 542 ((DynamicMBean )resource).setAttribute(attribute); 543 } catch (InvalidAttributeValueException e) { 544 throw new MBeanException (e); 545 } 546 return; 547 } 548 549 if (attribute == null) 551 throw new RuntimeOperationsException 552 (new IllegalArgumentException ("Attribute is null"), 553 "Attribute is null"); 554 555 String name = attribute.getName(); 556 Object value = attribute.getValue(); 557 558 if (name == null) 559 throw new RuntimeOperationsException 560 (new IllegalArgumentException ("Attribute name is null"), 561 "Attribute name is null"); 562 563 ModelMBeanAttributeInfo attrInfo=info.getAttribute(name); 564 if (attrInfo == null) 565 throw new AttributeNotFoundException ("Cannot find attribute " + name); 566 567 Descriptor attrDesc=attrInfo.getDescriptor(); 568 if (attrDesc == null) 569 throw new AttributeNotFoundException ("Cannot find attribute " + name + " descriptor"); 570 571 try { 572 Object oldValue=null; 574 if( getAttMap.get(name) != null ) 575 oldValue=getAttribute( name ); 576 sendAttributeChangeNotification(new Attribute ( name, oldValue), 577 attribute); 578 } catch( Exception ex ) { 579 log.error( "Error sending notification " + name, ex ); 580 } 581 582 Method m=(Method )setAttMap.get( name ); 584 585 if( m==null ) { 586 String setMethod = (String ) attrDesc.getFieldValue("setMethod"); 588 if (setMethod == null) 589 throw new AttributeNotFoundException ("Cannot find attribute " + name + " set method name"); 590 591 String argType=attrInfo.getType(); 592 593 Class signature[] = new Class [] { getAttributeClass( argType ) }; 594 595 Object object = null; 596 NoSuchMethodException exception = null; 597 try { 598 object = this; 599 m = object.getClass().getMethod(setMethod, signature); 600 } catch (NoSuchMethodException e) { 601 exception = e;; 602 } 603 if( m== null && resource != null ) { 604 try { 605 object = resource; 606 m = object.getClass().getMethod(setMethod, signature); 607 exception=null; 608 } catch (NoSuchMethodException e) { 609 if( log.isDebugEnabled()) 610 log.debug("Method not found in resource " +resource); 611 exception = e; 612 } 613 } 614 if( exception != null ) 615 throw new ReflectionException (exception, 616 "Cannot find setter method " + setMethod + 617 " " + resource); 618 setAttMap.put( name, m ); 619 } 620 621 Object result = null; 622 try { 623 if( m.getDeclaringClass().isAssignableFrom( this.getClass()) ) { 624 result = m.invoke(this, new Object [] { value }); 625 } else { 626 result = m.invoke(resource, new Object [] { value }); 627 } 628 } catch (InvocationTargetException e) { 629 Throwable t = e.getTargetException(); 630 if (t == null) 631 t = e; 632 if (t instanceof RuntimeException ) 633 throw new RuntimeOperationsException 634 ((RuntimeException ) t, "Exception invoking method " + name); 635 else if (t instanceof Error ) 636 throw new RuntimeErrorException 637 ((Error ) t, "Error invoking method " + name); 638 else 639 throw new MBeanException 640 (e, "Exception invoking method " + name); 641 } catch (Exception e) { 642 log.error("Exception invoking method " + name , e ); 643 throw new MBeanException 644 (e, "Exception invoking method " + name); 645 } 646 647 attributes.put( name, value ); 648 if( source != null ) { 649 source.updateField(oname, name, value); 651 } 652 } 653 654 public String toString() { 655 if( resource==null ) 656 return "BaseModelMbean[" + resourceType + "]"; 657 return resource.toString(); 658 } 659 660 667 public AttributeList setAttributes(AttributeList attributes) { 668 669 if (attributes == null) 671 throw new RuntimeOperationsException 672 (new IllegalArgumentException ("Attributes list is null"), 673 "Attributes list is null"); 674 675 AttributeList response = new AttributeList (); 677 String names[] = new String [attributes.size()]; 678 int n = 0; 679 Iterator items = attributes.iterator(); 680 while (items.hasNext()) { 681 Attribute item = (Attribute ) items.next(); 682 names[n++] = item.getName(); 683 try { 684 setAttribute(item); 685 } catch (Exception e) { 686 ; } 688 } 689 690 return (getAttributes(names)); 691 692 } 693 694 695 697 698 709 public Object getManagedResource() 710 throws InstanceNotFoundException , InvalidTargetObjectTypeException , 711 MBeanException , RuntimeOperationsException { 712 713 if (resource == null) 714 throw new RuntimeOperationsException 715 (new IllegalArgumentException ("Managed resource is null"), 716 "Managed resource is null"); 717 718 return resource; 719 720 } 721 722 723 746 public void setManagedResource(Object resource, String type) 747 throws InstanceNotFoundException , InvalidTargetObjectTypeException , 748 MBeanException , RuntimeOperationsException 749 { 750 if (resource == null) 751 throw new RuntimeOperationsException 752 (new IllegalArgumentException ("Managed resource is null"), 753 "Managed resource is null"); 754 755 if (!"objectreference".equalsIgnoreCase(type)) 756 throw new InvalidTargetObjectTypeException (type); 757 758 this.resource = resource; 759 this.resourceType = resource.getClass().getName(); 760 761 try { 763 Method m=resource.getClass().getMethod("setModelMBean", 764 new Class [] {ModelMBean .class}); 765 if( m!= null ) { 766 m.invoke(resource, new Object [] {this}); 767 } 768 } catch( NoSuchMethodException t ) { 769 } catch( Throwable t ) { 771 log.error( "Can't set model mbean ", t ); 772 } 773 } 774 775 776 792 public void setModelMBeanInfo(ModelMBeanInfo info) 793 throws MBeanException , RuntimeOperationsException { 794 795 if (info == null) 796 throw new RuntimeOperationsException 797 (new IllegalArgumentException ("ModelMBeanInfo is null"), 798 "ModelMBeanInfo is null"); 799 800 if (!isModelMBeanInfoValid(info)) 801 throw new RuntimeOperationsException 802 (new IllegalArgumentException ("ModelMBeanInfo is invalid"), 803 "ModelMBeanInfo is invalid"); 804 805 this.info = (ModelMBeanInfo ) info.clone(); 806 807 } 808 809 810 812 813 824 public void addAttributeChangeNotificationListener 825 (NotificationListener listener, String name, Object handback) 826 throws IllegalArgumentException { 827 828 if (listener == null) 829 throw new IllegalArgumentException ("Listener is null"); 830 if (attributeBroadcaster == null) 831 attributeBroadcaster = new BaseNotificationBroadcaster(); 832 833 if( log.isDebugEnabled() ) 834 log.debug("addAttributeNotificationListener " + listener); 835 836 BaseAttributeFilter filter = new BaseAttributeFilter(name); 837 attributeBroadcaster.addNotificationListener 838 (listener, filter, handback); 839 840 } 841 842 843 854 public void removeAttributeChangeNotificationListener 855 (NotificationListener listener, String name) 856 throws ListenerNotFoundException { 857 858 if (listener == null) 859 throw new IllegalArgumentException ("Listener is null"); 860 if (attributeBroadcaster == null) 861 attributeBroadcaster = new BaseNotificationBroadcaster(); 862 863 attributeBroadcaster.removeNotificationListener(listener); 865 866 } 867 868 869 882 public void removeAttributeChangeNotificationListener 883 (NotificationListener listener, String attributeName, Object handback) 884 throws ListenerNotFoundException { 885 886 removeAttributeChangeNotificationListener(listener, attributeName); 887 888 } 889 890 891 903 public void sendAttributeChangeNotification 904 (AttributeChangeNotification notification) 905 throws MBeanException , RuntimeOperationsException { 906 907 if (notification == null) 908 throw new RuntimeOperationsException 909 (new IllegalArgumentException ("Notification is null"), 910 "Notification is null"); 911 if (attributeBroadcaster == null) 912 return; if( log.isDebugEnabled() ) 914 log.debug( "AttributeChangeNotification " + notification ); 915 attributeBroadcaster.sendNotification(notification); 916 917 } 918 919 920 932 public void sendAttributeChangeNotification 933 (Attribute oldValue, Attribute newValue) 934 throws MBeanException , RuntimeOperationsException { 935 936 String type = null; 938 if (newValue.getValue() != null) 939 type = newValue.getValue().getClass().getName(); 940 else if (oldValue.getValue() != null) 941 type = oldValue.getValue().getClass().getName(); 942 else 943 return; 945 AttributeChangeNotification notification = 946 new AttributeChangeNotification 947 (this, 1, System.currentTimeMillis(), 948 "Attribute value has changed", 949 oldValue.getName(), type, 950 oldValue.getValue(), newValue.getValue()); 951 sendAttributeChangeNotification(notification); 952 953 } 954 955 956 957 958 969 public void sendNotification(Notification notification) 970 throws MBeanException , RuntimeOperationsException { 971 972 if (notification == null) 973 throw new RuntimeOperationsException 974 (new IllegalArgumentException ("Notification is null"), 975 "Notification is null"); 976 if (generalBroadcaster == null) 977 return; generalBroadcaster.sendNotification(notification); 979 980 } 981 982 983 994 public void sendNotification(String message) 995 throws MBeanException , RuntimeOperationsException { 996 997 if (message == null) 998 throw new RuntimeOperationsException 999 (new IllegalArgumentException ("Message is null"), 1000 "Message is null"); 1001 Notification notification = new Notification 1002 ("jmx.modelmbean.generic", this, 1, message); 1003 sendNotification(notification); 1004 1005 } 1006 1007 1008 1009 1010 1012 1013 1024 public void addNotificationListener(NotificationListener listener, 1025 NotificationFilter filter, 1026 Object handback) 1027 throws IllegalArgumentException { 1028 1029 if (listener == null) 1030 throw new IllegalArgumentException ("Listener is null"); 1031 1032 if( log.isDebugEnabled() ) log.debug("addNotificationListener " + listener); 1033 1034 if (generalBroadcaster == null) 1035 generalBroadcaster = new BaseNotificationBroadcaster(); 1036 generalBroadcaster.addNotificationListener 1037 (listener, filter, handback); 1038 1039 if (attributeBroadcaster == null) 1044 attributeBroadcaster = new BaseNotificationBroadcaster(); 1045 1046 if( log.isDebugEnabled() ) 1047 log.debug("addAttributeNotificationListener " + listener); 1048 1049 attributeBroadcaster.addNotificationListener 1050 (listener, filter, handback); 1051 } 1052 1053 1054 1058 public MBeanNotificationInfo [] getNotificationInfo() { 1059 1060 MBeanNotificationInfo current[] = info.getNotifications(); 1062 if (current == null) 1063 current = new MBeanNotificationInfo [0]; 1064 MBeanNotificationInfo response[] = 1065 new MBeanNotificationInfo [current.length + 2]; 1066 Descriptor descriptor = null; 1067 1068 descriptor = new DescriptorSupport 1070 (new String [] { "name=GENERIC", 1071 "descriptorType=notification", 1072 "log=T", 1073 "severity=5", 1074 "displayName=jmx.modelmbean.generic" }); 1075 response[0] = new ModelMBeanNotificationInfo 1076 (new String [] { "jmx.modelmbean.generic" }, 1077 "GENERIC", 1078 "Text message notification from the managed resource", 1079 descriptor); 1080 1081 descriptor = new DescriptorSupport 1083 (new String [] { "name=ATTRIBUTE_CHANGE", 1084 "descriptorType=notification", 1085 "log=T", 1086 "severity=5", 1087 "displayName=jmx.attribute.change" }); 1088 response[1] = new ModelMBeanNotificationInfo 1089 (new String [] { "jmx.attribute.change" }, 1090 "ATTRIBUTE_CHANGE", 1091 "Observed MBean attribute value has changed", 1092 descriptor); 1093 1094 System.arraycopy(current, 0, response, 2, current.length); 1096 return (response); 1097 1098 } 1099 1100 1101 1110 public void removeNotificationListener(NotificationListener listener) 1111 throws ListenerNotFoundException { 1112 1113 if (listener == null) 1114 throw new IllegalArgumentException ("Listener is null"); 1115 if (generalBroadcaster == null) 1116 generalBroadcaster = new BaseNotificationBroadcaster(); 1117 generalBroadcaster.removeNotificationListener(listener); 1118 1119 1120 } 1121 1122 1123 1134 public void removeNotificationListener(NotificationListener listener, 1135 Object handback) 1136 throws ListenerNotFoundException { 1137 1138 removeNotificationListener(listener); 1139 1140 } 1141 1142 1143 1156 public void removeNotificationListener(NotificationListener listener, 1157 NotificationFilter filter, 1158 Object handback) 1159 throws ListenerNotFoundException { 1160 1161 removeNotificationListener(listener); 1162 1163 } 1164 1165 1166 1168 1169 1186 public void load() throws InstanceNotFoundException , 1187 MBeanException , RuntimeOperationsException { 1188 throw new MBeanException 1190 (new IllegalStateException ("Persistence is not supported"), 1191 "Persistence is not supported"); 1192 1193 } 1194 1195 1196 1212 public void store() throws InstanceNotFoundException , 1213 MBeanException , RuntimeOperationsException { 1214 1215 throw new MBeanException 1217 (new IllegalStateException ("Persistence is not supported"), 1218 "Persistence is not supported"); 1219 1220 } 1221 1222 1224 1229 public void setModeledType( String type ) { 1230 initModelInfo(type); 1231 createResource(); 1232 } 1233 1238 protected void initModelInfo( String type ) { 1239 try { 1240 if( log.isDebugEnabled()) 1241 log.debug("setModeledType " + type); 1242 1243 log.debug( "Set model Info " + type); 1244 if(type==null) { 1245 return; 1246 } 1247 resourceType=type; 1248 Class c=null; 1250 try { 1251 c=Class.forName( type); 1252 } catch( Throwable t ) { 1253 log.debug( "Error creating class " + t); 1254 } 1255 1256 ManagedBean descriptor=getRegistry().findManagedBean(c, type); 1258 if( descriptor==null ) 1259 return; 1260 this.setModelMBeanInfo(descriptor.createMBeanInfo()); 1261 } catch( Throwable ex) { 1262 log.error( "TCL: " + Thread.currentThread().getContextClassLoader(), 1263 ex); 1264 } 1265 } 1266 1267 1270 protected void createResource() { 1271 try { 1272 Class c=null; 1274 try { 1275 c=Class.forName( resourceType ); 1276 resource = c.newInstance(); 1277 } catch( Throwable t ) { 1278 log.error( "Error creating class " + t); 1279 } 1280 } catch( Throwable ex) { 1281 log.error( "TCL: " + Thread.currentThread().getContextClassLoader(), 1282 ex); 1283 } 1284 } 1285 1286 1287 public String getModelerType() { 1288 return resourceType; 1289 } 1290 1291 public String getClassName() { 1292 return getModelerType(); 1293 } 1294 1295 public ObjectName getJmxName() { 1296 return oname; 1297 } 1298 1299 public String getObjectName() { 1300 if (oname != null) { 1301 return oname.toString(); 1302 } else { 1303 return null; 1304 } 1305 } 1306 1307 public void setRegistry(Registry registry) { 1308 this.registry = registry; 1309 } 1310 1311 public Registry getRegistry() { 1312 if( registry == null ) 1314 registry=Registry.getRegistry(); 1315 1316 return registry; 1317 } 1318 1319 1321 1322 1325 protected ModelMBeanInfo createDefaultModelMBeanInfo() { 1326 1327 return (new ModelMBeanInfoSupport (this.getClass().getName(), 1328 "Default ModelMBean", 1329 null, null, null, null)); 1330 1331 } 1332 1333 1342 protected boolean isModelMBeanInfoValid(ModelMBeanInfo info) { 1343 return (true); 1344 } 1345 1346 1350 public ObjectName preRegister(MBeanServer server, 1351 ObjectName name) 1352 throws Exception 1353 { 1354 if( log.isDebugEnabled()) 1355 log.debug("preRegister " + resource + " " + name ); 1356 oname=name; 1357 if( resource instanceof MBeanRegistration ) { 1358 oname = ((MBeanRegistration )resource).preRegister(server, name ); 1359 } 1360 return oname; 1361 } 1362 1363 public void postRegister(Boolean registrationDone) { 1364 if( resource instanceof MBeanRegistration ) { 1365 ((MBeanRegistration )resource).postRegister(registrationDone); 1366 } 1367 } 1368 1369 public void preDeregister() throws Exception { 1370 if( resource instanceof MBeanRegistration ) { 1371 ((MBeanRegistration )resource).preDeregister(); 1372 } 1373 } 1374 1375 public void postDeregister() { 1376 if( resource instanceof MBeanRegistration ) { 1377 ((MBeanRegistration )resource).postDeregister(); 1378 } 1379 } 1380} 1381 | Popular Tags |