1 16 package org.mortbay.util.jmx; 17 18 import java.lang.reflect.InvocationTargetException ; 19 import java.lang.reflect.Method ; 20 import java.lang.reflect.Modifier ; 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Locale ; 25 import java.util.Map ; 26 import java.util.ResourceBundle ; 27 28 import javax.management.Attribute ; 29 import javax.management.AttributeChangeNotification ; 30 import javax.management.AttributeList ; 31 import javax.management.AttributeNotFoundException ; 32 import javax.management.InstanceNotFoundException ; 33 import javax.management.InvalidAttributeValueException ; 34 import javax.management.ListenerNotFoundException ; 35 import javax.management.MBeanException ; 36 import javax.management.MBeanInfo ; 37 import javax.management.MBeanNotificationInfo ; 38 import javax.management.MBeanOperationInfo ; 39 import javax.management.MBeanParameterInfo ; 40 import javax.management.MBeanRegistration ; 41 import javax.management.MBeanServer ; 42 import javax.management.Notification ; 43 import javax.management.NotificationFilter ; 44 import javax.management.NotificationListener ; 45 import javax.management.ObjectName ; 46 import javax.management.ReflectionException ; 47 import javax.management.RuntimeOperationsException ; 48 import javax.management.modelmbean.InvalidTargetObjectTypeException ; 49 import javax.management.modelmbean.ModelMBean ; 50 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 51 import javax.management.modelmbean.ModelMBeanInfo ; 52 import javax.management.modelmbean.ModelMBeanInfoSupport ; 53 import javax.management.modelmbean.ModelMBeanNotificationInfo ; 54 import javax.management.modelmbean.ModelMBeanOperationInfo ; 55 56 import org.apache.commons.logging.Log; 57 import org.mortbay.log.LogFactory; 58 import org.mortbay.util.LogSupport; 59 import org.mortbay.util.TypeUtil; 60 61 62 63 87 public class ModelMBeanImpl 88 implements ModelMBean , 89 MBeanRegistration 90 { 91 private static Log log = LogFactory.getLog(ModelMBeanImpl.class); 92 93 public final static int IMPACT_ACTION = MBeanOperationInfo.ACTION; 94 public final static int IMPACT_ACTION_INFO = MBeanOperationInfo.ACTION_INFO; 95 public final static int IMPACT_INFO = MBeanOperationInfo.INFO; 96 public final static int IMPACT_UNKOWN = MBeanOperationInfo.UNKNOWN; 97 98 public final static String STRING="java.lang.String"; 99 public final static String OBJECT="java.lang.Object"; 100 public final static String INT="int"; 101 102 public final static String [] NO_PARAMS=new String [0]; 103 104 public final static boolean READ_WRITE=true; 105 public final static boolean READ_ONLY=false; 106 public final static boolean ON_MBEAN=true; 107 public final static boolean ON_OBJECT=false; 108 109 110 private static HashMap __objectId = new HashMap (); 111 112 private static String __defaultDomain="org.mortbay"; 113 114 protected ModelMBeanInfoSupport _beanInfo; 115 private MBeanServer _mBeanServer; 116 private Object _object; 117 private ObjectName _objectName; 118 119 private boolean _dirty=false; 120 private HashMap _getter = new HashMap (4); 121 private HashMap _setter = new HashMap (4); 122 private HashMap _method = new HashMap (4); 123 private ArrayList _attributes = new ArrayList (4); 124 private ArrayList _operations = new ArrayList (4); 125 private ArrayList _notifications = new ArrayList (4); 126 private String _baseObjectName=null; 127 private Map _components = new HashMap (4); 128 129 130 131 147 public static ModelMBean mbeanFor(Object o) 148 { 149 try 150 { 151 Class oClass = o.getClass(); 152 ClassLoader loader =oClass.getClassLoader(); 153 154 ModelMBean mbean = null; 155 boolean jmx=false; 156 Class [] interfaces=null; 157 int i=0; 158 159 while (mbean==null && oClass!=null) 160 { 161 Class focus=interfaces==null?oClass:interfaces[i]; 162 String pName = focus.getPackage().getName(); 163 String cName = focus.getName().substring(pName.length()+1); 164 String mName=pName+(jmx?".jmx.":".")+cName+"MBean"; 165 166 try{ 167 Class mClass=loader.loadClass(mName); 168 if(log.isTraceEnabled())log.trace("mbeanFor "+o+" mClass="+mClass); 169 mbean=(ModelMBean )mClass.newInstance(); 170 mbean.setManagedResource(o,"objectReference"); 171 if(log.isDebugEnabled())log.debug("mbeanFor "+o+" is "+mbean); 172 return mbean; 173 } 174 catch(ClassNotFoundException e) 175 { 176 if (e.toString().endsWith("MBean")) 177 { if(log.isTraceEnabled())log.trace(e.toString());} 178 else 179 log.warn(LogSupport.EXCEPTION,e); 180 } 181 catch(Error e) 182 { 183 log.warn(LogSupport.EXCEPTION,e); 184 mbean=null; 185 } 186 catch(Exception e) 187 { 188 log.warn(LogSupport.EXCEPTION,e); 189 mbean=null; 190 } 191 192 if (jmx) 193 { 194 if (interfaces!=null) 195 { 196 i++; 197 if (i>=interfaces.length) 198 { 199 interfaces=null; 200 oClass=oClass.getSuperclass(); 201 } 202 } 203 else 204 { 205 interfaces=oClass.getInterfaces(); 206 i=0; 207 if (interfaces==null || interfaces.length==0) 208 { 209 interfaces=null; 210 oClass=oClass.getSuperclass(); 211 } 212 } 213 } 214 jmx=!jmx; 215 } 216 } 217 catch(Exception e) 218 { 219 LogSupport.ignore(log,e); 220 } 221 return null; 222 } 223 224 225 229 public ModelMBeanImpl() 230 {} 231 232 233 237 public ModelMBeanImpl(Object proxyObject) 238 { 239 try 240 { 241 setManagedResource(proxyObject,"objectReference"); 242 } 243 catch(Exception e) 244 { 245 log.warn(LogSupport.EXCEPTION,e); 246 throw new IllegalArgumentException (e.toString()); 247 } 248 } 249 250 251 252 public static String getDefaultDomain() { return __defaultDomain; } 253 254 255 public static void setDefaultDomain(String d) { __defaultDomain=d; } 256 257 258 public MBeanServer getMBeanServer() { return _mBeanServer; } 259 260 261 public ObjectName getObjectName() { return _objectName; } 262 263 264 public Object getManagedResource() { return _object; } 265 266 267 public void setManagedResource(Object proxyObject, String type) 268 throws MBeanException , 269 RuntimeOperationsException , 270 InstanceNotFoundException , 271 InvalidTargetObjectTypeException 272 { 273 if (proxyObject==null) 274 { 275 proxyObject=null; 276 return; 277 } 278 279 log.debug("setManagedResource"); 280 if (!"objectreference".equalsIgnoreCase(type)) 281 throw new InvalidTargetObjectTypeException (type); 282 283 if (_object==null) 284 { 285 _object=proxyObject; 287 288 defineManagedResource(); 289 } 290 else 291 _object=proxyObject; 292 } 293 294 295 301 protected void defineManagedResource() 302 {} 303 304 305 308 public void setModelMBeanInfo(ModelMBeanInfo info) 309 throws MBeanException , 310 RuntimeOperationsException 311 { 312 throw new Error ("setModelMBeanInfo not supported"); 313 } 314 315 316 323 public synchronized void defineAttribute(String name) 324 { 325 defineAttribute(name,true,false); 326 } 327 328 329 337 public synchronized void defineAttribute(String name, boolean writable) 338 { 339 defineAttribute(name,writable,false); 340 } 341 342 343 352 public synchronized void defineAttribute(String name, 353 boolean writable, 354 boolean onMBean) 355 { 356 _dirty=true; 357 358 String uName=name.substring(0,1).toUpperCase()+name.substring(1); 359 name=java.beans.Introspector.decapitalize(name); 360 Class oClass=onMBean?this.getClass():_object.getClass(); 361 362 Class type=null; 363 Method getter=null; 364 Method setter=null; 365 Method [] methods=oClass.getMethods(); 366 for (int m=0;m<methods.length;m++) 367 { 368 if ((methods[m].getModifiers()&Modifier.PUBLIC)==0) 369 continue; 370 371 if (methods[m].getName().equals("get"+uName) && 373 methods[m].getParameterTypes().length==0) 374 { 375 if (getter!=null) 376 throw new IllegalArgumentException ("Multiple getters for attr "+name); 377 getter=methods[m]; 378 if (type!=null && 379 !type.equals(methods[m].getReturnType())) 380 throw new IllegalArgumentException ("Type conflict for attr "+name); 381 type=methods[m].getReturnType(); 382 } 383 384 if (methods[m].getName().equals("is"+uName) && 386 methods[m].getParameterTypes().length==0) 387 { 388 if (getter!=null) 389 throw new IllegalArgumentException ("Multiple getters for attr "+name); 390 getter=methods[m]; 391 if (type!=null && 392 !type.equals(methods[m].getReturnType())) 393 throw new IllegalArgumentException ("Type conflict for attr "+name); 394 type=methods[m].getReturnType(); 395 } 396 397 if (writable && 399 methods[m].getName().equals("set"+uName) && 400 methods[m].getParameterTypes().length==1) 401 { 402 if (setter!=null) 403 throw new IllegalArgumentException ("Multiple setters for attr "+name); 404 setter=methods[m]; 405 if (type!=null && 406 !type.equals(methods[m].getParameterTypes()[0])) 407 throw new IllegalArgumentException ("Type conflict for attr "+name); 408 type=methods[m].getParameterTypes()[0]; 409 } 410 } 411 412 if (getter==null && setter==null) 413 throw new IllegalArgumentException ("No getter or setters found for "+name); 414 415 try 416 { 417 _getter.put(name,getter); 419 _setter.put(name,setter); 420 _attributes.add(new ModelMBeanAttributeInfo (name, 422 findDescription(name), 423 getter, 424 setter)); 425 } 426 catch(Exception e) 427 { 428 log.warn(LogSupport.EXCEPTION,e); 429 throw new IllegalArgumentException (e.toString()); 430 } 431 } 432 433 434 439 public synchronized void defineAttribute(ModelMBeanAttributeInfo attrInfo) 440 { 441 if (_object==null) 442 throw new IllegalStateException ("No Object"); 443 444 _dirty=true; 445 446 String name=attrInfo.getName(); 447 String uName=name.substring(0,1).toUpperCase()+name.substring(1); 448 Class oClass=_object.getClass(); 449 450 try 451 { 452 Class type=TypeUtil.fromName(attrInfo.getType()); 453 if (type==null) 454 type=Thread.currentThread().getContextClassLoader().loadClass(attrInfo.getType()); 455 456 Method getter=null; 457 Method setter=null; 458 459 if (attrInfo.isReadable()) 460 getter=oClass.getMethod((attrInfo.isIs()?"is":"get")+uName,(java.lang.Class [])null); 461 462 if (attrInfo.isWritable()) 463 setter=oClass.getMethod("set"+uName,new Class [] {type}); 464 465 _getter.put(name,getter); 466 _setter.put(name,setter); 467 _attributes.add(attrInfo); 468 } 469 catch(Exception e) 470 { 471 log.warn(LogSupport.EXCEPTION,e); 472 throw new IllegalArgumentException (e.toString()); 473 } 474 } 475 476 477 484 public synchronized void defineOperation(String name,int impact) 485 { 486 defineOperation(name,null,impact,false); 487 } 488 489 490 503 public synchronized void defineOperation(String name, 504 String [] signature, 505 int impact) 506 { 507 defineOperation(name,signature,impact,false); 508 } 509 510 511 525 public synchronized void defineOperation(String name, 526 String [] signature, 527 int impact, 528 boolean onMBean) 529 { 530 _dirty=true; 531 Class oClass=onMBean?this.getClass():_object.getClass(); 532 if (signature==null) signature=new String [0]; 533 534 try 535 { 536 Class [] types = new Class [signature.length]; 537 MBeanParameterInfo [] pInfo = new 538 MBeanParameterInfo [signature.length]; 539 540 String methodKey=name+"("; 542 for (int i=0;i<signature.length;i++) 543 { 544 Class type=TypeUtil.fromName(signature[i]); 545 if (type==null) 546 type=Thread.currentThread().getContextClassLoader().loadClass(signature[i]); 547 types[i]=type; 548 signature[i]=type.isPrimitive()?TypeUtil.toName(type):signature[i]; 549 methodKey+=(i>0?",":"")+signature[i]; 550 } 551 methodKey+=")"; 552 553 for (int i=0;i<signature.length;i++) 555 { 556 String description=findDescription(methodKey+"["+i+"]"); 557 int colon=description.indexOf(":"); 558 if (colon<0) 559 { 560 description="param"+i+":"+description; 561 colon=description.indexOf(":"); 562 } 563 pInfo[i]=new 564 MBeanParameterInfo (description.substring(0,colon).trim(), 565 signature[i], 566 description.substring(colon+1).trim()); 567 } 568 569 Method method=oClass.getMethod(name,types); 571 Class returnClass=method.getReturnType(); 572 _method.put(methodKey,method); 573 _operations.add(new ModelMBeanOperationInfo 574 (name, 575 findDescription(methodKey), 576 pInfo, 577 returnClass.isPrimitive()?TypeUtil.toName(returnClass):(returnClass.getName()), 578 impact)); 579 } 580 catch(Exception e) 581 { 582 log.warn("operation "+name,e); 583 throw new IllegalArgumentException (e.toString()); 584 } 585 586 } 587 588 589 594 public synchronized void defineOperation(ModelMBeanOperationInfo opInfo) 595 { 596 _dirty=true; 597 Class oClass=_object.getClass(); 598 599 try 600 { 601 MBeanParameterInfo [] pInfo = opInfo.getSignature(); 602 603 Class [] types = new Class [pInfo.length]; 604 String method=opInfo.getName()+"("; 605 for (int i=0;i<pInfo.length;i++) 606 { 607 Class type=TypeUtil.fromName(pInfo[i].getType()); 608 if (type==null) 609 type=Thread.currentThread().getContextClassLoader().loadClass(pInfo[i].getType()); 610 types[i]=type; 611 method+=(i>0?",":"")+pInfo[i].getType(); 612 } 613 method+=")"; 614 615 _method.put(method,oClass.getMethod(opInfo.getName(),types)); 616 _operations.add(opInfo); 617 } 618 catch(Exception e) 619 { 620 log.warn(LogSupport.EXCEPTION,e); 621 throw new IllegalArgumentException (e.toString()); 622 } 623 } 624 625 626 public synchronized MBeanInfo getMBeanInfo() 627 { 628 log.debug("getMBeanInfo"); 629 630 if (_dirty) 631 { 632 _dirty=false; 633 ModelMBeanAttributeInfo [] attributes = (ModelMBeanAttributeInfo []) 634 _attributes.toArray(new ModelMBeanAttributeInfo [_attributes.size()]); 635 ModelMBeanOperationInfo [] operations = (ModelMBeanOperationInfo []) 636 _operations.toArray(new ModelMBeanOperationInfo [_operations.size()]); 637 ModelMBeanNotificationInfo [] notifications =(ModelMBeanNotificationInfo []) 638 _notifications.toArray(new ModelMBeanNotificationInfo [_notifications.size()]); 639 640 _beanInfo = 641 new ModelMBeanInfoSupport (_object.getClass().getName(), 642 findDescription(null), 643 attributes, 644 null, 645 operations, 646 notifications); 647 } 648 649 return _beanInfo; 650 } 651 652 653 public Object getAttribute(String name) 654 throws AttributeNotFoundException , 655 MBeanException , 656 ReflectionException 657 { 658 if(log.isDebugEnabled())log.debug("getAttribute "+name); 659 Method getter = (Method )_getter.get(name); 660 if (getter==null) 661 throw new AttributeNotFoundException (name); 662 try 663 { 664 Object o=_object; 665 if (getter.getDeclaringClass().isInstance(this)) 666 o=this; 667 return getter.invoke(o,(java.lang.Object [])null); 668 } 669 catch(IllegalAccessException e) 670 { 671 log.warn(LogSupport.EXCEPTION,e); 672 throw new AttributeNotFoundException (e.toString()); 673 } 674 catch(InvocationTargetException e) 675 { 676 log.warn(LogSupport.EXCEPTION,e); 677 throw new ReflectionException ((Exception )e.getTargetException()); 678 } 679 } 680 681 682 public AttributeList getAttributes(String [] names) 683 { 684 log.debug("getAttributes"); 685 AttributeList results=new AttributeList (names.length); 686 for (int i=0;i<names.length;i++) 687 { 688 try 689 { 690 results.add(new Attribute (names[i], 691 getAttribute(names[i]))); 692 } 693 catch(Exception e) 694 { 695 log.warn(LogSupport.EXCEPTION,e); 696 } 697 } 698 return results; 699 } 700 701 702 703 public void setAttribute(Attribute attr) 704 throws AttributeNotFoundException , 705 InvalidAttributeValueException , 706 MBeanException , 707 ReflectionException 708 { 709 if (attr==null) 710 return; 711 712 if(log.isDebugEnabled())log.debug("setAttribute "+attr.getName()+"="+attr.getValue()); 713 Method setter = (Method )_setter.get(attr.getName()); 714 if (setter==null) 715 throw new AttributeNotFoundException (attr.getName()); 716 try 717 { 718 Object o=_object; 719 if (setter.getDeclaringClass().isInstance(this)) 720 o=this; 721 setter.invoke(o,new Object []{attr.getValue()}); 722 } 723 catch(IllegalAccessException e) 724 { 725 log.warn(LogSupport.EXCEPTION,e); 726 throw new AttributeNotFoundException (e.toString()); 727 } 728 catch(InvocationTargetException e) 729 { 730 log.warn(LogSupport.EXCEPTION,e); 731 throw new ReflectionException ((Exception )e.getTargetException()); 732 } 733 } 734 735 736 public AttributeList setAttributes(AttributeList attrs) 737 { 738 log.debug("setAttributes"); 739 740 AttributeList results=new AttributeList (attrs.size()); 741 Iterator iter = attrs.iterator(); 742 while(iter.hasNext()) 743 { 744 try 745 { 746 Attribute attr=(Attribute )iter.next(); 747 setAttribute(attr); 748 results.add(new Attribute (attr.getName(), 749 getAttribute(attr.getName()))); 750 } 751 catch(Exception e) 752 { 753 log.warn(LogSupport.EXCEPTION,e); 754 } 755 } 756 return results; 757 } 758 759 760 public Object invoke(String name, Object [] params, String [] signature) 761 throws MBeanException , 762 ReflectionException 763 { 764 if(log.isDebugEnabled())log.debug("invoke "+name); 765 766 String methodKey=name+"("; 767 if (signature!=null) 768 for (int i=0;i<signature.length;i++) 769 methodKey+=(i>0?",":"")+signature[i]; 770 methodKey+=")"; 771 772 try 773 { 774 Method method = (Method )_method.get(methodKey); 775 if (method==null) 776 throw new NoSuchMethodException (methodKey); 777 778 Object o=_object; 779 if (method.getDeclaringClass().isInstance(this)) 780 o=this; 781 return method.invoke(o,params); 782 } 783 catch(NoSuchMethodException e) 784 { 785 log.warn(LogSupport.EXCEPTION,e); 786 throw new ReflectionException (e); 787 } 788 catch(IllegalAccessException e) 789 { 790 log.warn(LogSupport.EXCEPTION,e); 791 throw new MBeanException (e); 792 } 793 catch(InvocationTargetException e) 794 { 795 log.warn(LogSupport.EXCEPTION,e); 796 throw new ReflectionException ((Exception )e.getTargetException()); 797 } 798 799 } 800 801 802 public void load() 803 throws MBeanException , 804 RuntimeOperationsException , 805 InstanceNotFoundException 806 { 807 log.debug("load"); 808 } 809 810 811 public void store() 812 throws MBeanException , 813 RuntimeOperationsException , 814 InstanceNotFoundException 815 { 816 log.debug("store"); 817 } 818 819 820 public void addNotificationListener(NotificationListener listener, 821 NotificationFilter filter, 822 Object handback) 823 throws IllegalArgumentException 824 { 825 log.debug("addNotificationListener"); 826 } 827 828 829 public MBeanNotificationInfo [] getNotificationInfo() 830 { 831 log.debug("getNotificationInfo"); 832 return null; 833 } 834 835 836 public void removeNotificationListener(NotificationListener listener) 837 throws ListenerNotFoundException 838 { 839 log.debug("removeNotificationListener"); 840 } 841 842 843 public void addAttributeChangeNotificationListener(NotificationListener listener, 844 String name, 845 Object handback) 846 throws MBeanException , 847 RuntimeOperationsException , 848 IllegalArgumentException 849 { 850 log.debug("addAttributeChangeNotificationListener"); 851 } 852 853 854 public void removeAttributeChangeNotificationListener(NotificationListener listener, 855 String name) 856 throws MBeanException , 857 RuntimeOperationsException , 858 ListenerNotFoundException 859 { 860 log.debug("removeAttributeChangeNotificationListener"); 861 } 862 863 864 public void sendAttributeChangeNotification(Attribute oldAttr, 865 Attribute newAttr) 866 throws MBeanException , 867 RuntimeOperationsException 868 { 869 log.debug("sendAttributeChangeNotification"); 870 } 871 872 873 public void sendAttributeChangeNotification(AttributeChangeNotification notify) 874 throws MBeanException , 875 RuntimeOperationsException 876 { 877 log.debug("sendAttributeChangeNotification"); 878 } 879 880 881 public void sendNotification(String notify) 882 throws MBeanException , 883 RuntimeOperationsException 884 { 885 log.debug("sendNotification"); 886 } 887 888 889 public void sendNotification(Notification notify) 890 throws MBeanException , 891 RuntimeOperationsException 892 { 893 log.debug("sendNotification"); 894 } 895 896 897 925 private String findDescription(String key) 926 { 927 Class lookIn = this.getClass(); 928 929 String [] objectNames=new String [3]; 931 objectNames[0]=_object.getClass().getName(); 932 if (objectNames[0].indexOf(".")>=0) 933 objectNames[1]=objectNames[0].substring(objectNames[0].lastIndexOf(".")+1); 934 935 while(lookIn!=null) 936 { 937 String pkg=lookIn.getName(); 938 int lastDot= pkg.lastIndexOf("."); 939 if (lastDot>0) 940 { 941 objectNames[2]=pkg.substring(lastDot+1); 942 pkg=pkg.substring(0,lastDot); 943 } 944 else 945 { 946 objectNames[2]=pkg; 947 pkg=null; 948 } 949 950 String resource=(pkg==null?"mbean":(pkg.replace('.','/')+"/mbean")); 951 if(log.isTraceEnabled())log.trace("Look for: "+resource); 952 953 try 954 { 955 ResourceBundle bundle= 956 ResourceBundle.getBundle(resource, 957 Locale.getDefault(), 958 _object.getClass().getClassLoader()); 959 960 if(log.isTraceEnabled())log.trace("Bundle "+resource); 961 962 for (int i=0;i<objectNames.length;i++) 963 { 964 String name=objectNames[i]; 965 966 if (name==null) 967 continue; 968 if (name.endsWith("MBean")) 969 name=name.substring(0,name.length()-5); 970 if (key!=null && key.length()>0) 971 name+="."+key; 972 973 try{ 974 String description=bundle.getString(name); 975 if (description!=null && description.length()>0) 976 return description; 977 } 978 catch(Exception e) { if(log.isTraceEnabled())log.trace(e.toString()); } 979 } 980 } 981 catch(Exception e) { if(log.isTraceEnabled())log.trace(e.toString()); } 982 983 lookIn=lookIn.getSuperclass(); 984 } 985 986 if (key==null || key.length()==0) 987 return objectNames[0]; 988 989 return key; 990 } 991 992 993 999 protected ObjectName newObjectName(MBeanServer server) 1000 { 1001 if (_baseObjectName!=null) 1004 { 1005 if (_baseObjectName.indexOf(':')>=0) 1006 return uniqueObjectName(server,_baseObjectName); 1007 return uniqueObjectName(server,getDefaultDomain()+":"+ 1008 _baseObjectName); 1009 } 1010 return uniqueObjectName(server,getDefaultDomain()+":"); 1011 } 1012 1013 1014 public void setBaseObjectName(String s) 1015 { 1016 _baseObjectName=s; 1017 } 1018 1019 1020 public String getBaseObjectName() 1021 { 1022 return _baseObjectName; 1023 } 1024 1025 1026 1034 public synchronized ObjectName preRegister(MBeanServer server, ObjectName oName) 1035 { 1036 _mBeanServer=server; 1037 _objectName=oName; 1038 if (_objectName==null) 1039 { 1040 try{oName=newObjectName(server);} 1041 catch(Exception e){log.warn(LogSupport.EXCEPTION,e);} 1042 } 1043 if(log.isDebugEnabled())log.debug("preRegister "+_objectName+" -> "+oName); 1044 _objectName=oName; 1045 1046 return _objectName; 1047 } 1048 1049 1050 1051 public void postRegister(Boolean ok) 1052 { 1053 if (ok.booleanValue()) 1054 log.info("Registered "+_objectName); 1055 else 1056 { 1057 _mBeanServer=null; 1058 _objectName=null; 1059 } 1060 } 1061 1062 1063 public void preDeregister() 1064 { 1065 log.info("Deregister "+_objectName); 1066 getComponentMBeans(null,_components); 1067 _components.clear(); 1068 } 1069 1070 1071 1074 public void postDeregister() 1075 { 1076 _beanInfo=null; 1077 _mBeanServer=null; 1078 _object=null; 1079 _objectName=null; 1080 if (_getter!=null) 1081 _getter.clear(); 1082 _getter=null; 1083 if (_setter!=null) 1084 _setter.clear(); 1085 _setter=null; 1086 if (_method!=null) 1087 _method.clear(); 1088 _method=null; 1089 if (_attributes!=null) 1090 _attributes.clear(); 1091 _attributes=null; 1092 if (_operations!=null) 1093 _operations.clear(); 1094 _operations=null; 1095 if (_notifications!=null) 1096 _notifications.clear(); 1097 _notifications=null; 1098 } 1099 1100 1101 1109 public synchronized ObjectName uniqueObjectName(MBeanServer server, 1110 String objectName) 1111 { 1112 return uniqueObjectName(server,_object,objectName); 1113 } 1114 1115 1116 public synchronized ObjectName uniqueObjectName(MBeanServer server, 1117 Object object, 1118 String objectName) 1119 { 1120 if (!objectName.endsWith("=")) 1121 { 1122 String className = object.getClass().getName(); 1123 if (className.indexOf(".")>0) 1124 className=className.substring(className.lastIndexOf(".")+1); 1125 if (className.endsWith("MBean")) 1126 className=className.substring(0,className.length()-5); 1127 if (!objectName.endsWith(":")) 1128 objectName+=","; 1129 objectName+=className+"="; 1130 } 1131 1132 ObjectName oName=null; 1133 try 1134 { 1135 while(true) 1136 { 1137 Integer id=(Integer )__objectId.get(objectName); 1138 if (id==null) 1139 id=new Integer (0); 1140 oName=new ObjectName (objectName+id); 1141 id=new Integer (id.intValue()+1); 1142 __objectId.put(objectName,id); 1143 1144 if (server==null) 1146 break; 1147 1148 if (!server.isRegistered(oName)) 1151 break; 1152 } 1153 } 1154 catch(Exception e) 1155 { 1156 log.warn(LogSupport.EXCEPTION,e); 1157 } 1158 1159 return oName; 1160 } 1161 1162 1163 1164 1173 protected ObjectName [] getComponentMBeans(Object [] components, Map map) 1174 { 1175 if (map==null) 1176 map=_components; 1177 ObjectName [] beans=null; 1178 if (components==null) 1179 beans = new ObjectName [0]; 1180 else 1181 { 1182 beans = new ObjectName [components==null?0:components.length]; 1183 1184 for (int i=0;i<components.length;i++) 1186 { 1187 ObjectName on = (ObjectName )map.get(components[i]); 1188 if (on==null) 1189 { 1190 ModelMBean mbean = mbeanFor(components[i]); 1191 if (mbean==null) 1192 log.warn("No mbean for "+components[i]); 1193 else 1194 { 1195 try 1196 { 1197 if (mbean instanceof ModelMBeanImpl) 1198 { 1199 ((ModelMBeanImpl)mbean).setBaseObjectName(getObjectName().toString()); 1200 on=getMBeanServer().registerMBean(mbean,null).getObjectName(); 1201 } 1202 else 1203 { 1204 on=uniqueObjectName(getMBeanServer(), 1205 components[i], 1206 getObjectName().toString()); 1207 on=getMBeanServer().registerMBean(mbean,on).getObjectName(); 1208 } 1209 map.put(components[i],on); 1210 } 1211 catch (Exception e) 1212 { 1213 log.warn(LogSupport.EXCEPTION,e); 1214 } 1215 } 1216 } 1217 beans[i]=on; 1218 } 1219 } 1220 1221 if (components==null || map.size()>components.length) 1223 { 1224 Object [] to_delete=new Object [map.size()-beans.length]; 1225 int d=0; 1226 Iterator iter = map.keySet().iterator(); 1227 keys: 1228 while(iter.hasNext()) 1229 { 1230 Object bean = iter.next(); 1231 if (components!=null) 1232 { 1233 for(int i=0;i<components.length;i++) 1234 if (components[i]==bean) 1235 continue keys; 1236 } 1237 to_delete[d++]=bean; 1238 } 1239 1240 for (;d-->0;) 1241 { 1242 try{getMBeanServer().unregisterMBean((ObjectName )map.remove(to_delete[d]));} 1243 catch (Exception e) {log.warn(LogSupport.EXCEPTION,e);} 1244 } 1245 } 1246 1247 return beans; 1248 } 1249 1250 1253 protected void destroyComponentMBeans (Map map) 1254 { 1255 if (null==map) 1258 map = _components; 1259 1260 if (map==null) 1261 return; 1262 1263 Iterator itor = map.values().iterator(); 1264 while (itor.hasNext()) 1265 { 1266 try 1267 { 1268 ObjectName o = (ObjectName )itor.next(); 1269 getMBeanServer().unregisterMBean(o); 1270 itor.remove(); 1271 } 1272 catch (Exception e) {log.warn(LogSupport.EXCEPTION,e);} 1273 } 1274 1275 } 1276} 1277 | Popular Tags |