1 17 18 19 package org.apache.tomcat.util.modeler; 20 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.InputStream ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Hashtable ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import javax.management.DynamicMBean ; 33 import javax.management.MBeanAttributeInfo ; 34 import javax.management.MBeanInfo ; 35 import javax.management.MBeanOperationInfo ; 36 import javax.management.MBeanRegistration ; 37 import javax.management.MBeanServer ; 38 import javax.management.MBeanServerFactory ; 39 import javax.management.MalformedObjectNameException ; 40 import javax.management.ObjectName ; 41 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 import org.apache.tomcat.util.modeler.modules.ModelerSource; 45 46 55 56 74 public class Registry implements RegistryMBean, MBeanRegistration { 75 78 private static Log log = LogFactory.getLog(Registry.class); 79 80 82 84 private static HashMap perLoaderRegistries=null; 85 86 90 private static Registry registry = null; 91 92 94 98 private MBeanServer server = null; 99 100 104 private HashMap descriptors = new HashMap (); 105 106 108 private HashMap descriptorsByClass = new HashMap (); 109 110 private HashMap searchedPaths=new HashMap (); 112 113 private Object guard; 114 115 private Hashtable idDomains=new Hashtable (); 118 private Hashtable ids=new Hashtable (); 119 120 121 123 125 public Registry() { 126 super(); 127 } 128 129 132 149 public synchronized static Registry getRegistry(Object key, Object guard) { 150 Registry localRegistry; 151 if( perLoaderRegistries!=null ) { 152 if( key==null ) 153 key=Thread.currentThread().getContextClassLoader(); 154 if( key != null ) { 155 localRegistry=(Registry)perLoaderRegistries.get(key); 156 if( localRegistry == null ) { 157 localRegistry=new Registry(); 158 localRegistry.guard=guard; 160 perLoaderRegistries.put( key, localRegistry ); 161 return localRegistry; 162 } 163 if( localRegistry.guard != null && 164 localRegistry.guard != guard ) { 165 return null; } 167 return localRegistry; 168 } 169 } 170 171 if (registry == null) { 173 registry = new Registry(); 174 } 175 if( registry.guard != null && 176 registry.guard != guard ) { 177 return null; 178 } 179 return (registry); 180 } 181 182 190 public static void setUseContextClassLoader( boolean enable ) { 191 if( enable ) { 192 perLoaderRegistries=new HashMap (); 193 } 194 } 195 196 198 203 public void stop() { 204 descriptorsByClass = new HashMap (); 205 descriptors = new HashMap (); 206 searchedPaths=new HashMap (); 207 } 208 209 224 public List loadMBeans( Object source, ClassLoader cl ) 225 throws Exception 226 { 227 return load("MbeansSource", source, null ); 228 } 229 230 231 241 public void loadMetadata(Object source ) throws Exception { 242 loadDescriptors( null, source, null ); 243 } 244 245 274 public void registerComponent(Object bean, String oname, String type) 275 throws Exception 276 { 277 registerComponent(bean, new ObjectName (oname), type); 278 } 279 280 287 public void unregisterComponent( String oname ) { 288 try { 289 unregisterComponent(new ObjectName (oname)); 290 } catch (MalformedObjectNameException e) { 291 log.info("Error creating object name " + e ); 292 } 293 } 294 295 296 305 public void invoke( List mbeans, String operation, boolean failFirst ) 306 throws Exception 307 { 308 if( mbeans==null ) { 309 return; 310 } 311 Iterator itr=mbeans.iterator(); 312 while(itr.hasNext()) { 313 Object current=itr.next(); 314 ObjectName oN=null; 315 try { 316 if( current instanceof ObjectName ) { 317 oN=(ObjectName )current; 318 } 319 if( current instanceof String ) { 320 oN=new ObjectName ( (String )current ); 321 } 322 if( oN==null ) { 323 continue; 324 } 325 if( getMethodInfo(oN, operation) == null) { 326 continue; 327 } 328 getMBeanServer().invoke(oN, operation, 329 new Object [] {}, new String [] {}); 330 331 } catch( Exception t ) { 332 if( failFirst ) throw t; 333 log.info("Error initializing " + current + " " + t.toString()); 334 } 335 } 336 } 337 338 340 348 public synchronized int getId( String domain, String name) { 349 if( domain==null) { 350 domain=""; 351 } 352 Hashtable domainTable=(Hashtable )idDomains.get( domain ); 353 if( domainTable == null ) { 354 domainTable=new Hashtable (); 355 idDomains.put( domain, domainTable); 356 } 357 if( name==null ) { 358 name=""; 359 } 360 Integer i=(Integer )domainTable.get(name); 361 362 if( i!= null ) { 363 return i.intValue(); 364 } 365 366 int id[]=(int [])ids.get( domain ); 367 if( id == null ) { 368 id=new int[1]; 369 ids.put( domain, id); 370 } 371 int code=id[0]++; 372 domainTable.put( name, new Integer ( code )); 373 return code; 374 } 375 376 379 386 public void addManagedBean(ManagedBean bean) { 387 descriptors.put(bean.getName(), bean); 389 if( bean.getType() != null ) { 390 descriptorsByClass.put( bean.getType(), bean ); 391 } 392 } 393 394 395 403 public ManagedBean findManagedBean(String name) { 404 ManagedBean mb=((ManagedBean) descriptors.get(name)); 406 if( mb==null ) 407 mb=(ManagedBean)descriptorsByClass.get(name); 408 return mb; 409 } 410 411 417 public String [] findManagedBeans() { 418 return ((String []) descriptors.keySet().toArray(new String [0])); 419 } 420 421 422 430 public String [] findManagedBeans(String group) { 431 432 ArrayList results = new ArrayList (); 433 Iterator items = descriptors.values().iterator(); 434 while (items.hasNext()) { 435 ManagedBean item = (ManagedBean) items.next(); 436 if ((group == null) && (item.getGroup() == null)) { 437 results.add(item.getName()); 438 } else if (group.equals(item.getGroup())) { 439 results.add(item.getName()); 440 } 441 } 442 String values[] = new String [results.size()]; 443 return ((String []) results.toArray(values)); 444 445 } 446 447 448 454 public void removeManagedBean(ManagedBean bean) { 455 descriptors.remove(bean.getName()); 457 descriptorsByClass.remove( bean.getType()); 458 } 459 460 462 469 public static MBeanServer getServer() { 470 return Registry.getRegistry().getMBeanServer(); 471 } 472 473 481 public static void setServer(MBeanServer mbeanServer) { 482 Registry.getRegistry().setMBeanServer(mbeanServer); 483 } 484 485 496 public static void loadRegistry(InputStream stream) throws Exception { 497 Registry registry = getRegistry(); 498 registry.loadMetadata(stream); 499 } 500 501 507 public synchronized static Registry getRegistry() { 508 return getRegistry(null, null); 509 } 510 511 513 520 public String getType( ObjectName oname, String attName ) 521 { 522 String type=null; 523 MBeanInfo info=null; 524 try { 525 info=server.getMBeanInfo(oname); 526 } catch (Exception e) { 527 log.info( "Can't find metadata for object" + oname ); 528 return null; 529 } 530 531 MBeanAttributeInfo attInfo[]=info.getAttributes(); 532 for( int i=0; i<attInfo.length; i++ ) { 533 if( attName.equals(attInfo[i].getName())) { 534 type=attInfo[i].getType(); 535 return type; 536 } 537 } 538 return null; 539 } 540 541 547 public MBeanOperationInfo getMethodInfo( ObjectName oname, String opName ) 548 { 549 String type=null; 550 MBeanInfo info=null; 551 try { 552 info=server.getMBeanInfo(oname); 553 } catch (Exception e) { 554 log.info( "Can't find metadata " + oname ); 555 return null; 556 } 557 MBeanOperationInfo attInfo[]=info.getOperations(); 558 for( int i=0; i<attInfo.length; i++ ) { 559 if( opName.equals(attInfo[i].getName())) { 560 return attInfo[i]; 561 } 562 } 563 return null; 564 } 565 566 571 public void unregisterComponent( ObjectName oname ) { 572 try { 573 if( getMBeanServer().isRegistered(oname)) { 574 getMBeanServer().unregisterMBean(oname); 575 } 576 } catch( Throwable t ) { 577 log.error( "Error unregistering mbean ", t); 578 } 579 } 580 581 586 public synchronized MBeanServer getMBeanServer() { 587 long t1=System.currentTimeMillis(); 588 589 if (server == null) { 590 if( MBeanServerFactory.findMBeanServer(null).size() > 0 ) { 591 server=(MBeanServer )MBeanServerFactory.findMBeanServer(null).get(0); 592 if( log.isDebugEnabled() ) { 593 log.debug("Using existing MBeanServer " + (System.currentTimeMillis() - t1 )); 594 } 595 } else { 596 server=MBeanServerFactory.createMBeanServer(); 597 if( log.isDebugEnabled() ) { 598 log.debug("Creating MBeanServer"+ (System.currentTimeMillis() - t1 )); 599 } 600 } 601 } 602 return (server); 603 } 604 605 607 public ManagedBean findManagedBean(Object bean, Class beanClass, String type) 608 throws Exception 609 { 610 if( bean!=null && beanClass==null ) { 611 beanClass=bean.getClass(); 612 } 613 614 if( type==null ) { 615 type=beanClass.getName(); 616 } 617 618 ManagedBean managed = findManagedBean(type); 620 621 if( managed==null ) { 623 if( log.isDebugEnabled() ) { 625 log.debug( "Looking for descriptor "); 626 } 627 findDescriptor( beanClass, type ); 628 629 managed=findManagedBean(type); 630 } 631 632 if( bean instanceof DynamicMBean ) { 633 if( log.isDebugEnabled() ) { 634 log.debug( "Dynamic mbean support "); 635 } 636 loadDescriptors("MbeansDescriptorsDynamicMBeanSource", 638 bean, type); 639 640 managed=findManagedBean(type); 641 } 642 643 if( managed==null ) { 645 if( log.isDebugEnabled() ) { 646 log.debug( "Introspecting "); 647 } 648 649 loadDescriptors("MbeansDescriptorsIntrospectionSource", 651 beanClass, type); 652 653 managed=findManagedBean(type); 654 if( managed==null ) { 655 log.warn( "No metadata found for " + type ); 656 return null; 657 } 658 managed.setName( type ); 659 addManagedBean(managed); 660 } 661 return managed; 662 } 663 664 665 673 public Object convertValue(String type, String value) 674 { 675 Object objValue=value; 676 677 if( type==null || "java.lang.String".equals( type )) { 678 objValue=value; 680 } else if( "javax.management.ObjectName".equals( type ) || 681 "ObjectName".equals( type )) { 682 try { 683 objValue=new ObjectName ( value ); 684 } catch (MalformedObjectNameException e) { 685 return null; 686 } 687 } else if( "java.lang.Integer".equals( type ) || 688 "int".equals( type )) { 689 objValue=new Integer ( value ); 690 } else if( "java.lang.Long".equals( type ) || 691 "long".equals( type )) { 692 objValue=new Long ( value ); 693 } else if( "java.lang.Boolean".equals( type ) || 694 "boolean".equals( type )) { 695 objValue=new Boolean ( value ); 696 } 697 return objValue; 698 } 699 700 709 public List load( String sourceType, Object source, String param) 710 throws Exception 711 { 712 if( log.isTraceEnabled()) { 713 log.trace("load " + source ); 714 } 715 String location=null; 716 String type=null; 717 Object inputsource=null; 718 719 if( source instanceof DynamicMBean ) { 720 sourceType="MbeansDescriptorsDynamicMBeanSource"; 721 inputsource=source; 722 } else if( source instanceof URL ) { 723 URL url=(URL )source; 724 location=url.toString(); 725 type=param; 726 inputsource=url.openStream(); 727 if( sourceType == null ) { 728 sourceType = sourceTypeFromExt(location); 729 } 730 } else if( source instanceof File ) { 731 location=((File )source).getAbsolutePath(); 732 inputsource=new FileInputStream ((File )source); 733 type=param; 734 if( sourceType == null ) { 735 sourceType = sourceTypeFromExt(location); 736 } 737 } else if( source instanceof InputStream ) { 738 type=param; 739 inputsource=source; 740 } else if( source instanceof Class ) { 741 location=((Class )source).getName(); 742 type=param; 743 inputsource=source; 744 if( sourceType== null ) { 745 sourceType="MbeansDescriptorsIntrospectionSource"; 746 } 747 } 748 749 if( sourceType==null ) { 750 sourceType="MbeansDescriptorsDigesterSource"; 751 } 752 ModelerSource ds=getModelerSource(sourceType); 753 List mbeans=ds.loadDescriptors(this, location, type, inputsource); 754 755 return mbeans; 756 } 757 758 private String sourceTypeFromExt( String s ) { 759 if( s.endsWith( ".ser")) { 760 return "MbeansDescriptorsSerSource"; 761 } 762 else if( s.endsWith(".xml")) { 763 return "MbeansDescriptorsDigesterSource"; 764 } 765 return null; 766 } 767 768 776 public void registerComponent(Object bean, ObjectName oname, String type) 777 throws Exception 778 { 779 if( log.isDebugEnabled() ) { 780 log.debug( "Managed= "+ oname); 781 } 782 783 if( bean ==null ) { 784 log.error("Null component " + oname ); 785 return; 786 } 787 788 try { 789 if( type==null ) { 790 type=bean.getClass().getName(); 791 } 792 793 ManagedBean managed = findManagedBean(bean.getClass(), type); 794 795 DynamicMBean mbean = managed.createMBean(bean); 797 798 if( getMBeanServer().isRegistered( oname )) { 799 if( log.isDebugEnabled()) { 800 log.debug("Unregistering existing component " + oname ); 801 } 802 getMBeanServer().unregisterMBean( oname ); 803 } 804 805 getMBeanServer().registerMBean( mbean, oname); 806 } catch( Exception ex) { 807 log.error("Error registering " + oname, ex ); 808 throw ex; 809 } 810 } 811 812 817 public void loadDescriptors( String packageName, ClassLoader classLoader ) { 818 String res=packageName.replace( '.', '/'); 819 820 if( log.isTraceEnabled() ) { 821 log.trace("Finding descriptor " + res ); 822 } 823 824 if( searchedPaths.get( packageName ) != null ) { 825 return; 826 } 827 String descriptors=res + "/mbeans-descriptors.ser"; 828 829 URL dURL=classLoader.getResource( descriptors ); 830 831 if( dURL == null ) { 832 descriptors=res + "/mbeans-descriptors.xml"; 833 dURL=classLoader.getResource( descriptors ); 834 } 835 if( dURL == null ) { 836 return; 837 } 838 839 log.debug( "Found " + dURL); 840 searchedPaths.put( packageName, dURL ); 841 try { 842 if( descriptors.endsWith(".xml" )) 843 loadDescriptors("MbeansDescriptorsDigesterSource", dURL, null); 844 else 845 loadDescriptors("MbeansDescriptorsSerSource", dURL, null); 846 return; 847 } catch(Exception ex ) { 848 log.error("Error loading " + dURL); 849 } 850 851 return; 852 } 853 854 862 public void loadDescriptors( String sourceType, Object source, String param) 863 throws Exception 864 { 865 List mbeans=load( sourceType, source, param ); 866 if( mbeans == null) return; 867 868 Iterator itr=mbeans.iterator(); 869 while( itr.hasNext() ) { 870 Object mb=itr.next(); 871 if( mb instanceof ManagedBean) { 872 addManagedBean((ManagedBean)mb); 873 } 874 } 875 } 876 877 883 private void findDescriptor( Class beanClass, String type ) { 884 if( type==null ) { 885 type=beanClass.getName(); 886 } 887 ClassLoader classLoader=null; 888 if( beanClass!=null ) { 889 classLoader=beanClass.getClassLoader(); 890 } 891 if( classLoader==null ) { 892 classLoader=Thread.currentThread().getContextClassLoader(); 893 } 894 if( classLoader==null ) { 895 classLoader=this.getClass().getClassLoader(); 896 } 897 898 String className=type; 899 String pkg=className; 900 while( pkg.indexOf( ".") > 0 ) { 901 int lastComp=pkg.lastIndexOf( "."); 902 if( lastComp <= 0 ) return; 903 pkg=pkg.substring(0, lastComp); 904 if( searchedPaths.get( pkg ) != null ) { 905 return; 906 } 907 loadDescriptors(pkg, classLoader); 908 } 909 return; 910 } 911 912 private ModelerSource getModelerSource( String type ) 913 throws Exception 914 { 915 if( type==null ) type="MbeansDescriptorsDigesterSource"; 916 if( type.indexOf( ".") < 0 ) { 917 type="org.apache.tomcat.util.modeler.modules." + type; 918 } 919 920 Class c=Class.forName( type ); 921 ModelerSource ds=(ModelerSource)c.newInstance(); 922 return ds; 923 } 924 925 926 928 public ObjectName preRegister(MBeanServer server, 929 ObjectName name) throws Exception 930 { 931 this.server=server; 932 return name; 933 } 934 935 public void postRegister(Boolean registrationDone) { 936 } 937 938 public void preDeregister() throws Exception { 939 } 940 941 public void postDeregister() { 942 } 943 944 945 946 947 951 954 public void unregisterRegistry(ClassLoader loader ) { 955 perLoaderRegistries.remove(loader); 957 } 958 959 public ManagedBean findManagedBean(Class beanClass, String type) 960 throws Exception 961 { 962 return findManagedBean(null, beanClass, type); 963 } 964 965 971 public void setMBeanServer( MBeanServer server ) { 972 this.server=server; 973 } 974 975 public void resetMetadata() { 976 stop(); 977 } 978 986 public void loadDescriptors( Object source ) 987 throws Exception 988 { 989 loadDescriptors("MbeansDescriptorsDigesterSource", source, null ); 990 } 991 992 994 public void registerComponent(Object bean, String domain, String type, 995 String name) 996 throws Exception 997 { 998 StringBuffer sb=new StringBuffer (); 999 sb.append( domain ).append(":"); 1000 sb.append( name ); 1001 String nameStr=sb.toString(); 1002 ObjectName oname=new ObjectName ( nameStr ); 1003 registerComponent(bean, oname, type ); 1004 } 1005 1006 1007 1008 public void unregisterComponent( String domain, String name ) { 1010 try { 1011 ObjectName oname=new ObjectName ( domain + ":" + name ); 1012 1013 getMBeanServer().unregisterMBean( oname ); 1015 } catch( Throwable t ) { 1016 log.error( "Error unregistering mbean ", t ); 1017 } 1018 } 1019 1020 1021 1030 public void loadCachedDescriptors( Object source ) 1031 throws Exception 1032 { 1033 loadDescriptors("MbeansDescriptorsSerSource", source, null ); 1034 } 1035} 1036 | Popular Tags |