1 16 17 18 package org.apache.commons.modeler; 19 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.InputStream ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.Enumeration ; 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 import javax.management.modelmbean.ModelMBean ; 42 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.logging.LogFactory; 45 import org.apache.commons.modeler.modules.ModelerSource; 46 47 56 57 75 public class Registry implements RegistryMBean, MBeanRegistration { 76 78 public static String MODELER_MANIFEST="/META-INF/mbeans-descriptors.xml"; 79 80 83 private static Log log = LogFactory.getLog(Registry.class); 84 85 87 89 private static HashMap perLoaderRegistries=null; 90 91 95 private static Registry registry = null; 96 97 99 103 private MBeanServer server = null; 104 105 109 private HashMap descriptors = new HashMap (); 110 111 113 private HashMap descriptorsByClass = new HashMap (); 114 115 private HashMap searchedPaths=new HashMap (); 117 118 private Object key; 119 private Object guard; 120 121 private Hashtable idDomains=new Hashtable (); 123 private Hashtable ids=new Hashtable (); 124 125 126 128 130 public Registry() { 131 super(); 132 } 133 134 137 154 public synchronized static Registry getRegistry(Object key, Object guard) { 155 Registry localRegistry; 156 if( perLoaderRegistries!=null ) { 157 if( key==null ) 158 key=Thread.currentThread().getContextClassLoader(); 159 if( key != null ) { 160 localRegistry=(Registry)perLoaderRegistries.get(key); 161 if( localRegistry == null ) { 162 localRegistry=new Registry(); 163 localRegistry.key=key; 164 localRegistry.guard=guard; 165 perLoaderRegistries.put( key, localRegistry ); 166 return localRegistry; 167 } 168 if( localRegistry.guard != null && 169 localRegistry.guard != guard ) { 170 return null; } 172 return localRegistry; 173 } 174 } 175 176 if (registry == null) { 178 registry = new Registry(); 179 } 180 if( registry.guard != null && 181 registry.guard != guard ) { 182 return null; 183 } 184 return (registry); 185 } 186 187 194 public static void setUseContextClassLoader( boolean enable ) { 195 if( enable ) { 196 perLoaderRegistries=new HashMap (); 197 } 198 } 199 200 202 209 public void setGuard( Object guard ) { 210 if( this.guard!=null ) { 211 return; } 213 this.guard=guard; 214 } 215 216 220 public void stop() { 221 descriptorsByClass = new HashMap (); 222 descriptors = new HashMap (); 223 searchedPaths=new HashMap (); 224 } 225 226 241 public List loadMBeans( Object source, ClassLoader cl ) 242 throws Exception 243 { 244 return load("MbeansSource", source, null ); 245 } 246 247 248 261 public void loadMetadata(Object source ) throws Exception { 262 if( source instanceof ClassLoader ) { 263 loadMetaInfDescriptors((ClassLoader )source); 264 return; 265 } else { 266 loadDescriptors( null, source, null ); 267 } 268 269 } 270 271 300 public void registerComponent(Object bean, String oname, String type) 301 throws Exception 302 { 303 registerComponent(bean, new ObjectName (oname), type); 304 } 305 306 313 public void unregisterComponent( String oname ) { 314 try { 315 unregisterComponent(new ObjectName (oname)); 316 } catch (MalformedObjectNameException e) { 317 log.info("Error creating object name " + e ); 318 } 319 } 320 321 322 331 public void invoke( List mbeans, String operation, boolean failFirst ) 332 throws Exception 333 { 334 if( mbeans==null ) { 335 return; 336 } 337 Iterator itr=mbeans.iterator(); 338 while(itr.hasNext()) { 339 Object current=itr.next(); 340 ObjectName oN=null; 341 try { 342 if( current instanceof ObjectName ) { 343 oN=(ObjectName )current; 344 } 345 if( current instanceof String ) { 346 oN=new ObjectName ( (String )current ); 347 } 348 if( oN==null ) { 349 continue; 350 } 351 if( getMethodInfo(oN, operation) == null) { 352 continue; 353 } 354 getMBeanServer().invoke(oN, operation, 355 new Object [] {}, new String [] {}); 356 357 } catch( Exception t ) { 358 if( failFirst ) throw t; 359 log.info("Error initializing " + current + " " + t.toString()); 360 } 361 } 362 } 363 364 366 374 public synchronized int getId( String domain, String name) { 375 if( domain==null) { 376 domain=""; 377 } 378 Hashtable domainTable=(Hashtable )idDomains.get( domain ); 379 if( domainTable == null ) { 380 domainTable=new Hashtable (); 381 idDomains.put( domain, domainTable); 382 } 383 if( name==null ) { 384 name=""; 385 } 386 Integer i=(Integer )domainTable.get(name); 387 388 if( i!= null ) { 389 return i.intValue(); 390 } 391 392 int id[]=(int [])ids.get( domain ); 393 if( id == null ) { 394 id=new int[1]; 395 ids.put( domain, id); 396 } 397 int code=id[0]++; 398 domainTable.put( name, new Integer ( code )); 399 return code; 400 } 401 402 405 412 public void addManagedBean(ManagedBean bean) { 413 descriptors.put(bean.getName(), bean); 415 if( bean.getType() != null ) { 416 descriptorsByClass.put( bean.getType(), bean ); 417 } 418 } 419 420 421 429 public ManagedBean findManagedBean(String name) { 430 ManagedBean mb=((ManagedBean) descriptors.get(name)); 432 if( mb==null ) 433 mb=(ManagedBean)descriptorsByClass.get(name); 434 return mb; 435 } 436 437 443 public String [] findManagedBeans() { 444 return ((String []) descriptors.keySet().toArray(new String [0])); 445 } 446 447 448 456 public String [] findManagedBeans(String group) { 457 458 ArrayList results = new ArrayList (); 459 Iterator items = descriptors.values().iterator(); 460 while (items.hasNext()) { 461 ManagedBean item = (ManagedBean) items.next(); 462 if ((group == null) && (item.getGroup() == null)) { 463 results.add(item.getName()); 464 } else if (group.equals(item.getGroup())) { 465 results.add(item.getName()); 466 } 467 } 468 String values[] = new String [results.size()]; 469 return ((String []) results.toArray(values)); 470 471 } 472 473 474 480 public void removeManagedBean(ManagedBean bean) { 481 descriptors.remove(bean.getName()); 483 descriptorsByClass.remove( bean.getType()); 484 } 485 486 488 495 public static MBeanServer getServer() { 496 return Registry.getRegistry().getMBeanServer(); 497 } 498 499 507 public static void setServer(MBeanServer mbeanServer) { 508 Registry.getRegistry().setServer(mbeanServer); 509 } 510 511 522 public static void loadRegistry(InputStream stream) throws Exception { 523 Registry registry = getRegistry(); 524 registry.loadMetadata(stream); 525 } 526 527 533 public synchronized static Registry getRegistry() { 534 return getRegistry(null, null); 535 } 536 537 539 546 public String getType( ObjectName oname, String attName ) 547 { 548 String type=null; 549 MBeanInfo info=null; 550 try { 551 info=server.getMBeanInfo(oname); 552 } catch (Exception e) { 553 log.info( "Can't find metadata for object" + oname ); 554 return null; 555 } 556 557 MBeanAttributeInfo attInfo[]=info.getAttributes(); 558 for( int i=0; i<attInfo.length; i++ ) { 559 if( attName.equals(attInfo[i].getName())) { 560 type=attInfo[i].getType(); 561 return type; 562 } 563 } 564 return null; 565 } 566 567 573 public MBeanOperationInfo getMethodInfo( ObjectName oname, String opName ) 574 { 575 String type=null; 576 MBeanInfo info=null; 577 try { 578 info=server.getMBeanInfo(oname); 579 } catch (Exception e) { 580 log.info( "Can't find metadata " + oname ); 581 return null; 582 } 583 MBeanOperationInfo attInfo[]=info.getOperations(); 584 for( int i=0; i<attInfo.length; i++ ) { 585 if( opName.equals(attInfo[i].getName())) { 586 return attInfo[i]; 587 } 588 } 589 return null; 590 } 591 592 597 public void unregisterComponent( ObjectName oname ) { 598 try { 599 if( getMBeanServer().isRegistered(oname)) { 600 getMBeanServer().unregisterMBean(oname); 601 } 602 } catch( Throwable t ) { 603 log.error( "Error unregistering mbean ", t); 604 } 605 } 606 607 612 public synchronized MBeanServer getMBeanServer() { 613 long t1=System.currentTimeMillis(); 614 615 if (server == null) { 616 if( MBeanServerFactory.findMBeanServer(null).size() > 0 ) { 617 server=(MBeanServer )MBeanServerFactory.findMBeanServer(null).get(0); 618 if( log.isDebugEnabled() ) { 619 log.debug("Using existing MBeanServer " + (System.currentTimeMillis() - t1 )); 620 } 621 } else { 622 server=MBeanServerFactory.createMBeanServer(); 623 if( log.isDebugEnabled() ) { 624 log.debug("Creating MBeanServer"+ (System.currentTimeMillis() - t1 )); 625 } 626 } 627 } 628 return (server); 629 } 630 631 633 public ManagedBean findManagedBean(Object bean, Class beanClass, String type) 634 throws Exception 635 { 636 if( bean!=null && beanClass==null ) { 637 beanClass=bean.getClass(); 638 } 639 640 if( type==null ) { 641 type=beanClass.getName(); 642 } 643 644 ManagedBean managed = findManagedBean(type); 646 647 if( managed==null ) { 649 if( log.isDebugEnabled() ) { 651 log.debug( "Looking for descriptor "); 652 } 653 findDescriptor( beanClass, type ); 654 655 managed=findManagedBean(type); 656 } 657 658 if( bean instanceof DynamicMBean ) { 659 if( log.isDebugEnabled() ) { 660 log.debug( "Dynamic mbean support "); 661 } 662 loadDescriptors("MbeansDescriptorsDynamicMBeanSource", 664 bean, type); 665 666 managed=findManagedBean(type); 667 } 668 669 if( managed==null ) { 671 if( log.isDebugEnabled() ) { 672 log.debug( "Introspecting "); 673 } 674 675 loadDescriptors("MbeansDescriptorsIntrospectionSource", 677 beanClass, type); 678 679 managed=findManagedBean(type); 680 if( managed==null ) { 681 log.warn( "No metadata found for " + type ); 682 return null; 683 } 684 managed.setName( type ); 685 addManagedBean(managed); 686 } 687 return managed; 688 } 689 690 691 699 public Object convertValue(String type, String value) 700 { 701 Object objValue=value; 702 703 if( type==null || "java.lang.String".equals( type )) { 704 objValue=value; 706 } else if( "javax.management.ObjectName".equals( type ) || 707 "ObjectName".equals( type )) { 708 try { 709 objValue=new ObjectName ( value ); 710 } catch (MalformedObjectNameException e) { 711 return null; 712 } 713 } else if( "java.lang.Integer".equals( type ) || 714 "int".equals( type )) { 715 objValue=new Integer ( value ); 716 } else if( "java.lang.Boolean".equals( type ) || 717 "boolean".equals( type )) { 718 objValue=new Boolean ( value ); 719 } 720 return objValue; 721 } 722 723 732 public List load( String sourceType, Object source, String param) 733 throws Exception 734 { 735 if( log.isTraceEnabled()) { 736 log.trace("load " + source ); 737 } 738 String location=null; 739 String type=null; 740 Object inputsource=null; 741 742 if( source instanceof DynamicMBean ) { 743 sourceType="MbeansDescriptorsDynamicMBeanSource"; 744 inputsource=source; 745 } else if( source instanceof URL ) { 746 URL url=(URL )source; 747 location=url.toString(); 748 type=param; 749 inputsource=url.openStream(); 750 if( sourceType == null ) { 751 sourceType = sourceTypeFromExt(location); 752 } 753 } else if( source instanceof File ) { 754 location=((File )source).getAbsolutePath(); 755 inputsource=new FileInputStream ((File )source); 756 type=param; 757 if( sourceType == null ) { 758 sourceType = sourceTypeFromExt(location); 759 } 760 } else if( source instanceof InputStream ) { 761 type=param; 762 inputsource=source; 763 } else if( source instanceof Class ) { 764 location=((Class )source).getName(); 765 type=param; 766 inputsource=source; 767 if( sourceType== null ) { 768 sourceType="MbeansDescriptorsIntrospectionSource"; 769 } 770 } 771 772 if( sourceType==null ) { 773 sourceType="MbeansDescriptorsDOMSource"; 774 } 775 ModelerSource ds=getModelerSource(sourceType); 776 List mbeans=ds.loadDescriptors(this, location, type, inputsource); 777 778 return mbeans; 779 } 780 781 private String sourceTypeFromExt( String s ) { 782 if( s.endsWith( ".ser")) { 783 return "MbeansDescriptorsSerSource"; 784 } 785 else if( s.endsWith(".xml")) { 786 return "MbeansDescriptorsDOMSource"; 787 } 788 return null; 789 } 790 791 799 public void registerComponent(Object bean, ObjectName oname, String type) 800 throws Exception 801 { 802 if( log.isDebugEnabled() ) { 803 log.debug( "Managed= "+ oname); 804 } 805 806 if( bean ==null ) { 807 log.error("Null component " + oname ); 808 return; 809 } 810 811 try { 812 if( type==null ) { 813 type=bean.getClass().getName(); 814 } 815 816 ManagedBean managed = findManagedBean(bean.getClass(), type); 817 818 ModelMBean mbean = managed.createMBean(bean); 820 821 if( getMBeanServer().isRegistered( oname )) { 822 if( log.isDebugEnabled()) { 823 log.debug("Unregistering existing component " + oname ); 824 } 825 getMBeanServer().unregisterMBean( oname ); 826 } 827 828 getMBeanServer().registerMBean( mbean, oname); 829 } catch( Exception ex) { 830 log.error("Error registering " + oname, ex ); 831 throw ex; 832 } 833 } 834 835 840 public void loadDescriptors( String packageName, ClassLoader classLoader ) { 841 String res=packageName.replace( '.', '/'); 842 843 if( log.isTraceEnabled() ) { 844 log.trace("Finding descriptor " + res ); 845 } 846 847 if( searchedPaths.get( packageName ) != null ) { 848 return; 849 } 850 String descriptors=res + "/mbeans-descriptors.ser"; 851 852 URL dURL=classLoader.getResource( descriptors ); 853 854 if( dURL == null ) { 855 descriptors=res + "/mbeans-descriptors.xml"; 856 dURL=classLoader.getResource( descriptors ); 857 } 858 if( dURL == null ) { 859 return; 860 } 861 862 log.debug( "Found " + dURL); 863 searchedPaths.put( packageName, dURL ); 864 try { 865 if( descriptors.endsWith(".xml" )) 866 loadDescriptors("MbeansDescriptorsDOMSource", dURL, null); 867 else 868 loadDescriptors("MbeansDescriptorsSerSource", dURL, null); 869 return; 870 } catch(Exception ex ) { 871 log.error("Error loading " + dURL); 872 } 873 874 return; 875 } 876 877 885 public void loadDescriptors( String sourceType, Object source, String param) 886 throws Exception 887 { 888 List mbeans=load( sourceType, source, param ); 889 if( mbeans == null) return; 890 891 Iterator itr=mbeans.iterator(); 892 while( itr.hasNext() ) { 893 Object mb=itr.next(); 894 if( mb instanceof ManagedBean) { 895 addManagedBean((ManagedBean)mb); 896 } 897 } 898 } 899 900 905 private void loadMetaInfDescriptors(ClassLoader cl) { 906 try { 907 Enumeration en=cl.getResources(MODELER_MANIFEST); 908 while( en.hasMoreElements() ) { 909 URL url=(URL )en.nextElement(); 910 InputStream is=url.openStream(); 911 if( log.isDebugEnabled()) log.debug("Loading " + url); 912 loadDescriptors("MBeansDescriptorDOMSource", is, null ); 913 } 914 } catch( Exception ex ) { 915 ex.printStackTrace(); 916 } 917 } 918 919 925 private void findDescriptor( Class beanClass, String type ) { 926 if( type==null ) { 927 type=beanClass.getName(); 928 } 929 ClassLoader classLoader=null; 930 if( beanClass!=null ) { 931 classLoader=beanClass.getClassLoader(); 932 } 933 if( classLoader==null ) { 934 classLoader=Thread.currentThread().getContextClassLoader(); 935 } 936 if( classLoader==null ) { 937 classLoader=this.getClass().getClassLoader(); 938 } 939 940 String className=type; 941 String pkg=className; 942 while( pkg.indexOf( ".") > 0 ) { 943 int lastComp=pkg.lastIndexOf( "."); 944 if( lastComp <= 0 ) return; 945 pkg=pkg.substring(0, lastComp); 946 if( searchedPaths.get( pkg ) != null ) { 947 return; 948 } 949 loadDescriptors(pkg, classLoader); 950 } 951 return; 952 } 953 954 private ModelerSource getModelerSource( String type ) 955 throws Exception 956 { 957 if( type==null ) type="MbeansDescriptorsDOMSource"; 958 if( type.indexOf( ".") < 0 ) { 959 type="org.apache.commons.modeler.modules." + type; 960 } 961 962 Class c=Class.forName( type ); 963 ModelerSource ds=(ModelerSource)c.newInstance(); 964 return ds; 965 } 966 967 968 970 public ObjectName preRegister(MBeanServer server, 971 ObjectName name) throws Exception 972 { 973 this.server=server; 974 return name; 975 } 976 977 public void postRegister(Boolean registrationDone) { 978 } 979 980 public void preDeregister() throws Exception { 981 } 982 983 public void postDeregister() { 984 } 985 986 987 988 989 993 996 public void unregisterRegistry(ClassLoader loader ) { 997 perLoaderRegistries.remove(loader); 999 } 1000 1001 public ManagedBean findManagedBean(Class beanClass, String type) 1002 throws Exception 1003 { 1004 return findManagedBean(null, beanClass, type); 1005 } 1006 1007 1013 public void setMBeanServer( MBeanServer server ) { 1014 this.server=server; 1015 } 1016 1017 public void resetMetadata() { 1018 stop(); 1019 } 1020 1028 public void loadDescriptors( Object source ) 1029 throws Exception 1030 { 1031 loadDescriptors("MbeansDescriptorsDOMSource", source, null ); 1032 } 1033 1034 1036 public void registerComponent(Object bean, String domain, String type, 1037 String name) 1038 throws Exception 1039 { 1040 StringBuffer sb=new StringBuffer (); 1041 sb.append( domain ).append(":"); 1042 sb.append( name ); 1043 String nameStr=sb.toString(); 1044 ObjectName oname=new ObjectName ( nameStr ); 1045 registerComponent(bean, oname, type ); 1046 } 1047 1048 1049 1050 public void unregisterComponent( String domain, String name ) { 1052 try { 1053 ObjectName oname=new ObjectName ( domain + ":" + name ); 1054 1055 getMBeanServer().unregisterMBean( oname ); 1057 } catch( Throwable t ) { 1058 log.error( "Error unregistering mbean ", t ); 1059 } 1060 } 1061 1062 public List loadMBeans( Object source ) 1063 throws Exception 1064 { 1065 return loadMBeans( source, null ); 1066 } 1067 1068 1069 1078 public void loadCachedDescriptors( Object source ) 1079 throws Exception 1080 { 1081 loadDescriptors("MbeansDescriptorsSerSource", source, null ); 1082 } 1083} 1084 | Popular Tags |