1 17 18 package org.apache.catalina.mbeans; 19 20 21 import java.util.Hashtable ; 22 23 import javax.management.DynamicMBean ; 24 import javax.management.MBeanException ; 25 import javax.management.MBeanServer ; 26 import javax.management.MalformedObjectNameException ; 27 import javax.management.ObjectName ; 28 import javax.management.modelmbean.ModelMBean ; 29 30 import org.apache.catalina.Contained; 31 import org.apache.catalina.Container; 32 import org.apache.catalina.Context; 33 import org.apache.catalina.Engine; 34 import org.apache.catalina.Group; 35 import org.apache.catalina.Host; 36 import org.apache.catalina.Loader; 37 import org.apache.catalina.Manager; 38 import org.apache.catalina.Realm; 39 import org.apache.catalina.Role; 40 import org.apache.catalina.Server; 41 import org.apache.catalina.Service; 42 import org.apache.catalina.User; 43 import org.apache.catalina.UserDatabase; 44 import org.apache.catalina.Valve; 45 import org.apache.catalina.connector.Connector; 46 import org.apache.catalina.deploy.ContextEnvironment; 47 import org.apache.catalina.deploy.ContextResource; 48 import org.apache.catalina.deploy.ContextResourceLink; 49 import org.apache.catalina.deploy.NamingResources; 50 import org.apache.catalina.valves.ValveBase; 51 import org.apache.commons.logging.Log; 52 import org.apache.commons.logging.LogFactory; 53 import org.apache.tomcat.util.IntrospectionUtils; 54 import org.apache.tomcat.util.modeler.ManagedBean; 55 import org.apache.tomcat.util.modeler.Registry; 56 57 58 65 66 public class MBeanUtils { 67 private static Log log = LogFactory.getLog(MBeanUtils.class); 68 69 71 72 77 private static String exceptions[][] = { 78 { "org.apache.ajp.tomcat4.Ajp13Connector", 79 "Ajp13Connector" }, 80 { "org.apache.coyote.tomcat4.Ajp13Connector", 81 "CoyoteConnector" }, 82 { "org.apache.catalina.users.JDBCGroup", 83 "Group" }, 84 { "org.apache.catalina.users.JDBCRole", 85 "Role" }, 86 { "org.apache.catalina.users.JDBCUser", 87 "User" }, 88 { "org.apache.catalina.users.MemoryGroup", 89 "Group" }, 90 { "org.apache.catalina.users.MemoryRole", 91 "Role" }, 92 { "org.apache.catalina.users.MemoryUser", 93 "User" }, 94 }; 95 96 97 100 private static Registry registry = createRegistry(); 101 102 103 106 private static MBeanServer mserver = createServer(); 107 108 109 111 117 static String createManagedName(Object component) { 118 119 String className = component.getClass().getName(); 121 for (int i = 0; i < exceptions.length; i++) { 122 if (className.equals(exceptions[i][0])) { 123 return (exceptions[i][1]); 124 } 125 } 126 127 int period = className.lastIndexOf('.'); 129 if (period >= 0) 130 className = className.substring(period + 1); 131 return (className); 132 133 } 134 135 136 144 static DynamicMBean createMBean(Connector connector) 145 throws Exception { 146 147 String mname = createManagedName(connector); 148 ManagedBean managed = registry.findManagedBean(mname); 149 if (managed == null) { 150 Exception e = new Exception ("ManagedBean is not found with "+mname); 151 throw new MBeanException (e); 152 } 153 String domain = managed.getDomain(); 154 if (domain == null) 155 domain = mserver.getDefaultDomain(); 156 DynamicMBean mbean = managed.createMBean(connector); 157 ObjectName oname = createObjectName(domain, connector); 158 if( mserver.isRegistered( oname )) { 159 mserver.unregisterMBean(oname); 160 } 161 mserver.registerMBean(mbean, oname); 162 return (mbean); 163 164 } 165 166 167 175 static DynamicMBean createMBean(Context context) 176 throws Exception { 177 178 String mname = createManagedName(context); 179 ManagedBean managed = registry.findManagedBean(mname); 180 if (managed == null) { 181 Exception e = new Exception ("ManagedBean is not found with "+mname); 182 throw new MBeanException (e); 183 } 184 String domain = managed.getDomain(); 185 if (domain == null) 186 domain = mserver.getDefaultDomain(); 187 DynamicMBean mbean = managed.createMBean(context); 188 ObjectName oname = createObjectName(domain, context); 189 if( mserver.isRegistered(oname)) { 190 log.debug("Already registered " + oname); 191 mserver.unregisterMBean(oname); 192 } 193 mserver.registerMBean(mbean, oname); 194 return (mbean); 195 196 } 197 198 199 207 static DynamicMBean createMBean(ContextEnvironment environment) 208 throws Exception { 209 210 String mname = createManagedName(environment); 211 ManagedBean managed = registry.findManagedBean(mname); 212 if (managed == null) { 213 Exception e = new Exception ("ManagedBean is not found with "+mname); 214 throw new MBeanException (e); 215 } 216 String domain = managed.getDomain(); 217 if (domain == null) 218 domain = mserver.getDefaultDomain(); 219 DynamicMBean mbean = managed.createMBean(environment); 220 ObjectName oname = createObjectName(domain, environment); 221 if( mserver.isRegistered( oname )) { 222 mserver.unregisterMBean(oname); 223 } 224 mserver.registerMBean(mbean, oname); 225 return (mbean); 226 227 } 228 229 230 238 static DynamicMBean createMBean(ContextResource resource) 239 throws Exception { 240 241 String mname = createManagedName(resource); 242 ManagedBean managed = registry.findManagedBean(mname); 243 if (managed == null) { 244 Exception e = new Exception ("ManagedBean is not found with "+mname); 245 throw new MBeanException (e); 246 } 247 String domain = managed.getDomain(); 248 if (domain == null) 249 domain = mserver.getDefaultDomain(); 250 DynamicMBean mbean = managed.createMBean(resource); 251 ObjectName oname = createObjectName(domain, resource); 252 if( mserver.isRegistered( oname )) { 253 mserver.unregisterMBean(oname); 254 } 255 mserver.registerMBean(mbean, oname); 256 return (mbean); 257 258 } 259 260 261 269 static DynamicMBean createMBean(ContextResourceLink resourceLink) 270 throws Exception { 271 272 String mname = createManagedName(resourceLink); 273 ManagedBean managed = registry.findManagedBean(mname); 274 if (managed == null) { 275 Exception e = new Exception ("ManagedBean is not found with "+mname); 276 throw new MBeanException (e); 277 } 278 String domain = managed.getDomain(); 279 if (domain == null) 280 domain = mserver.getDefaultDomain(); 281 DynamicMBean mbean = managed.createMBean(resourceLink); 282 ObjectName oname = createObjectName(domain, resourceLink); 283 if( mserver.isRegistered( oname )) { 284 mserver.unregisterMBean(oname); 285 } 286 mserver.registerMBean(mbean, oname); 287 return (mbean); 288 289 } 290 291 299 static DynamicMBean createMBean(Engine engine) 300 throws Exception { 301 302 String mname = createManagedName(engine); 303 ManagedBean managed = registry.findManagedBean(mname); 304 if (managed == null) { 305 Exception e = new Exception ("ManagedBean is not found with "+mname); 306 throw new MBeanException (e); 307 } 308 String domain = managed.getDomain(); 309 if (domain == null) 310 domain = mserver.getDefaultDomain(); 311 DynamicMBean mbean = managed.createMBean(engine); 312 ObjectName oname = createObjectName(domain, engine); 313 if( mserver.isRegistered( oname )) { 314 mserver.unregisterMBean(oname); 315 } 316 mserver.registerMBean(mbean, oname); 317 return (mbean); 318 319 } 320 321 322 330 static DynamicMBean createMBean(Group group) 331 throws Exception { 332 333 String mname = createManagedName(group); 334 ManagedBean managed = registry.findManagedBean(mname); 335 if (managed == null) { 336 Exception e = new Exception ("ManagedBean is not found with "+mname); 337 throw new MBeanException (e); 338 } 339 String domain = managed.getDomain(); 340 if (domain == null) 341 domain = mserver.getDefaultDomain(); 342 DynamicMBean mbean = managed.createMBean(group); 343 ObjectName oname = createObjectName(domain, group); 344 if( mserver.isRegistered( oname )) { 345 mserver.unregisterMBean(oname); 346 } 347 mserver.registerMBean(mbean, oname); 348 return (mbean); 349 350 } 351 352 353 361 static DynamicMBean createMBean(Host host) 362 throws Exception { 363 364 String mname = createManagedName(host); 365 ManagedBean managed = registry.findManagedBean(mname); 366 if (managed == null) { 367 Exception e = new Exception ("ManagedBean is not found with "+mname); 368 throw new MBeanException (e); 369 } 370 String domain = managed.getDomain(); 371 if (domain == null) 372 domain = mserver.getDefaultDomain(); 373 DynamicMBean mbean = managed.createMBean(host); 374 ObjectName oname = createObjectName(domain, host); 375 if( mserver.isRegistered( oname )) { 376 mserver.unregisterMBean(oname); 377 } 378 mserver.registerMBean(mbean, oname); 379 return (mbean); 380 381 } 382 383 384 392 static DynamicMBean createMBean(Loader loader) 393 throws Exception { 394 395 String mname = createManagedName(loader); 396 ManagedBean managed = registry.findManagedBean(mname); 397 if (managed == null) { 398 Exception e = new Exception ("ManagedBean is not found with "+mname); 399 throw new MBeanException (e); 400 } 401 String domain = managed.getDomain(); 402 if (domain == null) 403 domain = mserver.getDefaultDomain(); 404 DynamicMBean mbean = managed.createMBean(loader); 405 ObjectName oname = createObjectName(domain, loader); 406 if( mserver.isRegistered( oname )) { 407 mserver.unregisterMBean( oname ); 409 } 410 mserver.registerMBean(mbean, oname); 411 return (mbean); 412 413 } 414 415 423 static DynamicMBean createMBean(Manager manager) 424 throws Exception { 425 426 String mname = createManagedName(manager); 427 ManagedBean managed = registry.findManagedBean(mname); 428 if (managed == null) { 429 Exception e = new Exception ("ManagedBean is not found with "+mname); 430 throw new MBeanException (e); 431 } 432 String domain = managed.getDomain(); 433 if (domain == null) 434 domain = mserver.getDefaultDomain(); 435 DynamicMBean mbean = managed.createMBean(manager); 436 ObjectName oname = createObjectName(domain, manager); 437 if( mserver.isRegistered( oname )) { 438 mserver.unregisterMBean(oname); 439 } 440 mserver.registerMBean(mbean, oname); 441 return (mbean); 442 443 } 444 445 446 454 static DynamicMBean createMBean(MBeanFactory factory) 455 throws Exception { 456 457 String mname = createManagedName(factory); 458 ManagedBean managed = registry.findManagedBean(mname); 459 if (managed == null) { 460 Exception e = new Exception ("ManagedBean is not found with "+mname); 461 throw new MBeanException (e); 462 } 463 String domain = managed.getDomain(); 464 if (domain == null) 465 domain = mserver.getDefaultDomain(); 466 DynamicMBean mbean = managed.createMBean(factory); 467 ObjectName oname = createObjectName(domain, factory); 468 if( mserver.isRegistered(oname )) { 469 mserver.unregisterMBean(oname); 470 } 471 mserver.registerMBean(mbean, oname); 472 return (mbean); 473 474 } 475 476 477 485 static DynamicMBean createMBean(NamingResources resource) 486 throws Exception { 487 488 String mname = createManagedName(resource); 489 ManagedBean managed = registry.findManagedBean(mname); 490 if (managed == null) { 491 Exception e = new Exception ("ManagedBean is not found with "+mname); 492 throw new MBeanException (e); 493 } 494 String domain = managed.getDomain(); 495 if (domain == null) 496 domain = mserver.getDefaultDomain(); 497 DynamicMBean mbean = managed.createMBean(resource); 498 ObjectName oname = createObjectName(domain, resource); 499 if( mserver.isRegistered( oname )) { 500 mserver.unregisterMBean(oname); 501 } 502 mserver.registerMBean(mbean, oname); 503 return (mbean); 504 505 } 506 507 508 516 static DynamicMBean createMBean(Realm realm) 517 throws Exception { 518 519 String mname = createManagedName(realm); 520 ManagedBean managed = registry.findManagedBean(mname); 521 if (managed == null) { 522 Exception e = new Exception ("ManagedBean is not found with "+mname); 523 throw new MBeanException (e); 524 } 525 String domain = managed.getDomain(); 526 if (domain == null) 527 domain = mserver.getDefaultDomain(); 528 DynamicMBean mbean = managed.createMBean(realm); 529 ObjectName oname = createObjectName(domain, realm); 530 if( mserver.isRegistered( oname )) { 531 mserver.unregisterMBean(oname); 532 } 533 mserver.registerMBean(mbean, oname); 534 return (mbean); 535 536 } 537 538 539 547 static DynamicMBean createMBean(Role role) 548 throws Exception { 549 550 String mname = createManagedName(role); 551 ManagedBean managed = registry.findManagedBean(mname); 552 if (managed == null) { 553 Exception e = new Exception ("ManagedBean is not found with "+mname); 554 throw new MBeanException (e); 555 } 556 String domain = managed.getDomain(); 557 if (domain == null) 558 domain = mserver.getDefaultDomain(); 559 DynamicMBean mbean = managed.createMBean(role); 560 ObjectName oname = createObjectName(domain, role); 561 if( mserver.isRegistered( oname )) { 562 mserver.unregisterMBean(oname); 563 } 564 mserver.registerMBean(mbean, oname); 565 return (mbean); 566 567 } 568 569 570 578 static DynamicMBean createMBean(Server server) 579 throws Exception { 580 581 String mname = createManagedName(server); 582 ManagedBean managed = registry.findManagedBean(mname); 583 if (managed == null) { 584 Exception e = new Exception ("ManagedBean is not found with "+mname); 585 throw new MBeanException (e); 586 } 587 String domain = managed.getDomain(); 588 if (domain == null) 589 domain = mserver.getDefaultDomain(); 590 DynamicMBean mbean = managed.createMBean(server); 591 ObjectName oname = createObjectName(domain, server); 592 if( mserver.isRegistered( oname )) { 593 mserver.unregisterMBean(oname); 594 } 595 mserver.registerMBean(mbean, oname); 596 return (mbean); 597 598 } 599 600 601 609 static DynamicMBean createMBean(Service service) 610 throws Exception { 611 612 String mname = createManagedName(service); 613 ManagedBean managed = registry.findManagedBean(mname); 614 if (managed == null) { 615 Exception e = new Exception ("ManagedBean is not found with "+mname); 616 throw new MBeanException (e); 617 } 618 String domain = managed.getDomain(); 619 if (domain == null) 620 domain = mserver.getDefaultDomain(); 621 DynamicMBean mbean = managed.createMBean(service); 622 ObjectName oname = createObjectName(domain, service); 623 if( mserver.isRegistered( oname )) { 624 mserver.unregisterMBean(oname); 625 } 626 mserver.registerMBean(mbean, oname); 627 return (mbean); 628 629 } 630 631 632 640 static DynamicMBean createMBean(User user) 641 throws Exception { 642 643 String mname = createManagedName(user); 644 ManagedBean managed = registry.findManagedBean(mname); 645 if (managed == null) { 646 Exception e = new Exception ("ManagedBean is not found with "+mname); 647 throw new MBeanException (e); 648 } 649 String domain = managed.getDomain(); 650 if (domain == null) 651 domain = mserver.getDefaultDomain(); 652 DynamicMBean mbean = managed.createMBean(user); 653 ObjectName oname = createObjectName(domain, user); 654 if( mserver.isRegistered( oname )) { 655 mserver.unregisterMBean(oname); 656 } 657 mserver.registerMBean(mbean, oname); 658 return (mbean); 659 660 } 661 662 663 671 static DynamicMBean createMBean(UserDatabase userDatabase) 672 throws Exception { 673 674 String mname = createManagedName(userDatabase); 675 ManagedBean managed = registry.findManagedBean(mname); 676 if (managed == null) { 677 Exception e = new Exception ("ManagedBean is not found with "+mname); 678 throw new MBeanException (e); 679 } 680 String domain = managed.getDomain(); 681 if (domain == null) 682 domain = mserver.getDefaultDomain(); 683 DynamicMBean mbean = managed.createMBean(userDatabase); 684 ObjectName oname = createObjectName(domain, userDatabase); 685 if( mserver.isRegistered( oname )) { 686 mserver.unregisterMBean(oname); 687 } 688 mserver.registerMBean(mbean, oname); 689 return (mbean); 690 691 } 692 693 694 702 static DynamicMBean createMBean(Valve valve) 703 throws Exception { 704 705 String mname = createManagedName(valve); 706 ManagedBean managed = registry.findManagedBean(mname); 707 if (managed == null) { 708 Exception e = new Exception ("ManagedBean is not found with "+mname); 709 throw new MBeanException (e); 710 } 711 String domain = managed.getDomain(); 712 if (domain == null) 713 domain = mserver.getDefaultDomain(); 714 DynamicMBean mbean = managed.createMBean(valve); 715 ObjectName oname = createObjectName(domain, valve); 716 if( mserver.isRegistered( oname )) { 717 mserver.unregisterMBean(oname); 718 } 719 mserver.registerMBean(mbean, oname); 720 return (mbean); 721 722 } 723 724 733 static ObjectName createObjectName(String domain, 734 Connector connector) 735 throws MalformedObjectNameException { 736 737 ObjectName name = null; 738 if (connector.getClass().getName().indexOf("CoyoteConnector") >= 0 ) { 739 try { 740 String address = (String ) 741 IntrospectionUtils.getProperty(connector, "address"); 742 Integer port = (Integer ) 743 IntrospectionUtils.getProperty(connector, "port"); 744 Service service = connector.getService(); 745 String serviceName = null; 746 if (service != null) 747 serviceName = service.getName(); 748 StringBuffer sb = new StringBuffer (domain); 749 sb.append(":type=Connector"); 750 sb.append(",port=" + port); 751 if ((address != null) && (address.length()>0)) { 752 sb.append(",address=" + address); 753 } 754 name = new ObjectName (sb.toString()); 755 return (name); 756 } catch (Exception e) { 757 throw new MalformedObjectNameException 758 ("Cannot create object name for " + connector+e); 759 } 760 } else { 761 throw new MalformedObjectNameException 762 ("Cannot create object name for " + connector); 763 } 764 765 } 766 767 768 777 static ObjectName createObjectName(String domain, 778 Context context) 779 throws MalformedObjectNameException { 780 781 ObjectName name = null; 782 Host host = (Host)context.getParent(); 783 Service service = ((Engine)host.getParent()).getService(); 784 String path = context.getPath(); 785 if (path.length() < 1) 786 path = "/"; 787 name = new ObjectName (domain + ":j2eeType=WebModule,name=//" + 789 host.getName()+ path + 790 ",J2EEApplication=none,J2EEServer=none"); 791 792 return (name); 793 794 } 795 796 797 806 public static ObjectName createObjectName(String domain, 807 ContextEnvironment environment) 808 throws MalformedObjectNameException { 809 810 ObjectName name = null; 811 Object container = 812 environment.getNamingResources().getContainer(); 813 if (container instanceof Server ) { 814 name = new ObjectName (domain + ":type=Environment" + 815 ",resourcetype=Global,name=" + environment.getName()); 816 } else if (container instanceof Context) { 817 String path = ((Context)container).getPath(); 818 if (path.length() < 1) 819 path = "/"; 820 Host host = (Host) ((Context)container).getParent(); 821 Engine engine = (Engine) host.getParent(); 822 Service service = engine.getService(); 823 name = new ObjectName (domain + ":type=Environment" + 824 ",resourcetype=Context,path=" + path + 825 ",host=" + host.getName() + 826 ",name=" + environment.getName()); 827 } 828 return (name); 829 830 } 831 832 833 842 public static ObjectName createObjectName(String domain, 843 ContextResource resource) 844 throws MalformedObjectNameException { 845 846 ObjectName name = null; 847 String quotedResourceName = ObjectName.quote(resource.getName()); 848 Object container = 849 resource.getNamingResources().getContainer(); 850 if (container instanceof Server ) { 851 name = new ObjectName (domain + ":type=Resource" + 852 ",resourcetype=Global,class=" + resource.getType() + 853 ",name=" + quotedResourceName); 854 } else if (container instanceof Context) { 855 String path = ((Context)container).getPath(); 856 if (path.length() < 1) 857 path = "/"; 858 Host host = (Host) ((Context)container).getParent(); 859 Engine engine = (Engine) host.getParent(); 860 Service service = engine.getService(); 861 name = new ObjectName (domain + ":type=Resource" + 862 ",resourcetype=Context,path=" + path + 863 ",host=" + host.getName() + 864 ",class=" + resource.getType() + 865 ",name=" + quotedResourceName); 866 } 867 868 return (name); 869 870 } 871 872 873 882 public static ObjectName createObjectName(String domain, 883 ContextResourceLink resourceLink) 884 throws MalformedObjectNameException { 885 886 ObjectName name = null; 887 String quotedResourceLinkName 888 = ObjectName.quote(resourceLink.getName()); 889 Object container = 890 resourceLink.getNamingResources().getContainer(); 891 if (container instanceof Server ) { 892 name = new ObjectName (domain + ":type=ResourceLink" + 893 ",resourcetype=Global" + 894 ",name=" + quotedResourceLinkName); 895 } else if (container instanceof Context) { 896 String path = ((Context)container).getPath(); 897 if (path.length() < 1) 898 path = "/"; 899 Host host = (Host) ((Context)container).getParent(); 900 Engine engine = (Engine) host.getParent(); 901 Service service = engine.getService(); 902 name = new ObjectName (domain + ":type=ResourceLink" + 903 ",resourcetype=Context,path=" + path + 904 ",host=" + host.getName() + 905 ",name=" + quotedResourceLinkName); 906 } 907 908 return (name); 909 910 } 911 912 913 914 923 static ObjectName createObjectName(String domain, 924 Engine engine) 925 throws MalformedObjectNameException { 926 927 ObjectName name = null; 928 name = new ObjectName (domain + ":type=Engine"); 929 return (name); 930 931 } 932 933 934 943 static ObjectName createObjectName(String domain, 944 Group group) 945 throws MalformedObjectNameException { 946 947 ObjectName name = null; 948 name = new ObjectName (domain + ":type=Group,groupname=" + 949 ObjectName.quote(group.getGroupname()) + 950 ",database=" + group.getUserDatabase().getId()); 951 return (name); 952 953 } 954 955 956 965 static ObjectName createObjectName(String domain, 966 Host host) 967 throws MalformedObjectNameException { 968 969 ObjectName name = null; 970 Engine engine = (Engine)host.getParent(); 971 Service service = engine.getService(); 972 name = new ObjectName (domain + ":type=Host,host=" + 973 host.getName()); 974 return (name); 975 976 } 977 978 979 988 static ObjectName createObjectName(String domain, 989 Loader loader) 990 throws MalformedObjectNameException { 991 992 ObjectName name = null; 993 Container container = loader.getContainer(); 994 995 if (container instanceof Engine) { 996 Service service = ((Engine)container).getService(); 997 name = new ObjectName (domain + ":type=Loader"); 998 } else if (container instanceof Host) { 999 Engine engine = (Engine) container.getParent(); 1000 Service service = engine.getService(); 1001 name = new ObjectName (domain + ":type=Loader,host=" + 1002 container.getName()); 1003 } else if (container instanceof Context) { 1004 String path = ((Context)container).getPath(); 1005 if (path.length() < 1) { 1006 path = "/"; 1007 } 1008 Host host = (Host) container.getParent(); 1009 Engine engine = (Engine) host.getParent(); 1010 Service service = engine.getService(); 1011 name = new ObjectName (domain + ":type=Loader,path=" + path + 1012 ",host=" + host.getName()); 1013 } 1014 1015 return (name); 1016 1017 } 1018 1019 1020 1029 static ObjectName createObjectName(String domain, 1030 Manager manager) 1031 throws MalformedObjectNameException { 1032 1033 ObjectName name = null; 1034 Container container = manager.getContainer(); 1035 1036 if (container instanceof Engine) { 1037 Service service = ((Engine)container).getService(); 1038 name = new ObjectName (domain + ":type=Manager"); 1039 } else if (container instanceof Host) { 1040 Engine engine = (Engine) container.getParent(); 1041 Service service = engine.getService(); 1042 name = new ObjectName (domain + ":type=Manager,host=" + 1043 container.getName()); 1044 } else if (container instanceof Context) { 1045 String path = ((Context)container).getPath(); 1046 if (path.length() < 1) { 1047 path = "/"; 1048 } 1049 Host host = (Host) container.getParent(); 1050 Engine engine = (Engine) host.getParent(); 1051 Service service = engine.getService(); 1052 name = new ObjectName (domain + ":type=Manager,path=" + path + 1053 ",host=" + host.getName()); 1054 } 1055 1056 return (name); 1057 1058 } 1059 1060 1061 1070 static ObjectName createObjectName(String domain, 1071 NamingResources resources) 1072 throws MalformedObjectNameException { 1073 1074 ObjectName name = null; 1075 Object container = resources.getContainer(); 1076 if (container instanceof Server ) { 1077 name = new ObjectName (domain + ":type=NamingResources" + 1078 ",resourcetype=Global"); 1079 } else if (container instanceof Context) { 1080 String path = ((Context)container).getPath(); 1081 if (path.length() < 1) 1082 path = "/"; 1083 Host host = (Host) ((Context)container).getParent(); 1084 Engine engine = (Engine) host.getParent(); 1085 Service service = engine.getService(); 1086 name = new ObjectName (domain + ":type=NamingResources" + 1087 ",resourcetype=Context,path=" + path + 1088 ",host=" + host.getName()); 1089 } 1090 1091 return (name); 1092 1093 } 1094 1095 1096 1105 static ObjectName createObjectName(String domain, 1106 MBeanFactory factory) 1107 throws MalformedObjectNameException { 1108 1109 ObjectName name = new ObjectName (domain + ":type=MBeanFactory"); 1110 1111 return (name); 1112 1113 } 1114 1115 1116 1125 static ObjectName createObjectName(String domain, 1126 Realm realm) 1127 throws MalformedObjectNameException { 1128 1129 ObjectName name = null; 1130 Container container = realm.getContainer(); 1131 1132 if (container instanceof Engine) { 1133 Service service = ((Engine)container).getService(); 1134 name = new ObjectName (domain + ":type=Realm"); 1135 } else if (container instanceof Host) { 1136 Engine engine = (Engine) container.getParent(); 1137 Service service = engine.getService(); 1138 name = new ObjectName (domain + ":type=Realm,host=" + 1139 container.getName()); 1140 } else if (container instanceof Context) { 1141 String path = ((Context)container).getPath(); 1142 if (path.length() < 1) { 1143 path = "/"; 1144 } 1145 Host host = (Host) container.getParent(); 1146 Engine engine = (Engine) host.getParent(); 1147 Service service = engine.getService(); 1148 name = new ObjectName (domain + ":type=Realm,path=" + path + 1149 ",host=" + host.getName()); 1150 } 1151 1152 return (name); 1153 1154 } 1155 1156 1157 1166 static ObjectName createObjectName(String domain, 1167 Role role) 1168 throws MalformedObjectNameException { 1169 1170 ObjectName name = null; 1171 name = new ObjectName (domain + ":type=Role,rolename=" + 1172 role.getRolename() + ",database=" + 1173 role.getUserDatabase().getId()); 1174 return (name); 1175 1176 } 1177 1178 1179 1188 static ObjectName createObjectName(String domain, 1189 Server server) 1190 throws MalformedObjectNameException { 1191 1192 ObjectName name = null; 1193 name = new ObjectName (domain + ":type=Server"); 1194 return (name); 1195 1196 } 1197 1198 1199 1208 static ObjectName createObjectName(String domain, 1209 Service service) 1210 throws MalformedObjectNameException { 1211 1212 ObjectName name = null; 1213 name = new ObjectName (domain + ":type=Service,serviceName=" + 1214 service.getName()); 1215 return (name); 1216 1217 } 1218 1219 1220 1229 static ObjectName createObjectName(String domain, 1230 User user) 1231 throws MalformedObjectNameException { 1232 1233 ObjectName name = null; 1234 name = new ObjectName (domain + ":type=User,username=" + 1235 ObjectName.quote(user.getUsername()) 1236 + ",database=" + user.getUserDatabase().getId()); 1237 return (name); 1238 1239 } 1240 1241 1242 1251 static ObjectName createObjectName(String domain, 1252 UserDatabase userDatabase) 1253 throws MalformedObjectNameException { 1254 1255 ObjectName name = null; 1256 name = new ObjectName (domain + ":type=UserDatabase,database=" + 1257 userDatabase.getId()); 1258 return (name); 1259 1260 } 1261 1262 1263 1272 static ObjectName createObjectName(String domain, 1273 Valve valve) 1274 throws MalformedObjectNameException { 1275 if( valve instanceof ValveBase ) { 1276 ObjectName name=((ValveBase)valve).getObjectName(); 1277 if( name != null ) 1278 return name; 1279 } 1280 1281 ObjectName name = null; 1282 Container container = null; 1283 String className=valve.getClass().getName(); 1284 int period = className.lastIndexOf('.'); 1285 if (period >= 0) 1286 className = className.substring(period + 1); 1287 if( valve instanceof Contained ) { 1288 container = ((Contained)valve).getContainer(); 1289 } 1290 if( container == null ) { 1291 throw new MalformedObjectNameException ( 1292 "Cannot create mbean for non-contained valve " + 1293 valve); 1294 } 1295 if (container instanceof Engine) { 1296 Service service = ((Engine)container).getService(); 1297 String local=""; 1298 int seq = getSeq(local); 1299 String ext=""; 1300 if( seq > 0 ) { 1301 ext=",seq=" + seq; 1302 } 1303 name = new ObjectName (domain + ":type=Valve,name=" + className + 1304 ext + local ); 1305 } else if (container instanceof Host) { 1306 Service service = ((Engine)container.getParent()).getService(); 1307 String local=",host=" +container.getName(); 1308 int seq = getSeq(local); 1309 String ext=""; 1310 if( seq > 0 ) { 1311 ext=",seq=" + seq; 1312 } 1313 name = new ObjectName (domain + ":type=Valve,name=" + className + 1314 ext + local ); 1315 } else if (container instanceof Context) { 1316 String path = ((Context)container).getPath(); 1317 if (path.length() < 1) { 1318 path = "/"; 1319 } 1320 Host host = (Host) container.getParent(); 1321 Service service = ((Engine) host.getParent()).getService(); 1322 String local=",path=" + path + ",host=" + 1323 host.getName(); 1324 int seq = getSeq(local); 1325 String ext=""; 1326 if( seq > 0 ) { 1327 ext=",seq=" + seq; 1328 } 1329 name = new ObjectName (domain + ":type=Valve,name=" + className + 1330 ext + local ); 1331 } 1332 1333 return (name); 1334 1335 } 1336 1337 static Hashtable seq=new Hashtable (); 1338 static int getSeq( String key ) { 1339 int i[]=(int [])seq.get( key ); 1340 if (i == null ) { 1341 i=new int[1]; 1342 i[0]=0; 1343 seq.put( key, i); 1344 } else { 1345 i[0]++; 1346 } 1347 return i[0]; 1348 } 1349 1350 1354 public synchronized static Registry createRegistry() { 1355 1356 if (registry == null) { 1357 registry = Registry.getRegistry(null, null); 1358 ClassLoader cl=ServerLifecycleListener.class.getClassLoader(); 1359 1360 registry.loadDescriptors("org.apache.catalina.mbeans", cl); 1361 registry.loadDescriptors("org.apache.catalina.authenticator", cl); 1362 registry.loadDescriptors("org.apache.catalina.core", cl); 1363 registry.loadDescriptors("org.apache.catalina", cl); 1364 registry.loadDescriptors("org.apache.catalina.deploy", cl); 1365 registry.loadDescriptors("org.apache.catalina.loader", cl); 1366 registry.loadDescriptors("org.apache.catalina.realm", cl); 1367 registry.loadDescriptors("org.apache.catalina.session", cl); 1368 registry.loadDescriptors("org.apache.catalina.startup", cl); 1369 registry.loadDescriptors("org.apache.catalina.users", cl); 1370 registry.loadDescriptors("org.apache.catalina.ha", cl); 1371 registry.loadDescriptors("org.apache.catalina.connector", cl); 1372 registry.loadDescriptors("org.apache.catalina.valves", cl); 1373 } 1374 return (registry); 1375 1376 } 1377 1378 1379 1384 public synchronized static MBeanServer createServer() { 1385 1386 if (mserver == null) { 1387 try { 1388 mserver = Registry.getRegistry(null, null).getMBeanServer(); 1389 } catch (Throwable t) { 1390 t.printStackTrace(System.out); 1391 System.exit(1); 1392 } 1393 } 1394 return (mserver); 1395 1396 } 1397 1398 1399 1407 static void destroyMBean(Connector connector, Service service) 1408 throws Exception { 1409 1410 connector.setService(service); 1411 String mname = createManagedName(connector); 1412 ManagedBean managed = registry.findManagedBean(mname); 1413 if (managed == null) { 1414 return; 1415 } 1416 String domain = managed.getDomain(); 1417 if (domain == null) 1418 domain = mserver.getDefaultDomain(); 1419 ObjectName oname = createObjectName(domain, connector); 1420 connector.setService(null); 1421 if( mserver.isRegistered( oname )) { 1422 mserver.unregisterMBean(oname); 1423 } 1424 } 1425 1426 1427 1435 static void destroyMBean(Context context) 1436 throws Exception { 1437 1438 String mname = createManagedName(context); 1439 ManagedBean managed = registry.findManagedBean(mname); 1440 if (managed == null) { 1441 return; 1442 } 1443 String domain = managed.getDomain(); 1444 if (domain == null) 1445 domain = mserver.getDefaultDomain(); 1446 ObjectName oname = createObjectName(domain, context); 1447 if( mserver.isRegistered(oname) ) 1448 mserver.unregisterMBean(oname); 1449 1450 } 1451 1452 1453 1461 static void destroyMBean(ContextEnvironment environment) 1462 throws Exception { 1463 1464 String mname = createManagedName(environment); 1465 ManagedBean managed = registry.findManagedBean(mname); 1466 if (managed == null) { 1467 return; 1468 } 1469 String domain = managed.getDomain(); 1470 if (domain == null) 1471 domain = mserver.getDefaultDomain(); 1472 ObjectName oname = createObjectName(domain, environment); 1473 if( mserver.isRegistered(oname) ) 1474 mserver.unregisterMBean(oname); 1475 1476 } 1477 1478 1479 1487 static void destroyMBean(ContextResource resource) 1488 throws Exception { 1489 1490 String mname = createManagedName(resource); 1491 ManagedBean managed = registry.findManagedBean(mname); 1492 if (managed == null) { 1493 return; 1494 } 1495 String domain = managed.getDomain(); 1496 if (domain == null) 1497 domain = mserver.getDefaultDomain(); 1498 ObjectName oname = createObjectName(domain, resource); 1499 if( mserver.isRegistered(oname )) 1500 mserver.unregisterMBean(oname); 1501 1502 } 1503 1504 1505 1513 static void destroyMBean(ContextResourceLink resourceLink) 1514 throws Exception { 1515 1516 String mname = createManagedName(resourceLink); 1517 ManagedBean managed = registry.findManagedBean(mname); 1518 if (managed == null) { 1519 return; 1520 } 1521 String domain = managed.getDomain(); 1522 if (domain == null) 1523 domain = mserver.getDefaultDomain(); 1524 ObjectName oname = createObjectName(domain, resourceLink); 1525 if( mserver.isRegistered(oname) ) 1526 mserver.unregisterMBean(oname); 1527 1528 } 1529 1530 1538 static void destroyMBean(Engine engine) 1539 throws Exception { 1540 1541 String mname = createManagedName(engine); 1542 ManagedBean managed = registry.findManagedBean(mname); 1543 if (managed == null) { 1544 return; 1545 } 1546 String domain = managed.getDomain(); 1547 if (domain == null) 1548 domain = mserver.getDefaultDomain(); 1549 ObjectName oname = createObjectName(domain, engine); 1550 if( mserver.isRegistered(oname) ) 1551 mserver.unregisterMBean(oname); 1552 1553 } 1554 1555 1556 1564 static void destroyMBean(Group group) 1565 throws Exception { 1566 1567 String mname = createManagedName(group); 1568 ManagedBean managed = registry.findManagedBean(mname); 1569 if (managed == null) { 1570 return; 1571 } 1572 String domain = managed.getDomain(); 1573 if (domain == null) 1574 domain = mserver.getDefaultDomain(); 1575 ObjectName oname = createObjectName(domain, group); 1576 if( mserver.isRegistered(oname) ) 1577 mserver.unregisterMBean(oname); 1578 1579 } 1580 1581 1582 1590 static void destroyMBean(Host host) 1591 throws Exception { 1592 1593 String mname = createManagedName(host); 1594 ManagedBean managed = registry.findManagedBean(mname); 1595 if (managed == null) { 1596 return; 1597 } 1598 String domain = managed.getDomain(); 1599 if (domain == null) 1600 domain = mserver.getDefaultDomain(); 1601 ObjectName oname = createObjectName(domain, host); 1602 if( mserver.isRegistered(oname) ) 1603 mserver.unregisterMBean(oname); 1604 1605 } 1606 1607 1608 1616 static void destroyMBean(Loader loader) 1617 throws Exception { 1618 1619 String mname = createManagedName(loader); 1620 ManagedBean managed = registry.findManagedBean(mname); 1621 if (managed == null) { 1622 return; 1623 } 1624 String domain = managed.getDomain(); 1625 if (domain == null) 1626 domain = mserver.getDefaultDomain(); 1627 ObjectName oname = createObjectName(domain, loader); 1628 if( mserver.isRegistered(oname) ) 1629 mserver.unregisterMBean(oname); 1630 1631 } 1632 1633 1634 1642 static void destroyMBean(Manager manager) 1643 throws Exception { 1644 1645 String mname = createManagedName(manager); 1646 ManagedBean managed = registry.findManagedBean(mname); 1647 if (managed == null) { 1648 return; 1649 } 1650 String domain = managed.getDomain(); 1651 if (domain == null) 1652 domain = mserver.getDefaultDomain(); 1653 ObjectName oname = createObjectName(domain, manager); 1654 if( mserver.isRegistered(oname) ) 1655 mserver.unregisterMBean(oname); 1656 1657 } 1658 1659 1660 1668 static void destroyMBean(NamingResources resources) 1669 throws Exception { 1670 1671 String mname = createManagedName(resources); 1672 ManagedBean managed = registry.findManagedBean(mname); 1673 if (managed == null) { 1674 return; 1675 } 1676 String domain = managed.getDomain(); 1677 if (domain == null) 1678 domain = mserver.getDefaultDomain(); 1679 ObjectName oname = createObjectName(domain, resources); 1680 if( mserver.isRegistered(oname) ) 1681 mserver.unregisterMBean(oname); 1682 1683 } 1684 1685 1686 1694 static void destroyMBean(Realm realm) 1695 throws Exception { 1696 1697 String mname = createManagedName(realm); 1698 ManagedBean managed = registry.findManagedBean(mname); 1699 if (managed == null) { 1700 return; 1701 } 1702 String domain = managed.getDomain(); 1703 if (domain == null) 1704 domain = mserver.getDefaultDomain(); 1705 ObjectName oname = createObjectName(domain, realm); 1706 if( mserver.isRegistered(oname) ) 1707 mserver.unregisterMBean(oname); 1708 1709 } 1710 1711 1712 1720 static void destroyMBean(Role role) 1721 throws Exception { 1722 1723 String mname = createManagedName(role); 1724 ManagedBean managed = registry.findManagedBean(mname); 1725 if (managed == null) { 1726 return; 1727 } 1728 String domain = managed.getDomain(); 1729 if (domain == null) 1730 domain = mserver.getDefaultDomain(); 1731 ObjectName oname = createObjectName(domain, role); 1732 if( mserver.isRegistered(oname) ) 1733 mserver.unregisterMBean(oname); 1734 1735 } 1736 1737 1738 1746 static void destroyMBean(Server server) 1747 throws Exception { 1748 1749 String mname = createManagedName(server); 1750 ManagedBean managed = registry.findManagedBean(mname); 1751 if (managed == null) { 1752 return; 1753 } 1754 String domain = managed.getDomain(); 1755 if (domain == null) 1756 domain = mserver.getDefaultDomain(); 1757 ObjectName oname = createObjectName(domain, server); 1758 if( mserver.isRegistered(oname) ) 1759 mserver.unregisterMBean(oname); 1760 1761 } 1762 1763 1764 1772 static void destroyMBean(Service service) 1773 throws Exception { 1774 1775 String mname = createManagedName(service); 1776 ManagedBean managed = registry.findManagedBean(mname); 1777 if (managed == null) { 1778 return; 1779 } 1780 String domain = managed.getDomain(); 1781 if (domain == null) 1782 domain = mserver.getDefaultDomain(); 1783 ObjectName oname = createObjectName(domain, service); 1784 if( mserver.isRegistered(oname) ) 1785 mserver.unregisterMBean(oname); 1786 1787 } 1788 1789 1790 1798 static void destroyMBean(User user) 1799 throws Exception { 1800 1801 String mname = createManagedName(user); 1802 ManagedBean managed = registry.findManagedBean(mname); 1803 if (managed == null) { 1804 return; 1805 } 1806 String domain = managed.getDomain(); 1807 if (domain == null) 1808 domain = mserver.getDefaultDomain(); 1809 ObjectName oname = createObjectName(domain, user); 1810 if( mserver.isRegistered(oname) ) 1811 mserver.unregisterMBean(oname); 1812 1813 } 1814 1815 1816 1824 static void destroyMBean(UserDatabase userDatabase) 1825 throws Exception { 1826 1827 String mname = createManagedName(userDatabase); 1828 ManagedBean managed = registry.findManagedBean(mname); 1829 if (managed == null) { 1830 return; 1831 } 1832 String domain = managed.getDomain(); 1833 if (domain == null) 1834 domain = mserver.getDefaultDomain(); 1835 ObjectName oname = createObjectName(domain, userDatabase); 1836 if( mserver.isRegistered(oname) ) 1837 mserver.unregisterMBean(oname); 1838 1839 } 1840 1841 1842 1850 static void destroyMBean(Valve valve, Container container) 1851 throws Exception { 1852 1853 ((Contained)valve).setContainer(container); 1854 String mname = createManagedName(valve); 1855 ManagedBean managed = registry.findManagedBean(mname); 1856 if (managed == null) { 1857 return; 1858 } 1859 String domain = managed.getDomain(); 1860 if (domain == null) 1861 domain = mserver.getDefaultDomain(); 1862 ObjectName oname = createObjectName(domain, valve); 1863 try { 1864 ((Contained)valve).setContainer(null); 1865 } catch (Throwable t) { 1866 ; 1867 } 1868 if( mserver.isRegistered(oname) ) { 1869 mserver.unregisterMBean(oname); 1870 } 1871 1872 } 1873 1874} 1875 | Popular Tags |