1 17 18 package org.apache.catalina.mbeans; 19 20 import java.io.File ; 21 import java.util.Vector ; 22 23 import javax.management.MBeanException ; 24 import javax.management.MBeanServer ; 25 import javax.management.ObjectName ; 26 import javax.management.RuntimeOperationsException ; 27 28 import org.apache.catalina.Context; 29 import org.apache.catalina.Engine; 30 import org.apache.catalina.Host; 31 import org.apache.catalina.Server; 32 import org.apache.catalina.ServerFactory; 33 import org.apache.catalina.Service; 34 import org.apache.catalina.Valve; 35 import org.apache.catalina.authenticator.SingleSignOn; 36 import org.apache.catalina.connector.Connector; 37 import org.apache.catalina.core.ContainerBase; 38 import org.apache.catalina.core.StandardContext; 39 import org.apache.catalina.core.StandardEngine; 40 import org.apache.catalina.core.StandardHost; 41 import org.apache.catalina.core.StandardService; 42 import org.apache.catalina.loader.WebappLoader; 43 import org.apache.catalina.realm.DataSourceRealm; 44 import org.apache.catalina.realm.JDBCRealm; 45 import org.apache.catalina.realm.JNDIRealm; 46 import org.apache.catalina.realm.MemoryRealm; 47 import org.apache.catalina.realm.UserDatabaseRealm; 48 import org.apache.catalina.session.StandardManager; 49 import org.apache.catalina.startup.ContextConfig; 50 import org.apache.catalina.startup.HostConfig; 51 import org.apache.catalina.valves.AccessLogValve; 52 import org.apache.catalina.valves.RemoteAddrValve; 53 import org.apache.catalina.valves.RemoteHostValve; 54 import org.apache.catalina.valves.RequestDumperValve; 55 import org.apache.catalina.valves.ValveBase; 56 import org.apache.tomcat.util.modeler.BaseModelMBean; 57 import org.apache.tomcat.util.modeler.Registry; 58 59 60 67 68 public class MBeanFactory extends BaseModelMBean { 69 70 private static org.apache.commons.logging.Log log = 71 org.apache.commons.logging.LogFactory.getLog(MBeanFactory.class); 72 73 76 private static MBeanServer mserver = MBeanUtils.createServer(); 77 78 81 private static Registry registry = MBeanUtils.createRegistry(); 82 83 84 86 87 96 public MBeanFactory() 97 throws MBeanException , RuntimeOperationsException { 98 99 super(); 100 101 } 102 103 104 106 107 108 109 111 112 117 public String findObjectName(String type) { 118 119 if (type.equals("org.apache.catalina.core.StandardContext")) { 120 return "StandardContext"; 121 } else if (type.equals("org.apache.catalina.core.StandardEngine")) { 122 return "Engine"; 123 } else if (type.equals("org.apache.catalina.core.StandardHost")) { 124 return "Host"; 125 } else { 126 return null; 127 } 128 129 } 130 131 132 139 private final String getPathStr(String t) { 140 if (t == null || t.equals("/")) { 141 return ""; 142 } 143 return t; 144 } 145 146 150 private ContainerBase getParentContainerFromParent(ObjectName pname) 151 throws Exception { 152 153 String type = pname.getKeyProperty("type"); 154 String j2eeType = pname.getKeyProperty("j2eeType"); 155 Service service = getService(pname); 156 StandardEngine engine = (StandardEngine) service.getContainer(); 157 if ((j2eeType!=null) && (j2eeType.equals("WebModule"))) { 158 String name = pname.getKeyProperty("name"); 159 name = name.substring(2); 160 int i = name.indexOf("/"); 161 String hostName = name.substring(0,i); 162 String path = name.substring(i); 163 Host host = (Host) engine.findChild(hostName); 164 String pathStr = getPathStr(path); 165 StandardContext context = (StandardContext)host.findChild(pathStr); 166 return context; 167 } else if (type != null) { 168 if (type.equals("Engine")) { 169 return engine; 170 } else if (type.equals("Host")) { 171 String hostName = pname.getKeyProperty("host"); 172 StandardHost host = (StandardHost) engine.findChild(hostName); 173 return host; 174 } 175 } 176 return null; 177 178 } 179 180 181 185 private ContainerBase getParentContainerFromChild(ObjectName oname) 186 throws Exception { 187 188 String hostName = oname.getKeyProperty("host"); 189 String path = oname.getKeyProperty("path"); 190 Service service = getService(oname); 191 StandardEngine engine = (StandardEngine) service.getContainer(); 192 if (hostName == null) { 193 return engine; 195 } else if (path == null) { 196 StandardHost host = (StandardHost) engine.findChild(hostName); 198 return host; 199 } else { 200 StandardHost host = (StandardHost) engine.findChild(hostName); 202 path = getPathStr(path); 203 StandardContext context = (StandardContext) host.findChild(path); 204 return context; 205 } 206 } 207 208 209 private Service getService(ObjectName oname) throws Exception { 210 211 String domain = oname.getDomain(); 212 Server server = ServerFactory.getServer(); 213 Service[] services = server.findServices(); 214 StandardService service = null; 215 for (int i = 0; i < services.length; i++) { 216 service = (StandardService) services[i]; 217 if (domain.equals(service.getObjectName().getDomain())) { 218 break; 219 } 220 } 221 if (!service.getObjectName().getDomain().equals(domain)) { 222 throw new Exception ("Service with the domain is not found"); 223 } 224 return service; 225 226 } 227 228 229 236 public String createAccessLoggerValve(String parent) 237 throws Exception { 238 239 ObjectName pname = new ObjectName (parent); 240 AccessLogValve accessLogger = new AccessLogValve(); 242 ContainerBase containerBase = getParentContainerFromParent(pname); 243 containerBase.addValve(accessLogger); 245 ObjectName oname = accessLogger.getObjectName(); 246 return (oname.toString()); 247 248 } 249 250 251 260 public String createAjpConnector(String parent, String address, int port) 261 throws Exception { 262 263 return createConnector(parent, address, port, true, false); 264 } 265 266 273 public String createDataSourceRealm(String parent, String dataSourceName, 274 String roleNameCol, String userCredCol, String userNameCol, 275 String userRoleTable, String userTable) throws Exception { 276 277 DataSourceRealm realm = new DataSourceRealm(); 279 realm.setDataSourceName(dataSourceName); 280 realm.setRoleNameCol(roleNameCol); 281 realm.setUserCredCol(userCredCol); 282 realm.setUserNameCol(userNameCol); 283 realm.setUserRoleTable(userRoleTable); 284 realm.setUserTable(userTable); 285 286 ObjectName pname = new ObjectName (parent); 288 ContainerBase containerBase = getParentContainerFromParent(pname); 289 containerBase.setRealm(realm); 291 ObjectName oname = realm.getObjectName(); 293 if (oname != null) { 294 return (oname.toString()); 295 } else { 296 return null; 297 } 298 299 } 300 301 310 public String createHttpConnector(String parent, String address, int port) 311 throws Exception { 312 return createConnector(parent, address, port, false, false); 313 } 314 315 326 private String createConnector(String parent, String address, int port, boolean isAjp, boolean isSSL) 327 throws Exception { 328 Connector retobj = new Connector(); 329 if ((address!=null) && (address.length()>0)) { 330 retobj.setProperty("address", address); 331 } 332 retobj.setPort(port); 334 retobj.setProtocol(isAjp ? "AJP/1.3" : "HTTP/1.1"); 336 retobj.setSecure(isSSL); 338 retobj.setScheme(isSSL ? "https" : "http"); 339 ObjectName pname = new ObjectName (parent); 342 Service service = getService(pname); 343 service.addConnector(retobj); 344 345 ObjectName coname = retobj.getObjectName(); 347 348 return (coname.toString()); 349 } 350 351 352 361 public String createHttpsConnector(String parent, String address, int port) 362 throws Exception { 363 return createConnector(parent, address, port, false, true); 364 } 365 366 373 public String createJDBCRealm(String parent, String driverName, 374 String connectionName, String connectionPassword, String connectionURL) 375 throws Exception { 376 377 JDBCRealm realm = new JDBCRealm(); 379 realm.setDriverName(driverName); 380 realm.setConnectionName(connectionName); 381 realm.setConnectionPassword(connectionPassword); 382 realm.setConnectionURL(connectionURL); 383 384 ObjectName pname = new ObjectName (parent); 386 ContainerBase containerBase = getParentContainerFromParent(pname); 387 containerBase.setRealm(realm); 389 ObjectName oname = realm.getObjectName(); 391 392 if (oname != null) { 393 return (oname.toString()); 394 } else { 395 return null; 396 } 397 398 } 399 400 401 408 public String createJNDIRealm(String parent) 409 throws Exception { 410 411 JNDIRealm realm = new JNDIRealm(); 413 414 ObjectName pname = new ObjectName (parent); 416 ContainerBase containerBase = getParentContainerFromParent(pname); 417 containerBase.setRealm(realm); 419 ObjectName oname = realm.getObjectName(); 421 422 if (oname != null) { 423 return (oname.toString()); 424 } else { 425 return null; 426 } 427 428 429 } 430 431 432 439 public String createMemoryRealm(String parent) 440 throws Exception { 441 442 MemoryRealm realm = new MemoryRealm(); 444 445 ObjectName pname = new ObjectName (parent); 447 ContainerBase containerBase = getParentContainerFromParent(pname); 448 containerBase.setRealm(realm); 450 ObjectName oname = realm.getObjectName(); 452 if (oname != null) { 453 return (oname.toString()); 454 } else { 455 return null; 456 } 457 458 } 459 460 461 468 public String createRemoteAddrValve(String parent) 469 throws Exception { 470 471 RemoteAddrValve valve = new RemoteAddrValve(); 473 474 ObjectName pname = new ObjectName (parent); 476 ContainerBase containerBase = getParentContainerFromParent(pname); 477 containerBase.addValve(valve); 478 ObjectName oname = valve.getObjectName(); 479 return (oname.toString()); 480 481 } 482 483 484 491 public String createRemoteHostValve(String parent) 492 throws Exception { 493 494 RemoteHostValve valve = new RemoteHostValve(); 496 497 ObjectName pname = new ObjectName (parent); 499 ContainerBase containerBase = getParentContainerFromParent(pname); 500 containerBase.addValve(valve); 501 ObjectName oname = valve.getObjectName(); 502 return (oname.toString()); 503 504 } 505 506 507 514 public String createRequestDumperValve(String parent) 515 throws Exception { 516 517 RequestDumperValve valve = new RequestDumperValve(); 519 520 ObjectName pname = new ObjectName (parent); 522 ContainerBase containerBase = getParentContainerFromParent(pname); 523 containerBase.addValve(valve); 524 ObjectName oname = valve.getObjectName(); 525 return (oname.toString()); 526 527 } 528 529 530 537 public String createSingleSignOn(String parent) 538 throws Exception { 539 540 SingleSignOn valve = new SingleSignOn(); 542 543 ObjectName pname = new ObjectName (parent); 545 ContainerBase containerBase = getParentContainerFromParent(pname); 546 containerBase.addValve(valve); 547 ObjectName oname = valve.getObjectName(); 548 return (oname.toString()); 549 550 } 551 552 553 562 public String createStandardContext(String parent, 563 String path, 564 String docBase) 565 throws Exception { 566 567 return 569 createStandardContext(parent,path,docBase,false,false,false,false); 570 } 571 572 575 private String getConfigFile(String path) { 576 String basename = null; 577 if (path.equals("")) { 578 basename = "ROOT"; 579 } else { 580 basename = path.substring(1).replace('/', '#'); 581 } 582 return (basename); 583 } 584 585 594 public String createStandardContext(String parent, 595 String path, 596 String docBase, 597 boolean xmlValidation, 598 boolean xmlNamespaceAware, 599 boolean tldValidation, 600 boolean tldNamespaceAware) 601 throws Exception { 602 603 StandardContext context = new StandardContext(); 605 path = getPathStr(path); 606 context.setPath(path); 607 context.setDocBase(docBase); 608 context.setXmlValidation(xmlValidation); 609 context.setXmlNamespaceAware(xmlNamespaceAware); 610 context.setTldValidation(tldValidation); 611 context.setTldNamespaceAware(tldNamespaceAware); 612 613 ContextConfig contextConfig = new ContextConfig(); 614 context.addLifecycleListener(contextConfig); 615 616 ObjectName pname = new ObjectName (parent); 618 ObjectName deployer = new ObjectName (pname.getDomain()+ 619 ":type=Deployer,host="+ 620 pname.getKeyProperty("host")); 621 if(mserver.isRegistered(deployer)) { 622 String contextPath = context.getPath(); 623 mserver.invoke(deployer, "addServiced", 624 new Object [] {contextPath}, 625 new String [] {"java.lang.String"}); 626 String configPath = (String )mserver.getAttribute(deployer, 627 "configBaseName"); 628 String baseName = getConfigFile(contextPath); 629 File configFile = new File (new File (configPath), baseName+".xml"); 630 context.setConfigFile(configFile.getAbsolutePath()); 631 mserver.invoke(deployer, "manageApp", 632 new Object [] {context}, 633 new String [] {"org.apache.catalina.Context"}); 634 mserver.invoke(deployer, "removeServiced", 635 new Object [] {contextPath}, 636 new String [] {"java.lang.String"}); 637 } else { 638 log.warn("Deployer not found for "+pname.getKeyProperty("host")); 639 Service service = getService(pname); 640 Engine engine = (Engine) service.getContainer(); 641 Host host = (Host) engine.findChild(pname.getKeyProperty("host")); 642 host.addChild(context); 643 } 644 645 ObjectName oname = context.getJmxName(); 647 648 return (oname.toString()); 649 650 } 651 652 653 663 664 public Vector createStandardEngineService(String parent, 665 String engineName, String defaultHost, String serviceName) 666 throws Exception { 667 668 StandardService service = new StandardService(); 670 service.setName(serviceName); 671 StandardEngine engine = new StandardEngine(); 673 engine.setName(engineName); 674 engine.setDefaultHost(defaultHost); 675 service.setContainer(engine); 677 Server server = ServerFactory.getServer(); 679 server.addService(service); 680 Vector onames = new Vector (); 681 ObjectName oname = 684 MBeanUtils.createObjectName(engineName, engine); 685 onames.add(0, oname); 686 oname = 688 MBeanUtils.createObjectName(engineName, service); 689 onames.add(1, oname); 690 return (onames); 691 692 } 693 694 695 710 public String createStandardHost(String parent, String name, 711 String appBase, 712 boolean autoDeploy, 713 boolean deployOnStartup, 714 boolean deployXML, 715 boolean unpackWARs, 716 boolean xmlNamespaceAware, 717 boolean xmlValidation) 718 throws Exception { 719 720 StandardHost host = new StandardHost(); 722 host.setName(name); 723 host.setAppBase(appBase); 724 host.setAutoDeploy(autoDeploy); 725 host.setDeployOnStartup(deployOnStartup); 726 host.setDeployXML(deployXML); 727 host.setUnpackWARs(unpackWARs); 728 host.setXmlNamespaceAware(xmlNamespaceAware); 729 host.setXmlValidation(xmlValidation); 730 731 HostConfig hostConfig = new HostConfig(); 733 host.addLifecycleListener(hostConfig); 734 735 ObjectName pname = new ObjectName (parent); 737 Service service = getService(pname); 738 Engine engine = (Engine) service.getContainer(); 739 engine.addChild(host); 740 741 return (host.getObjectName().toString()); 743 744 } 745 746 747 754 public String createStandardManager(String parent) 755 throws Exception { 756 757 StandardManager manager = new StandardManager(); 759 760 ObjectName pname = new ObjectName (parent); 762 ContainerBase containerBase = getParentContainerFromParent(pname); 763 if (containerBase != null) { 764 containerBase.setManager(manager); 765 } 766 ObjectName oname = manager.getObjectName(); 767 if (oname != null) { 768 return (oname.toString()); 769 } else { 770 return null; 771 } 772 773 } 774 775 776 784 public String createStandardService(String parent, String name, String domain) 785 throws Exception { 786 787 StandardService service = new StandardService(); 789 service.setName(name); 790 791 Server server = ServerFactory.getServer(); 793 server.addService(service); 794 795 return (service.getObjectName().toString()); 797 798 } 799 800 801 802 811 public String createUserDatabaseRealm(String parent, String resourceName) 812 throws Exception { 813 814 UserDatabaseRealm realm = new UserDatabaseRealm(); 816 realm.setResourceName(resourceName); 817 818 ObjectName pname = new ObjectName (parent); 820 ContainerBase containerBase = getParentContainerFromParent(pname); 821 containerBase.setRealm(realm); 823 ObjectName oname = realm.getObjectName(); 825 if (oname != null) { 829 return (oname.toString()); 830 } else { 831 return null; 832 } 833 834 } 835 836 837 844 public String createWebappLoader(String parent) 845 throws Exception { 846 847 WebappLoader loader = new WebappLoader(); 849 850 ObjectName pname = new ObjectName (parent); 852 ContainerBase containerBase = getParentContainerFromParent(pname); 853 if (containerBase != null) { 854 containerBase.setLoader(loader); 855 } 856 ObjectName oname = 859 MBeanUtils.createObjectName(pname.getDomain(), loader); 860 return (oname.toString()); 861 862 } 863 864 865 872 public void removeConnector(String name) throws Exception { 873 874 ObjectName oname = new ObjectName (name); 876 Server server = ServerFactory.getServer(); 877 Service service = getService(oname); 878 String port = oname.getKeyProperty("port"); 879 881 Connector conns[] = (Connector[]) service.findConnectors(); 882 883 for (int i = 0; i < conns.length; i++) { 884 String connAddress = String.valueOf(conns[i].getProperty("address")); 885 String connPort = ""+conns[i].getPort(); 886 887 if ((connAddress==null) && port.equals(connPort)) { 889 service.removeConnector(conns[i]); 890 conns[i].destroy(); 891 break; 892 } 893 if (port.equals(connPort)) { 895 service.removeConnector(conns[i]); 897 conns[i].destroy(); 898 break; 899 } 900 } 901 902 } 903 904 905 912 public void removeContext(String contextName) throws Exception { 913 914 ObjectName oname = new ObjectName (contextName); 916 String domain = oname.getDomain(); 917 StandardService service = (StandardService) getService(oname); 918 919 Engine engine = (Engine) service.getContainer(); 920 String name = oname.getKeyProperty("name"); 921 name = name.substring(2); 922 int i = name.indexOf("/"); 923 String hostName = name.substring(0,i); 924 String path = name.substring(i); 925 ObjectName deployer = new ObjectName (domain+":type=Deployer,host="+ 926 hostName); 927 String pathStr = getPathStr(path); 928 if(mserver.isRegistered(deployer)) { 929 mserver.invoke(deployer,"addServiced", 930 new Object []{pathStr}, 931 new String [] {"java.lang.String"}); 932 mserver.invoke(deployer,"unmanageApp", 933 new Object [] {pathStr}, 934 new String [] {"java.lang.String"}); 935 mserver.invoke(deployer,"removeServiced", 936 new Object [] {pathStr}, 937 new String [] {"java.lang.String"}); 938 } else { 939 log.warn("Deployer not found for "+hostName); 940 Host host = (Host) engine.findChild(hostName); 941 Context context = (Context) host.findChild(pathStr); 942 host.removeChild(context); 944 if(context instanceof StandardContext) 945 try { 946 ((StandardContext)context).destroy(); 947 } catch (Exception e) { 948 log.warn("Error during context [" + context.getName() + "] destroy ", e); 949 } 950 951 } 952 953 } 954 955 956 963 public void removeHost(String name) throws Exception { 964 965 ObjectName oname = new ObjectName (name); 967 String hostName = oname.getKeyProperty("host"); 968 Service service = getService(oname); 969 Engine engine = (Engine) service.getContainer(); 970 Host host = (Host) engine.findChild(hostName); 971 972 if(host!=null) { 974 if(host instanceof StandardHost) 975 ((StandardHost)host).destroy(); 976 else 977 engine.removeChild(host); 978 } 979 980 } 981 982 983 990 public void removeLoader(String name) throws Exception { 991 992 ObjectName oname = new ObjectName (name); 993 ContainerBase container = getParentContainerFromChild(oname); 995 container.setLoader(null); 996 997 } 998 999 1000 1007 public void removeManager(String name) throws Exception { 1008 1009 ObjectName oname = new ObjectName (name); 1010 ContainerBase container = getParentContainerFromChild(oname); 1012 container.setManager(null); 1013 1014 } 1015 1016 1017 1024 public void removeRealm(String name) throws Exception { 1025 1026 ObjectName oname = new ObjectName (name); 1027 ContainerBase container = getParentContainerFromChild(oname); 1029 container.setRealm(null); 1030 } 1031 1032 1033 1040 public void removeService(String name) throws Exception { 1041 1042 ObjectName oname = new ObjectName (name); 1044 String serviceName = oname.getKeyProperty("serviceName"); 1045 Server server = ServerFactory.getServer(); 1046 Service service = server.findService(serviceName); 1047 1048 server.removeService(service); 1050 1051 } 1052 1053 1054 1061 public void removeValve(String name) throws Exception { 1062 1063 ObjectName oname = new ObjectName (name); 1065 ContainerBase container = getParentContainerFromChild(oname); 1066 String sequence = oname.getKeyProperty("seq"); 1067 Valve[] valves = (Valve[])container.getValves(); 1068 for (int i = 0; i < valves.length; i++) { 1069 ObjectName voname = ((ValveBase) valves[i]).getObjectName(); 1070 if (voname.equals(oname)) { 1071 container.removeValve(valves[i]); 1072 } 1073 } 1074 } 1075 1076} 1077 1078 | Popular Tags |