1 17 18 19 package org.apache.catalina.connector; 20 21 import java.lang.reflect.Method ; 22 import java.net.URLEncoder ; 23 import java.util.HashMap ; 24 25 import javax.management.MBeanRegistration ; 26 import javax.management.MBeanServer ; 27 import javax.management.MalformedObjectNameException ; 28 import javax.management.ObjectName ; 29 30 import org.apache.catalina.Container; 31 import org.apache.catalina.Lifecycle; 32 import org.apache.catalina.LifecycleException; 33 import org.apache.catalina.LifecycleListener; 34 import org.apache.catalina.Service; 35 import org.apache.catalina.core.StandardEngine; 36 import org.apache.catalina.util.LifecycleSupport; 37 import org.apache.catalina.util.StringManager; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 import org.apache.coyote.Adapter; 41 import org.apache.coyote.ProtocolHandler; 42 import org.apache.tomcat.util.IntrospectionUtils; 43 import org.apache.tomcat.util.http.mapper.Mapper; 44 import org.apache.tomcat.util.modeler.Registry; 45 46 47 54 55 56 public class Connector 57 implements Lifecycle, MBeanRegistration 58 { 59 private static Log log = LogFactory.getLog(Connector.class); 60 61 62 65 public static final boolean RECYCLE_FACADES = 66 Boolean.valueOf(System.getProperty("org.apache.catalina.connector.RECYCLE_FACADES", "false")).booleanValue(); 67 68 69 71 72 public Connector() 73 throws Exception { 74 this(null); 75 } 76 77 public Connector(String protocol) 78 throws Exception { 79 setProtocol(protocol); 80 try { 82 Class clazz = Class.forName(protocolHandlerClassName); 83 this.protocolHandler = (ProtocolHandler) clazz.newInstance(); 84 } catch (Exception e) { 85 log.error 86 (sm.getString 87 ("coyoteConnector.protocolHandlerInstantiationFailed", e)); 88 } 89 } 90 91 92 94 95 98 protected Service service = null; 99 100 101 104 protected boolean allowTrace = false; 105 106 107 110 protected Container container = null; 111 112 113 116 protected boolean emptySessionPath = false; 117 118 119 122 protected boolean enableLookups = false; 123 124 125 128 protected boolean xpoweredBy = false; 129 130 131 134 protected static final String info = 135 "org.apache.catalina.connector.Connector/2.1"; 136 137 138 141 protected LifecycleSupport lifecycle = new LifecycleSupport(this); 142 143 144 147 protected int port = 0; 148 149 150 156 protected String proxyName = null; 157 158 159 165 protected int proxyPort = 0; 166 167 168 171 protected int redirectPort = 443; 172 173 174 178 protected String scheme = "http"; 179 180 181 185 protected boolean secure = false; 186 187 188 191 protected StringManager sm = 192 StringManager.getManager(Constants.Package); 193 194 195 199 protected int maxPostSize = 2 * 1024 * 1024; 200 201 202 206 protected int maxSavePostSize = 4 * 1024; 207 208 209 212 protected boolean initialized = false; 213 214 215 218 protected boolean started = false; 219 220 221 224 protected boolean stopped = false; 225 226 229 protected boolean useIPVHosts = false; 230 231 234 protected Thread thread = null; 235 236 237 241 protected String protocolHandlerClassName = 242 "org.apache.coyote.http11.Http11Protocol"; 243 244 245 248 protected ProtocolHandler protocolHandler = null; 249 250 251 254 protected Adapter adapter = null; 255 256 257 260 protected Mapper mapper = new Mapper(); 261 262 263 266 protected MapperListener mapperListener = new MapperListener(mapper); 267 268 269 272 protected String URIEncoding = null; 273 274 275 278 protected boolean useBodyEncodingForURI = false; 279 280 281 protected static HashMap replacements = new HashMap (); 282 static { 283 replacements.put("acceptCount", "backlog"); 284 replacements.put("connectionLinger", "soLinger"); 285 replacements.put("connectionTimeout", "soTimeout"); 286 replacements.put("connectionUploadTimeout", "timeout"); 287 replacements.put("clientAuth", "clientauth"); 288 replacements.put("keystoreFile", "keystore"); 289 replacements.put("randomFile", "randomfile"); 290 replacements.put("rootFile", "rootfile"); 291 replacements.put("keystorePass", "keypass"); 292 replacements.put("keystoreType", "keytype"); 293 replacements.put("sslProtocol", "protocol"); 294 replacements.put("sslProtocols", "protocols"); 295 } 296 297 298 300 301 304 public Object getProperty(String name) { 305 String repl = name; 306 if (replacements.get(name) != null) { 307 repl = (String ) replacements.get(name); 308 } 309 return IntrospectionUtils.getProperty(protocolHandler, repl); 310 } 311 312 313 316 public void setProperty(String name, String value) { 317 String repl = name; 318 if (replacements.get(name) != null) { 319 repl = (String ) replacements.get(name); 320 } 321 IntrospectionUtils.setProperty(protocolHandler, repl, value); 322 } 323 324 325 328 public Object getAttribute(String name) { 329 return getProperty(name); 330 } 331 332 333 336 public void setAttribute(String name, Object value) { 337 setProperty(name, String.valueOf(value)); 338 } 339 340 341 344 public void removeProperty(String name) { 345 } 348 349 350 353 public Service getService() { 354 355 return (this.service); 356 357 } 358 359 360 365 public void setService(Service service) { 366 367 this.service = service; 368 370 } 371 372 373 376 public boolean getAllowTrace() { 377 378 return (this.allowTrace); 379 380 } 381 382 383 388 public void setAllowTrace(boolean allowTrace) { 389 390 this.allowTrace = allowTrace; 391 setProperty("allowTrace", String.valueOf(allowTrace)); 392 393 } 394 395 398 public boolean isAvailable() { 399 400 return (started); 401 402 } 403 404 405 410 public int getBufferSize() { 411 return 2048; 412 } 413 414 420 public void setBufferSize(int bufferSize) { 421 } 422 423 424 428 public Container getContainer() { 429 if( container==null ) { 430 findContainer(); 432 } 433 return (container); 434 435 } 436 437 438 444 public void setContainer(Container container) { 445 446 this.container = container; 447 448 } 449 450 451 454 public boolean getEmptySessionPath() { 455 456 return (this.emptySessionPath); 457 458 } 459 460 461 466 public void setEmptySessionPath(boolean emptySessionPath) { 467 468 this.emptySessionPath = emptySessionPath; 469 setProperty("emptySessionPath", String.valueOf(emptySessionPath)); 470 471 } 472 473 474 477 public boolean getEnableLookups() { 478 479 return (this.enableLookups); 480 481 } 482 483 484 489 public void setEnableLookups(boolean enableLookups) { 490 491 this.enableLookups = enableLookups; 492 setProperty("enableLookups", String.valueOf(enableLookups)); 493 494 } 495 496 497 500 public String getInfo() { 501 502 return (info); 503 504 } 505 506 507 510 public Mapper getMapper() { 511 512 return (mapper); 513 514 } 515 516 517 521 public int getMaxPostSize() { 522 523 return (maxPostSize); 524 525 } 526 527 528 535 public void setMaxPostSize(int maxPostSize) { 536 537 this.maxPostSize = maxPostSize; 538 } 539 540 541 545 public int getMaxSavePostSize() { 546 547 return (maxSavePostSize); 548 549 } 550 551 552 559 public void setMaxSavePostSize(int maxSavePostSize) { 560 561 this.maxSavePostSize = maxSavePostSize; 562 setProperty("maxSavePostSize", String.valueOf(maxSavePostSize)); 563 } 564 565 566 569 public int getPort() { 570 571 return (this.port); 572 573 } 574 575 576 581 public void setPort(int port) { 582 583 this.port = port; 584 setProperty("port", String.valueOf(port)); 585 586 } 587 588 589 592 public String getProtocol() { 593 594 if ("org.apache.coyote.http11.Http11Protocol".equals 595 (getProtocolHandlerClassName()) 596 || "org.apache.coyote.http11.Http11AprProtocol".equals 597 (getProtocolHandlerClassName())) { 598 return "HTTP/1.1"; 599 } else if ("org.apache.coyote.ajp.AjpProtocol".equals 600 (getProtocolHandlerClassName()) 601 || "org.apache.coyote.ajp.AjpAprProtocol".equals 602 (getProtocolHandlerClassName())) { 603 return "AJP/1.3"; 604 } 605 return getProtocolHandlerClassName(); 606 607 } 608 609 610 615 public void setProtocol(String protocol) { 616 617 boolean apr = false; 619 try { 620 String methodName = "initialize"; 621 Class paramTypes[] = new Class [1]; 622 paramTypes[0] = String .class; 623 Object paramValues[] = new Object [1]; 624 paramValues[0] = null; 625 Method method = Class.forName("org.apache.tomcat.jni.Library") 626 .getMethod(methodName, paramTypes); 627 method.invoke(null, paramValues); 628 apr = true; 629 } catch (Throwable t) { 630 } 632 633 if (apr) { 634 if ("HTTP/1.1".equals(protocol)) { 635 setProtocolHandlerClassName 636 ("org.apache.coyote.http11.Http11AprProtocol"); 637 } else if ("AJP/1.3".equals(protocol)) { 638 setProtocolHandlerClassName 639 ("org.apache.coyote.ajp.AjpAprProtocol"); 640 } else if (protocol != null) { 641 setProtocolHandlerClassName(protocol); 642 } else { 643 setProtocolHandlerClassName 644 ("org.apache.coyote.http11.Http11AprProtocol"); 645 } 646 } else { 647 if ("HTTP/1.1".equals(protocol)) { 648 setProtocolHandlerClassName 649 ("org.apache.coyote.http11.Http11Protocol"); 650 } else if ("AJP/1.3".equals(protocol)) { 651 setProtocolHandlerClassName 652 ("org.apache.coyote.ajp.AjpProtocol"); 653 } else if (protocol != null) { 654 setProtocolHandlerClassName(protocol); 655 } 656 } 657 658 } 659 660 661 664 public String getProtocolHandlerClassName() { 665 666 return (this.protocolHandlerClassName); 667 668 } 669 670 671 677 public void setProtocolHandlerClassName(String protocolHandlerClassName) { 678 679 this.protocolHandlerClassName = protocolHandlerClassName; 680 681 } 682 683 684 687 public ProtocolHandler getProtocolHandler() { 688 689 return (this.protocolHandler); 690 691 } 692 693 694 697 public String getProxyName() { 698 699 return (this.proxyName); 700 701 } 702 703 704 709 public void setProxyName(String proxyName) { 710 711 if(proxyName != null && proxyName.length() > 0) { 712 this.proxyName = proxyName; 713 setProperty("proxyName", proxyName); 714 } else { 715 this.proxyName = null; 716 removeProperty("proxyName"); 717 } 718 719 } 720 721 722 725 public int getProxyPort() { 726 727 return (this.proxyPort); 728 729 } 730 731 732 737 public void setProxyPort(int proxyPort) { 738 739 this.proxyPort = proxyPort; 740 setProperty("proxyPort", String.valueOf(proxyPort)); 741 742 } 743 744 745 750 public int getRedirectPort() { 751 752 return (this.redirectPort); 753 754 } 755 756 757 762 public void setRedirectPort(int redirectPort) { 763 764 this.redirectPort = redirectPort; 765 setProperty("redirectPort", String.valueOf(redirectPort)); 766 767 } 768 769 770 774 public String getScheme() { 775 776 return (this.scheme); 777 778 } 779 780 781 787 public void setScheme(String scheme) { 788 789 this.scheme = scheme; 790 791 } 792 793 794 798 public boolean getSecure() { 799 800 return (this.secure); 801 802 } 803 804 805 811 public void setSecure(boolean secure) { 812 813 this.secure = secure; 814 setProperty("secure", Boolean.toString(secure)); 815 } 816 817 820 public String getURIEncoding() { 821 822 return (this.URIEncoding); 823 824 } 825 826 827 832 public void setURIEncoding(String URIEncoding) { 833 834 this.URIEncoding = URIEncoding; 835 setProperty("uRIEncoding", URIEncoding); 836 837 } 838 839 840 843 public boolean getUseBodyEncodingForURI() { 844 845 return (this.useBodyEncodingForURI); 846 847 } 848 849 850 855 public void setUseBodyEncodingForURI(boolean useBodyEncodingForURI) { 856 857 this.useBodyEncodingForURI = useBodyEncodingForURI; 858 setProperty 859 ("useBodyEncodingForURI", String.valueOf(useBodyEncodingForURI)); 860 861 } 862 863 864 871 public boolean getXpoweredBy() { 872 return xpoweredBy; 873 } 874 875 876 884 public void setXpoweredBy(boolean xpoweredBy) { 885 this.xpoweredBy = xpoweredBy; 886 setProperty("xpoweredBy", String.valueOf(xpoweredBy)); 887 } 888 889 895 public void setUseIPVHosts(boolean useIPVHosts) { 896 this.useIPVHosts = useIPVHosts; 897 setProperty("useIPVHosts", String.valueOf(useIPVHosts)); 898 } 899 900 903 public boolean getUseIPVHosts() { 904 return useIPVHosts; 905 } 906 907 909 910 914 public Request createRequest() { 915 916 Request request = new Request(); 917 request.setConnector(this); 918 return (request); 919 920 } 921 922 923 927 public Response createResponse() { 928 929 Response response = new Response(); 930 response.setConnector(this); 931 return (response); 932 933 } 934 935 936 938 939 944 public void addLifecycleListener(LifecycleListener listener) { 945 946 lifecycle.addLifecycleListener(listener); 947 948 } 949 950 951 955 public LifecycleListener[] findLifecycleListeners() { 956 957 return lifecycle.findLifecycleListeners(); 958 959 } 960 961 962 967 public void removeLifecycleListener(LifecycleListener listener) { 968 969 lifecycle.removeLifecycleListener(listener); 970 971 } 972 973 974 protected ObjectName createObjectName(String domain, String type) 975 throws MalformedObjectNameException { 976 String encodedAddr = null; 977 if (getProperty("address") != null) { 978 encodedAddr = URLEncoder.encode(getProperty("address").toString()); 979 } 980 String addSuffix = (getProperty("address") == null) ? "" : ",address=" 981 + encodedAddr; 982 ObjectName _oname = new ObjectName (domain + ":type=" + type + ",port=" 983 + getPort() + addSuffix); 984 return _oname; 985 } 986 987 990 public void initialize() 991 throws LifecycleException 992 { 993 if (initialized) { 994 if(log.isInfoEnabled()) 995 log.info(sm.getString("coyoteConnector.alreadyInitialized")); 996 return; 997 } 998 999 this.initialized = true; 1000 1001 if( oname == null && (container instanceof StandardEngine)) { 1002 try { 1003 StandardEngine cb=(StandardEngine)container; 1005 oname = createObjectName(cb.getName(), "Connector"); 1006 Registry.getRegistry(null, null) 1007 .registerComponent(this, oname, null); 1008 controller=oname; 1009 } catch (Exception e) { 1010 log.error( "Error registering connector ", e); 1011 } 1012 if(log.isDebugEnabled()) 1013 log.debug("Creating name for connector " + oname); 1014 } 1015 1016 adapter = new CoyoteAdapter(this); 1018 protocolHandler.setAdapter(adapter); 1019 1020 IntrospectionUtils.setProperty(protocolHandler, "jkHome", 1021 System.getProperty("catalina.base")); 1022 1023 try { 1024 protocolHandler.init(); 1025 } catch (Exception e) { 1026 throw new LifecycleException 1027 (sm.getString 1028 ("coyoteConnector.protocolHandlerInitializationFailed", e)); 1029 } 1030 } 1031 1032 1033 1036 public void pause() 1037 throws LifecycleException { 1038 try { 1039 protocolHandler.pause(); 1040 } catch (Exception e) { 1041 log.error(sm.getString 1042 ("coyoteConnector.protocolHandlerPauseFailed"), e); 1043 } 1044 } 1045 1046 1047 1050 public void resume() 1051 throws LifecycleException { 1052 try { 1053 protocolHandler.resume(); 1054 } catch (Exception e) { 1055 log.error(sm.getString 1056 ("coyoteConnector.protocolHandlerResumeFailed"), e); 1057 } 1058 } 1059 1060 1061 1066 public void start() throws LifecycleException { 1067 if( !initialized ) 1068 initialize(); 1069 1070 if (started ) { 1072 if(log.isInfoEnabled()) 1073 log.info(sm.getString("coyoteConnector.alreadyStarted")); 1074 return; 1075 } 1076 lifecycle.fireLifecycleEvent(START_EVENT, null); 1077 started = true; 1078 1079 if ( this.oname != null ) { 1082 try { 1084 Registry.getRegistry(null, null).registerComponent 1085 (protocolHandler, createObjectName(this.domain,"ProtocolHandler"), null); 1086 } catch (Exception ex) { 1087 log.error(sm.getString 1088 ("coyoteConnector.protocolRegistrationFailed"), ex); 1089 } 1090 } else { 1091 if(log.isInfoEnabled()) 1092 log.info(sm.getString 1093 ("coyoteConnector.cannotRegisterProtocol")); 1094 } 1095 1096 try { 1097 protocolHandler.start(); 1098 } catch (Exception e) { 1099 String errPrefix = ""; 1100 if(this.service != null) { 1101 errPrefix += "service.getName(): \"" + this.service.getName() + "\"; "; 1102 } 1103 1104 throw new LifecycleException 1105 (errPrefix + " " + sm.getString 1106 ("coyoteConnector.protocolHandlerStartFailed", e)); 1107 } 1108 1109 if( this.domain != null ) { 1110 mapperListener.setDomain( domain ); 1111 mapperListener.init(); 1113 try { 1114 ObjectName mapperOname = createObjectName(this.domain,"Mapper"); 1115 if (log.isDebugEnabled()) 1116 log.debug(sm.getString( 1117 "coyoteConnector.MapperRegistration", mapperOname)); 1118 Registry.getRegistry(null, null).registerComponent 1119 (mapper, mapperOname, "Mapper"); 1120 } catch (Exception ex) { 1121 log.error(sm.getString 1122 ("coyoteConnector.protocolRegistrationFailed"), ex); 1123 } 1124 } 1125 } 1126 1127 1128 1133 public void stop() throws LifecycleException { 1134 1135 if (!started) { 1137 log.error(sm.getString("coyoteConnector.notStarted")); 1138 return; 1139 1140 } 1141 lifecycle.fireLifecycleEvent(STOP_EVENT, null); 1142 started = false; 1143 1144 try { 1145 mapperListener.destroy(); 1146 Registry.getRegistry(null, null).unregisterComponent 1147 (createObjectName(this.domain,"Mapper")); 1148 Registry.getRegistry(null, null).unregisterComponent 1149 (createObjectName(this.domain,"ProtocolHandler")); 1150 } catch (MalformedObjectNameException e) { 1151 log.error( sm.getString 1152 ("coyoteConnector.protocolUnregistrationFailed"), e); 1153 } 1154 try { 1155 protocolHandler.destroy(); 1156 } catch (Exception e) { 1157 throw new LifecycleException 1158 (sm.getString 1159 ("coyoteConnector.protocolHandlerDestroyFailed", e)); 1160 } 1161 1162 } 1163 1164 1165 protected String domain; 1167 protected ObjectName oname; 1168 protected MBeanServer mserver; 1169 ObjectName controller; 1170 1171 public ObjectName getController() { 1172 return controller; 1173 } 1174 1175 public void setController(ObjectName controller) { 1176 this.controller = controller; 1177 } 1178 1179 public ObjectName getObjectName() { 1180 return oname; 1181 } 1182 1183 public String getDomain() { 1184 return domain; 1185 } 1186 1187 public ObjectName preRegister(MBeanServer server, 1188 ObjectName name) throws Exception { 1189 oname=name; 1190 mserver=server; 1191 domain=name.getDomain(); 1192 return name; 1193 } 1194 1195 public void postRegister(Boolean registrationDone) { 1196 } 1197 1198 public void preDeregister() throws Exception { 1199 } 1200 1201 public void postDeregister() { 1202 try { 1203 if( started ) { 1204 stop(); 1205 } 1206 } catch( Throwable t ) { 1207 log.error( "Unregistering - can't stop", t); 1208 } 1209 } 1210 1211 protected void findContainer() { 1212 try { 1213 ObjectName parentName=new ObjectName ( domain + ":" + 1215 "type=Service"); 1216 1217 if(log.isDebugEnabled()) 1218 log.debug("Adding to " + parentName ); 1219 if( mserver.isRegistered(parentName )) { 1220 mserver.invoke(parentName, "addConnector", new Object [] { this }, 1221 new String [] {"org.apache.catalina.connector.Connector"}); 1222 } 1226 ObjectName engName=new ObjectName ( domain + ":" + "type=Engine"); 1229 if( mserver.isRegistered(engName )) { 1230 Object obj=mserver.getAttribute(engName, "managedResource"); 1231 if(log.isDebugEnabled()) 1232 log.debug("Found engine " + obj + " " + obj.getClass()); 1233 container=(Container)obj; 1234 1235 initialize(); 1237 1238 if(log.isDebugEnabled()) 1239 log.debug("Initialized"); 1240 return; 1243 } 1244 } catch( Exception ex ) { 1245 log.error( "Error finding container " + ex); 1246 } 1247 } 1248 1249 public void init() throws Exception { 1250 1251 if( this.getService() != null ) { 1252 if(log.isDebugEnabled()) 1253 log.debug( "Already configured" ); 1254 return; 1255 } 1256 if( container==null ) { 1257 findContainer(); 1258 } 1259 } 1260 1261 public void destroy() throws Exception { 1262 if( oname!=null && controller==oname ) { 1263 if(log.isDebugEnabled()) 1264 log.debug("Unregister itself " + oname ); 1265 Registry.getRegistry(null, null).unregisterComponent(oname); 1266 } 1267 if( getService() == null) 1268 return; 1269 getService().removeConnector(this); 1270 } 1271 1272} 1273 | Popular Tags |