1 28 29 package com.caucho.jmx; 30 31 import com.caucho.loader.Environment; 32 import com.caucho.loader.WeakCloseListener; 33 import com.caucho.log.Log; 34 import com.caucho.util.L10N; 35 36 import javax.management.*; 37 import javax.management.loading.ClassLoaderRepository ; 38 import java.io.ObjectInputStream ; 39 import java.lang.reflect.Constructor ; 40 import java.lang.reflect.InvocationTargetException ; 41 import java.lang.reflect.Modifier ; 42 import java.util.Hashtable ; 43 import java.util.Iterator ; 44 import java.util.Set ; 45 import java.util.logging.Level ; 46 import java.util.logging.Logger ; 47 48 51 abstract public class AbstractMBeanServer implements MBeanServer { 52 private static final L10N L = new L10N(AbstractMBeanServer.class); 53 private static final Logger log = Log.open(AbstractMBeanServer.class); 54 55 static ObjectName SERVER_DELEGATE_NAME; 56 57 private String _defaultDomain; 59 60 63 public AbstractMBeanServer(String defaultDomain) 64 { 65 _defaultDomain = defaultDomain; 66 67 Environment.addClassLoaderListener(new WeakCloseListener(this)); 68 } 69 70 73 protected MBeanContext getContext() 74 { 75 return getContext(Thread.currentThread().getContextClassLoader()); 76 } 77 78 81 protected MBeanContext getExistingContext() 82 { 83 return getExistingContext(Thread.currentThread().getContextClassLoader()); 84 } 85 86 89 protected MBeanContext getGlobalContext() 90 { 91 return getContext(ClassLoader.getSystemClassLoader()); 92 } 93 94 97 abstract protected MBeanContext getContext(ClassLoader loader); 98 99 102 abstract protected MBeanContext getExistingContext(ClassLoader loader); 103 104 107 protected void removeContext(MBeanContext context, ClassLoader loader) 108 { 109 } 110 111 114 protected MBeanView getView() 115 { 116 return getContext().getView(); 117 } 118 119 122 protected MBeanView getGlobalView() 123 { 124 return getGlobalContext().getView(); 125 } 126 127 130 protected MBeanView getParentView() 131 { 132 return null; 133 } 134 135 142 public Object instantiate(String className) 143 throws ReflectionException, MBeanException 144 { 145 try { 146 Class cl = getClassLoaderRepository().loadClass(className); 147 148 return cl.newInstance(); 149 } catch (ClassNotFoundException e) { 150 throw new ReflectionException(e); 151 } catch (InstantiationException e) { 152 throw new ReflectionException(e); 153 } catch (ExceptionInInitializerError e) { 154 Throwable cause = e.getCause(); 155 if (cause instanceof Exception ) 156 throw new MBeanException((Exception ) cause); 157 else 158 throw e; 159 } catch (IllegalAccessException e) { 160 throw new ReflectionException(e); 161 } 162 } 163 164 172 public Object instantiate(String className, ObjectName loaderName) 173 throws ReflectionException, MBeanException, InstanceNotFoundException 174 { 175 MBeanWrapper mbean = getMBean(loaderName); 176 177 if (mbean == null) 178 throw new InstanceNotFoundException(String.valueOf(loaderName)); 179 else if (! (mbean.getObject() instanceof ClassLoader )) 180 throw new InstanceNotFoundException(L.l("{0} is not a class loader", 181 loaderName)); 182 183 try { 184 ClassLoader loader = (ClassLoader ) mbean.getObject(); 185 186 Class cl = loader.loadClass(className); 187 188 return cl.newInstance(); 189 } catch (ClassNotFoundException e) { 190 throw new ReflectionException(e); 191 } catch (InstantiationException e) { 192 throw new ReflectionException(e); 193 } catch (IllegalAccessException e) { 194 throw new ReflectionException(e); 195 } 196 } 197 198 208 public Object instantiate(String className, 209 Object []params, String []signature) 210 throws ReflectionException, MBeanException 211 { 212 try { 213 Class cl = getClassLoaderRepository().loadClass(className); 214 215 Constructor constructor = getConstructor(cl, signature); 216 217 return constructor.newInstance(params); 218 } catch (ClassNotFoundException e) { 219 throw new ReflectionException(e); 220 } catch (InstantiationException e) { 221 throw new ReflectionException(e); 222 } catch (InvocationTargetException e) { 223 throw new MBeanException(e); 224 } catch (IllegalAccessException e) { 225 throw new ReflectionException(e); 226 } 227 } 228 229 240 public Object instantiate(String className, ObjectName loaderName, 241 Object []params, String []signature) 242 throws ReflectionException, MBeanException, InstanceNotFoundException 243 { 244 MBeanWrapper mbean = getMBean(loaderName); 245 246 if (mbean == null) 247 throw new InstanceNotFoundException(String.valueOf(loaderName)); 248 else if (! (mbean.getObject() instanceof ClassLoader )) 249 throw new InstanceNotFoundException(L.l("{0} is not a class loader", 250 loaderName)); 251 252 try { 253 ClassLoader loader = (ClassLoader ) mbean.getObject(); 254 255 Class cl = loader.loadClass(className); 256 257 Constructor constructor = getConstructor(cl, signature); 258 259 return constructor.newInstance(params); 260 } catch (ClassNotFoundException e) { 261 throw new ReflectionException(e); 262 } catch (InstantiationException e) { 263 throw new ReflectionException(e); 264 } catch (InvocationTargetException e) { 265 throw new MBeanException(e); 266 } catch (IllegalAccessException e) { 267 throw new ReflectionException(e); 268 } 269 } 270 271 274 private Constructor getConstructor(Class cl, String []sig) 275 { 276 Constructor []constructors = cl.getConstructors(); 277 278 for (int i = 0; i < constructors.length; i++) { 279 if (! Modifier.isPublic(constructors[i].getModifiers())) 280 continue; 281 282 if (isMatch(constructors[i].getParameterTypes(), sig)) 283 return constructors[i]; 284 } 285 286 return null; 287 } 288 289 292 private boolean isMatch(Class []param, String []sig) 293 { 294 if (param.length != sig.length) 295 return false; 296 297 for (int i = 0; i < param.length; i++) { 298 if (! param[i].getName().equals(sig[i])) 299 return false; 300 } 301 302 return true; 303 } 304 305 313 public ObjectInstance createMBean(String className, ObjectName name) 314 throws ReflectionException, InstanceAlreadyExistsException, 315 MBeanException, NotCompliantMBeanException 316 { 317 return registerMBean(instantiate(className), name); 318 } 319 320 321 330 public ObjectInstance createMBean(String className, ObjectName name, 331 ObjectName loaderName) 332 throws ReflectionException, InstanceAlreadyExistsException, 333 MBeanException, NotCompliantMBeanException, InstanceNotFoundException 334 { 335 return registerMBean(instantiate(className, loaderName), name); 336 } 337 338 348 public ObjectInstance createMBean(String className, ObjectName name, 349 Object []params, String []signature) 350 throws ReflectionException, InstanceAlreadyExistsException, 351 MBeanException, NotCompliantMBeanException 352 { 353 return registerMBean(instantiate(className, params, signature), 354 name); 355 } 356 357 368 public ObjectInstance createMBean(String className, ObjectName name, 369 ObjectName loaderName, 370 Object []params, String []signature) 371 throws ReflectionException, InstanceAlreadyExistsException, 372 MBeanException, NotCompliantMBeanException, InstanceNotFoundException 373 { 374 return registerMBean(instantiate(className, loaderName, params, signature), 375 name); 376 } 377 378 386 public ObjectInstance registerMBean(Object object, ObjectName name) 387 throws InstanceAlreadyExistsException, 388 MBeanRegistrationException, 389 NotCompliantMBeanException 390 { 391 if (object == null) 392 throw new NullPointerException (); 393 394 MBeanContext context = getContext(); 395 396 if (context.getMBean(name) != null) { 397 throw new InstanceAlreadyExistsException(String.valueOf(name)); 398 } 399 400 DynamicMBean dynMBean = createMBean(object, name); 401 402 if (object instanceof IntrospectionMBean) 403 object = ((IntrospectionMBean) object).getImplementation(); 404 else if (object instanceof StandardMBean) { 405 object = ((StandardMBean) object).getImplementation(); 406 } 407 408 MBeanWrapper mbean = new MBeanWrapper(context, name, object, dynMBean); 409 410 return context.registerMBean(mbean, name); 411 } 412 413 416 private DynamicMBean createMBean(Object obj, ObjectName name) 417 throws NotCompliantMBeanException 418 { 419 if (obj == null) 420 throw new NotCompliantMBeanException(L.l("{0} mbean is null", name)); 421 else if (obj instanceof DynamicMBean) 422 return (DynamicMBean) obj; 423 424 Class ifc = getMBeanInterface(obj.getClass()); 425 426 if (ifc == null) 427 throw new NotCompliantMBeanException(L.l("{0} mbean has no MBean interface", name)); 428 429 return new IntrospectionMBean(obj, ifc); 430 } 431 432 435 private Class getMBeanInterface(Class cl) 436 { 437 for (; cl != null; cl = cl.getSuperclass()) { 438 Class []interfaces = cl.getInterfaces(); 439 440 String mbeanName = cl.getName() + "MBean"; 441 String mxbeanName = cl.getName() + "MXBean"; 442 443 int p = mbeanName.lastIndexOf('.'); 444 mbeanName = mbeanName.substring(p); 445 446 p = mxbeanName.lastIndexOf('.'); 447 mxbeanName = mxbeanName.substring(p); 448 449 for (int i = 0; i < interfaces.length; i++) { 450 Class ifc = interfaces[i]; 451 452 if (ifc.getName().endsWith(mbeanName) 453 || ifc.getName().endsWith(mxbeanName)) 454 return ifc; 455 } 456 } 457 458 return null; 459 } 460 461 466 public void unregisterMBean(ObjectName name) 467 throws InstanceNotFoundException, 468 MBeanRegistrationException 469 { 470 MBeanContext context = getExistingContext(); 471 472 if (context != null) { 473 context.unregisterMBean(name); 474 475 log.finer("unregistered " + name); 476 } 477 478 } 480 481 488 public ObjectInstance getObjectInstance(ObjectName name) 489 throws InstanceNotFoundException 490 { 491 MBeanWrapper mbean = getMBean(name); 492 493 if (mbean == null) 494 throw new InstanceNotFoundException(String.valueOf(name)); 495 496 return mbean.getObjectInstance(); 497 } 498 499 507 public Set <ObjectInstance> queryMBeans(ObjectName name, QueryExp query) 508 { 509 try { 510 if (query != null) { 511 query.setMBeanServer(this); 512 } 513 514 return getView().queryMBeans(name, query); 515 } catch (Exception e) { 516 log.log(Level.WARNING, e.toString(), e); 517 518 return null; 519 } 520 } 521 522 530 public Set <ObjectName> queryNames(ObjectName name, QueryExp query) 531 { 532 try { 533 if (query != null) { 534 query.setMBeanServer(this); 535 } 536 537 return getView().queryNames(name, query); 538 } catch (Exception e) { 539 log.log(Level.WARNING, e.toString(), e); 540 541 return null; 542 } 543 } 544 545 548 private boolean isMatch(ObjectName testName, 549 ObjectName queryName, 550 QueryExp query) 551 { 552 if (queryName == null) 553 return true; 554 555 if (! queryName.isPattern() && 556 ! testName.getDomain().equals(queryName.getDomain())) 557 return false; 558 559 if (queryName.isPropertyPattern()) { 560 563 Hashtable map = queryName.getKeyPropertyList(); 564 Iterator iter = map.keySet().iterator(); 565 while (iter.hasNext()) { 566 String key = (String ) iter.next(); 567 String value = (String ) map.get(key); 568 569 if (! value.equals(testName.getKeyProperty(key))) 570 return false; 571 } 572 } 573 else { 574 String testProps = testName.getCanonicalKeyPropertyListString(); 575 String queryProps = queryName.getCanonicalKeyPropertyListString(); 576 577 if (! testProps.equals(queryProps)) 578 return false; 579 } 580 581 return true; 582 } 583 584 591 public boolean isRegistered(ObjectName name) 592 { 593 return getView().getMBean(name) != null; 594 } 595 596 601 public Integer getMBeanCount() 602 { 603 return new Integer (getView().getMBeanCount()); 604 } 605 606 614 public Object getAttribute(ObjectName name, String attribute) 615 throws MBeanException, AttributeNotFoundException, 616 InstanceNotFoundException, ReflectionException 617 { 618 MBeanWrapper mbean = getMBean(name); 619 620 if (mbean == null) { 621 throw new InstanceNotFoundException(String.valueOf(name)); 622 } 623 624 return mbean.getAttribute(attribute); 625 } 626 627 635 public AttributeList getAttributes(ObjectName name, String []attributes) 636 throws InstanceNotFoundException, ReflectionException 637 { 638 MBeanWrapper mbean = getMBean(name); 639 640 if (mbean == null) 641 throw new InstanceNotFoundException(String.valueOf(name)); 642 643 return mbean.getAttributes(attributes); 644 } 645 646 652 public void setAttribute(ObjectName name, Attribute attribute) 653 throws InstanceNotFoundException, AttributeNotFoundException, 654 InvalidAttributeValueException, MBeanException, ReflectionException 655 { 656 MBeanWrapper mbean = getMBean(name); 657 658 if (mbean == null) 659 throw new InstanceNotFoundException(String.valueOf(name)); 660 661 mbean.setAttribute(attribute); 662 } 663 664 670 public AttributeList setAttributes(ObjectName name, AttributeList attributes) 671 throws InstanceNotFoundException, ReflectionException 672 { 673 MBeanWrapper mbean = getMBean(name); 674 675 if (mbean == null) 676 throw new InstanceNotFoundException(String.valueOf(name)); 677 678 return mbean.setAttributes(attributes); 679 } 680 681 689 public Object invoke(ObjectName name, 690 String operationName, 691 Object []params, 692 String []signature) 693 throws InstanceNotFoundException, MBeanException, ReflectionException 694 { 695 MBeanWrapper mbean = getMBean(name); 696 697 if (mbean == null) 698 throw new InstanceNotFoundException(String.valueOf(name)); 699 700 return mbean.invoke(operationName, params, signature); 701 } 702 703 706 public String getDefaultDomain() 707 { 708 return _defaultDomain; 709 } 710 711 719 public void addNotificationListener(ObjectName name, 720 NotificationListener listener, 721 NotificationFilter filter, 722 Object handback) 723 throws InstanceNotFoundException 724 { 725 MBeanWrapper mbean = getMBean(name); 726 727 if (mbean == null) 728 throw new InstanceNotFoundException(String.valueOf(name)); 729 730 mbean.addNotificationListener(listener, filter, handback); 731 732 getContext().addNotificationListener(name, listener, filter, handback); 733 } 734 735 743 public void addNotificationListener(ObjectName name, 744 ObjectName listenerName, 745 NotificationFilter filter, 746 Object handback) 747 throws InstanceNotFoundException 748 { 749 MBeanWrapper listenerMBean = getMBean(listenerName); 750 751 if (listenerMBean == null) 752 throw new InstanceNotFoundException(String.valueOf(listenerName)); 753 754 NotificationListener listener = listenerMBean.getListener(); 755 756 if (listener == null) { 757 IllegalArgumentException exn = new IllegalArgumentException (L.l("{0} does not implement NotificationListener.", listenerName)); 758 throw new RuntimeOperationsException(exn); 759 } 760 761 addNotificationListener(name, listener, filter, handback); 762 } 763 764 770 public void removeNotificationListener(ObjectName name, 771 NotificationListener listener) 772 throws InstanceNotFoundException, ListenerNotFoundException 773 { 774 MBeanWrapper mbean = getMBean(name); 775 776 if (mbean == null) 777 throw new InstanceNotFoundException(String.valueOf(name)); 778 779 mbean.removeNotificationListener(listener); 780 781 getContext().removeNotificationListener(name, listener); 782 } 783 784 790 public void removeNotificationListener(ObjectName name, 791 ObjectName listenerName) 792 throws InstanceNotFoundException, ListenerNotFoundException 793 { 794 MBeanWrapper listenerMBean = getMBean(listenerName); 795 796 if (listenerMBean == null) 797 throw new InstanceNotFoundException(String.valueOf(listenerName)); 798 799 NotificationListener listener = listenerMBean.getListener(); 800 801 if (listener == null) { 802 IllegalArgumentException exn = new IllegalArgumentException (L.l("{0} does not implement NotificationListener.")); 803 throw new RuntimeOperationsException(exn); 804 } 805 806 removeNotificationListener(name, listener); 807 } 808 809 819 public void removeNotificationListener(ObjectName name, 820 ObjectName listenerName, 821 NotificationFilter filter, 822 Object handback) 823 throws InstanceNotFoundException, ListenerNotFoundException 824 { 825 MBeanWrapper listenerMBean = getMBean(listenerName); 826 827 if (listenerMBean == null) 828 throw new InstanceNotFoundException(String.valueOf(listenerName)); 829 830 NotificationListener listener = listenerMBean.getListener(); 831 832 if (listener == null) { 833 IllegalArgumentException exn = new IllegalArgumentException (L.l("{0} does not implement NotificationListener.")); 834 throw new RuntimeOperationsException(exn); 835 } 836 837 removeNotificationListener(name, listener, filter, handback); 838 } 839 840 850 public void removeNotificationListener(ObjectName name, 851 NotificationListener listener, 852 NotificationFilter filter, 853 Object handback) 854 throws InstanceNotFoundException, ListenerNotFoundException 855 { 856 MBeanWrapper mbean = getMBean(name); 857 858 if (mbean == null) 859 throw new InstanceNotFoundException(String.valueOf(name)); 860 861 getContext().removeNotificationListener(name, listener, filter, handback); 862 863 mbean.removeNotificationListener(listener, filter, handback); 864 } 865 866 873 public MBeanInfo getMBeanInfo(ObjectName name) 874 throws InstanceNotFoundException, IntrospectionException, 875 ReflectionException 876 { 877 MBeanWrapper mbean = getMBean(name); 878 879 if (mbean == null) 880 throw new InstanceNotFoundException(String.valueOf(name)); 881 882 MBeanInfo info = mbean.getMBeanInfo(); 883 884 return info; 885 } 886 887 895 public boolean isInstanceOf(ObjectName name, String className) 896 throws InstanceNotFoundException 897 { 898 MBeanWrapper mbean = getMBean(name); 899 900 if (mbean == null) 901 throw new InstanceNotFoundException(String.valueOf(name)); 902 903 Object obj = mbean.getObject(); 904 Class cl = obj.getClass(); 905 906 return isInstanceOf(cl, className); 907 } 908 909 private boolean isInstanceOf(Class cl, String className) 910 { 911 if (cl == null) 912 return false; 913 914 if (cl.getName().equals(className)) 915 return true; 916 917 if (isInstanceOf(cl.getSuperclass(), className)) 918 return true; 919 920 Class []ifs = cl.getInterfaces(); 921 for (int i = 0; i < ifs.length; i++) { 922 if (isInstanceOf(ifs[i], className)) 923 return true; 924 } 925 926 return false; 927 } 928 929 938 public ClassLoader getClassLoaderFor(ObjectName name) 939 throws InstanceNotFoundException 940 { 941 MBeanWrapper mbean = getMBean(name); 942 943 if (mbean == null) 944 throw new InstanceNotFoundException(String.valueOf(name)); 945 946 return mbean.getContext().getClassLoader(); 947 } 948 949 958 public ClassLoader getClassLoader(ObjectName loaderName) 959 throws InstanceNotFoundException 960 { 961 return null; 962 } 963 964 969 public ClassLoaderRepository getClassLoaderRepository() 970 { 971 return getContext().getClassLoaderRepository(); 972 } 973 974 982 public ObjectInputStream deserialize(ObjectName name, byte []data) 983 throws InstanceNotFoundException, OperationsException 984 { 985 throw new UnsupportedOperationException (); 986 } 987 988 996 public ObjectInputStream deserialize(String className, byte []data) 997 throws OperationsException, ReflectionException 998 { 999 throw new UnsupportedOperationException (); 1000 } 1001 1002 1011 public ObjectInputStream deserialize(String className, 1012 ObjectName loaderName, 1013 byte []data) 1014 throws OperationsException, ReflectionException, 1015 InstanceNotFoundException 1016 { 1017 throw new UnsupportedOperationException (); 1018 } 1019 1020 1025 public String []getDomains() 1026 { 1027 return getView().getDomains(); 1028 } 1029 1030 1033 MBeanWrapper getMBean(ObjectName name) 1034 { 1035 return getView().getMBean(name); 1036 } 1037 1038 1041 public void destroy() 1042 { 1043 try { 1044 MBeanServerFactory.releaseMBeanServer(this); 1045 } catch (IllegalArgumentException e) { 1046 log.log(Level.FINEST, e.toString(), e); 1047 } catch (Throwable e) { 1048 log.log(Level.FINER, e.toString(), e); 1049 } 1050 } 1051 1052 1055 public String toString() 1056 { 1057 if (_defaultDomain != null) 1058 return "MBeanServerImpl[domain=" + _defaultDomain + "]"; 1059 else 1060 return "MBeanServerImpl[]"; 1061 } 1062 1063 static { 1064 try { 1065 SERVER_DELEGATE_NAME = new ObjectName("JMImplementation:type=MBeanServerDelegate"); 1066 } catch (Throwable e) { 1067 e.printStackTrace(); 1068 } 1069 } 1070} 1071 | Popular Tags |