1 17 package org.apache.geronimo.tomcat; 18 19 import java.net.InetSocketAddress ; 20 import java.net.UnknownHostException ; 21 import java.net.InetAddress ; 22 import java.util.Map ; 23 import java.util.HashMap ; 24 25 import org.apache.catalina.LifecycleException; 26 import org.apache.catalina.connector.Connector; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.geronimo.gbean.GBeanInfo; 30 import org.apache.geronimo.gbean.GBeanInfoBuilder; 31 import org.apache.geronimo.gbean.GBeanLifecycle; 32 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 33 import org.apache.geronimo.management.geronimo.WebManager; 34 35 38 public class ConnectorGBean extends BaseGBean implements GBeanLifecycle, ObjectRetriever, TomcatWebConnector { 39 private static final Log log = LogFactory.getLog(ConnectorGBean.class); 40 public final static String CONNECTOR_CONTAINER_REFERENCE = "TomcatContainer"; 41 42 protected final Connector connector; 43 private final TomcatContainer container; 44 private String name; 45 private String connectHost; 46 47 public ConnectorGBean(String name, String protocol, String host, int port, TomcatContainer container) throws Exception { 48 super(); 50 Map initParams = new HashMap (); 51 52 validateProtocol(protocol); 53 54 if (host == null){ 56 host = "0.0.0.0"; 57 } 58 59 if (port == 0){ 61 throw new IllegalArgumentException ("Must declare a port."); 62 } 63 64 initParams.put("address", host); 65 initParams.put("port", Integer.toString(port)); 66 initializeParams(protocol, initParams); 67 68 if(protocol != null && protocol.equals(WebManager.PROTOCOL_AJP)) { 71 protocol = "AJP/1.3"; 72 } else { 73 protocol = null; 74 } 75 76 if (name == null){ 77 throw new IllegalArgumentException ("name cannot be null."); 78 } 79 80 if (container == null){ 81 throw new IllegalArgumentException ("container cannot be null."); 82 } 83 84 this.name = name; 85 this.container = container; 86 87 connector = new Connector(protocol); 89 90 91 setParameters(connector, initParams); 93 94 } 95 96 103 protected void initializeParams(String protocol, Map params) {} 104 105 109 protected void validateProtocol(String protocol) { 110 if(protocol == null) { 111 return; 112 } 113 if(protocol.equals(WebManager.PROTOCOL_HTTPS)) { 114 throw new IllegalArgumentException ("Use a HttpsConnectorGBean for an HTTPS connector"); 115 } else if(!protocol.equals(WebManager.PROTOCOL_HTTP) && !protocol.equals(WebManager.PROTOCOL_AJP)) { 116 throw new IllegalArgumentException ("Unrecognized protocol '"+protocol+"' (use the values of the PROTOCOL_* constants in WebConnector)"); 117 } 118 } 119 120 public String getName() { 121 return name; 122 } 123 124 public Object getInternalObject() { 125 return connector; 126 } 127 128 public void doStart() throws LifecycleException { 129 container.addConnector(connector); 130 connector.start(); 131 log.debug(name + " connector started"); 132 } 133 134 public void doStop() { 135 try{ 136 connector.stop(); 137 } catch (LifecycleException e){ 138 log.error(e); 139 } 140 container.removeConnector(connector); 141 log.debug(name + " connector stopped"); 142 } 143 144 public void doFail() { 145 log.warn(name + " connector failed"); 146 doStop(); 147 } 148 149 public int getDefaultPort() { 150 return getProtocol().equals(WebManager.PROTOCOL_AJP) ? -1 : 151 getProtocol().equals(WebManager.PROTOCOL_HTTP) ? 80 : 152 getProtocol().equals(WebManager.PROTOCOL_HTTPS) ? 443 : -1; 153 } 154 155 public String getConnectUrl() { 156 if(connectHost == null) { 157 String host = getHost(); 158 if(host == null || host.equals("0.0.0.0")) { 159 InetAddress address = null; 160 try { 161 address = InetAddress.getLocalHost(); 162 } catch (UnknownHostException e) { 163 host = "unknown-host"; 164 } 165 if(address != null) { 166 host = address.getCanonicalHostName(); 167 if(host == null || host.equals("")) { 168 host = address.getHostAddress(); 169 } 170 } 171 } 172 connectHost = host; 173 } 174 return getProtocol().toLowerCase()+"://"+connectHost+(getPort() == getDefaultPort() ? "" : ":"+getPort()); 175 } 176 177 public boolean isEmptySessionPath(){ 178 return connector.getEmptySessionPath(); 179 } 180 181 public void setEmptySessionPath(boolean emptySessionPath){ 182 connector.setEmptySessionPath(emptySessionPath); 183 } 184 185 188 public String getProtocol() { 189 String protocol = connector.getProtocol(); 190 if(protocol.indexOf("AJP") > -1) { 191 return WebManager.PROTOCOL_AJP; 192 } else if(connector.getScheme().equalsIgnoreCase("http")) { 193 return WebManager.PROTOCOL_HTTP; 194 } else if(connector.getScheme().equalsIgnoreCase("https")) { 195 return WebManager.PROTOCOL_HTTPS; 196 } 197 throw new IllegalStateException ("Unknown protocol '"+protocol+"' and scheme '"+connector.getScheme()+"'"); 198 } 199 200 203 public int getPort() { 204 return connector.getPort(); 205 } 206 207 210 public void setPort(int port) { 211 connector.setPort(port); 212 } 213 214 217 public String getHost() { 218 Object value = connector.getAttribute("address"); 219 if(value == null) { 220 return "0.0.0.0"; 221 } else if(value instanceof InetAddress ) { 222 return ((InetAddress )value).getHostAddress(); 223 } else return value.toString(); 224 } 225 226 233 public void setHost(String host) throws UnknownHostException { 234 connector.setAttribute("address", host); 235 } 236 237 243 public InetSocketAddress getListenAddress() { 244 return new InetSocketAddress (getHost(), getPort()); 245 } 246 247 251 public int getBufferSizeBytes() { 252 Object value = connector.getAttribute("bufferSize"); 253 return value == null ? 2048 : Integer.parseInt(value.toString()); 254 } 255 256 260 public void setBufferSizeBytes(int bytes) { 261 connector.setAttribute("bufferSize", new Integer (bytes)); 262 } 263 264 268 public int getMaxThreads() { 269 Object value = connector.getAttribute("maxThreads"); 270 return value == null ? 200 : Integer.parseInt(value.toString()); 271 } 272 273 277 public void setMaxThreads(int threads) { 278 connector.setAttribute("maxThreads", new Integer (threads)); 279 } 280 281 286 public int getAcceptQueueSize() { 287 Object value = connector.getAttribute("acceptCount"); 288 return value == null ? 10 : Integer.parseInt(value.toString()); 289 } 290 291 296 public void setAcceptQueueSize(int size) { 297 connector.setAttribute("acceptCount", new Integer (size)); 298 } 299 300 304 public int getLingerMillis() { 305 Object value = connector.getAttribute("connectionLinger"); 306 return value == null ? -1 : Integer.parseInt(value.toString()); 307 } 308 309 313 public void setLingerMillis(int millis) { 314 connector.setAttribute("connectionLinger", new Integer (millis)); 315 } 316 317 322 public boolean isTcpNoDelay() { 323 Object value = connector.getAttribute("tcpNoDelay"); 324 return value == null ? true : new Boolean (value.toString()).booleanValue(); 325 } 326 327 332 public void setTcpNoDelay(boolean enable) { 333 connector.setAttribute("tcpNoDelay", new Boolean (enable)); 334 } 335 336 342 public int getRedirectPort() { 343 return connector.getRedirectPort(); 344 } 345 346 353 public void setRedirectPort(int port) { 354 connector.setRedirectPort(port); 355 } 356 357 public int getMinSpareThreads() { 358 Object value = connector.getAttribute("minSpareThreads"); 359 return value == null ? 4 : Integer.parseInt(value.toString()); 360 } 361 362 public void setMinSpareThreads(int threads) { 363 connector.setAttribute("minSpareThreads", new Integer (threads)); 364 } 365 366 public int getMaxSpareThreads() { 367 Object value = connector.getAttribute("maxSpareThreads"); 368 return value == null ? 50 : Integer.parseInt(value.toString()); 369 } 370 371 public void setMaxSpareThreads(int threads) { 372 connector.setAttribute("maxSpareThreads", new Integer (threads)); 373 } 374 375 public int getMaxHttpHeaderSizeBytes() { 376 Object value = connector.getAttribute("maxHttpHeaderSize"); 377 return value == null ? 4096 : Integer.parseInt(value.toString()); 378 } 379 380 public void setMaxHttpHeaderSizeBytes(int bytes) { 381 connector.setAttribute("maxHttpHeaderSize", new Integer (bytes)); 382 } 383 384 public boolean isHostLookupEnabled() { 385 return connector.getEnableLookups(); 386 } 387 388 public void setHostLookupEnabled(boolean enabled) { 389 connector.setEnableLookups(enabled); 390 } 391 392 public int getConnectionTimeoutMillis() { 393 Object value = connector.getAttribute("connectionTimeout"); 394 return value == null ? 60000 : Integer.parseInt(value.toString()); 395 } 396 397 public void setConnectionTimeoutMillis(int millis) { 398 connector.setAttribute("connectionTimeout", new Integer (millis)); 399 } 400 401 public boolean isUploadTimeoutEnabled() { 402 Object value = connector.getAttribute("disableUploadTimeout"); 403 return value == null ? true : !new Boolean (value.toString()).booleanValue(); 404 } 405 406 public void setUploadTimeoutEnabled(boolean enabled) { 407 connector.setAttribute("disableUploadTimeout", new Boolean (!enabled)); 408 } 409 410 public int getMaxPostSize() { 411 int value = connector.getMaxPostSize(); 412 return value == 0 ? 2097152 : value; 413 } 414 415 public void setMaxPostSize(int bytes) { 416 connector.setMaxPostSize(bytes); 417 } 418 419 public int getMaxSavePostSize() { 420 int value = connector.getMaxSavePostSize(); 421 return value == 0 ? 4096 : value; 422 } 423 424 public void setMaxSavePostSize(int kbytes) { 425 connector.setMaxSavePostSize(kbytes); 426 } 427 428 public int getMaxKeepAliveRequests() { 429 Object value = connector.getAttribute("maxKeepAliveRequests"); 430 return value == null ? 100 : Integer.parseInt(value.toString()); 431 } 432 433 public void setMaxKeepAliveRequests(int maxKeepAliveRequests) { 434 connector.setAttribute("maxKeepAliveRequests", new Integer (maxKeepAliveRequests)); 435 } 436 437 public int getSocketBuffer() { 438 Object value = connector.getAttribute("socketBuffer"); 439 return value == null ? 9000 : Integer.parseInt(value.toString()); 440 } 441 442 public void setSocketBuffer(int kbytes) { 443 connector.setAttribute("socketBuffer", new Integer (kbytes)); 444 } 445 446 public boolean getUseBodyEncodingForURI() { 447 return connector.getUseBodyEncodingForURI(); 448 } 449 450 public void setUseBodyEncodingForURI(boolean enabled) { 451 connector.setUseBodyEncodingForURI(enabled); 452 } 453 454 public void setAllowTrace(boolean allow) { 455 connector.setAllowTrace(allow); 456 } 457 458 public boolean getAllowTrace() { 459 return connector.getAllowTrace(); 460 } 461 462 public void setProxyName(String proxyName) { 463 connector.setProxyName(proxyName); 464 } 465 466 public String getProxyName() { 467 return connector.getProxyName(); 468 } 469 470 public void setProxyPort(int port) { 471 connector.setProxyPort(port); 472 } 473 474 public int getProxyPort() { 475 return connector.getProxyPort(); 476 } 477 478 public void setScheme(String scheme) { 479 connector.setScheme(scheme); 480 } 481 482 public String getScheme() { 483 return connector.getScheme(); 484 } 485 486 public void setUriEncoding(String encoding) { 487 connector.setURIEncoding(encoding); 488 } 489 490 public String getUriEncoding() { 491 return connector.getURIEncoding(); 492 } 493 494 public void setUseIPVHosts(boolean useIPVHosts) { 495 connector.setUseIPVHosts(useIPVHosts); 496 } 497 498 public boolean getUseIPVHosts() { 499 return connector.getUseIPVHosts(); 500 } 501 502 public void setXpoweredBy(boolean xpoweredBy) { 503 connector.setXpoweredBy(xpoweredBy); 504 } 505 506 public boolean getXpoweredBy() { 507 return connector.getXpoweredBy(); 508 } 509 510 public void setCompressableMimeType(String compressableMimeType) { 511 connector.setAttribute("compressableMimeType", compressableMimeType); 512 } 513 514 public String getCompressableMimeType() { 515 return (String ) connector.getAttribute("compressableMimeType"); 516 } 517 518 public void setCompression(String compression) { 519 connector.setAttribute("compression", compression); 520 } 521 522 public String getCompression() { 523 return (String ) connector.getAttribute("compression"); 524 } 525 526 public void setNoCompressionUserAgents(String noCompressionUserAgents) { 527 connector.setAttribute("noCompressionUserAgents", noCompressionUserAgents); 528 } 529 530 public String getNoCompressionUserAgents() { 531 return (String ) connector.getAttribute("noCompressionUserAgents"); 532 } 533 534 public void setRestrictedUserAgents(String restrictedUserAgents) { 535 connector.setAttribute("restrictedUserAgents", restrictedUserAgents); 536 } 537 538 public String getRestrictedUserAgents() { 539 return (String ) connector.getAttribute("restrictedUserAgents"); 540 } 541 542 public void setThreadPriority(int threadPriority) { 543 connector.setAttribute("threadPriority", new Integer (threadPriority)); 544 } 545 546 public int getThreadPriority() { 547 Object value = connector.getAttribute("threadPriority"); 548 return value == null ? 5 :Integer.parseInt(value.toString()); 549 } 550 551 public void setServer(String server) { 552 connector.setAttribute("server", server); 553 } 554 555 public String getServer() { 556 return (String ) connector.getAttribute("server"); 557 } 558 559 public void setStrategy(String strategy) { 560 connector.setAttribute("strategy", strategy); 561 } 562 563 public String getStrategy() { 564 return (String ) connector.getAttribute("strategy"); 565 } 566 567 public static final GBeanInfo GBEAN_INFO; 568 569 static { 570 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Tomcat Connector", ConnectorGBean.class); 571 infoFactory.addAttribute("name", String .class, true); 572 infoFactory.addAttribute("protocol", String .class, true); 573 infoFactory.addReference(CONNECTOR_CONTAINER_REFERENCE, TomcatContainer.class, NameFactory.GERONIMO_SERVICE); 574 infoFactory.addOperation("getInternalObject"); 575 infoFactory.addInterface(TomcatWebConnector.class, 576 new String []{ 577 "host", 578 "port", 579 "bufferSizeBytes", 580 "maxThreads", 581 "acceptQueueSize", 582 "lingerMillis", 583 "tcpNoDelay", 584 "redirectPort", 585 "minSpareThreads", 586 "maxSpareThreads", 587 "maxHttpHeaderSizeBytes", 588 "hostLookupEnabled", 589 "connectionTimeoutMillis", 590 "uploadTimeoutEnabled", 591 "connectUrl", 592 "maxPostSize", 593 "maxSavePostSize", 594 "emptySessionPath", 595 "maxKeepAliveRequests", 596 "socketBuffer", 597 "useBodyEncodingForURI", 598 "allowTrace", 599 "proxyName", 600 "proxyPort", 601 "scheme", 602 "secure", 603 "uriEncoding", 604 "useIPVHosts", 605 "xpoweredBy", 606 "compressableMimeType", 607 "compression", 608 "noCompressionUserAgents", 609 "restrictedUserAgents", 610 "threadPriority", 611 "server", 612 "strategy" 613 }, 614 615 new String []{ 616 "host", 617 "port", 618 "redirectPort", 619 "maxThreads"}); 620 infoFactory.setConstructor(new String [] { "name", "protocol", "host", "port", "TomcatContainer"}); 621 GBEAN_INFO = infoFactory.getBeanInfo(); 622 } 623 624 public static GBeanInfo getGBeanInfo() { 625 return GBEAN_INFO; 626 } 627 628 } 629 | Popular Tags |