1 16 17 package org.apache.ajp.tomcat4; 18 19 import java.io.IOException ; 20 import java.net.InetAddress ; 21 import java.net.ServerSocket ; 22 import java.net.Socket ; 23 import java.security.AccessControlException ; 24 import java.util.Stack ; 25 import java.util.Vector ; 26 27 import org.apache.catalina.Connector; 28 import org.apache.catalina.Container; 29 import org.apache.catalina.Lifecycle; 30 import org.apache.catalina.LifecycleException; 31 import org.apache.catalina.LifecycleListener; 32 import org.apache.catalina.Request; 33 import org.apache.catalina.Response; 34 import org.apache.catalina.Service; 35 import org.apache.catalina.net.DefaultServerSocketFactory; 36 import org.apache.catalina.net.ServerSocketFactory; 37 import org.apache.catalina.util.LifecycleSupport; 38 import org.apache.catalina.util.StringManager; 39 40 46 47 48 public final class Ajp13Connector 49 implements Connector, Lifecycle, Runnable { 50 51 52 54 55 58 private int acceptCount = 10; 59 60 61 65 private String address = null; 66 67 68 71 private int bufferSize = 2048; 72 73 74 77 protected Container container = null; 78 79 80 83 private Vector created = new Vector (); 84 85 86 89 private int curProcessors = 0; 90 91 92 95 private int debug = 0; 96 97 98 101 private ServerSocketFactory factory = null; 102 103 104 107 private static final String info = 108 "org.apache.catalina.connector.ajp.Ajp13Connector/1.0"; 109 110 111 114 private int redirectPort = -1; 115 116 119 private boolean enableLookups = false; 120 121 124 protected LifecycleSupport lifecycle = new LifecycleSupport(this); 125 126 127 130 protected int minProcessors = 5; 131 132 133 136 private int maxProcessors = 20; 137 138 139 143 private int connectionTimeout = -1; 144 145 146 150 private int connectionLinger = -1; 151 152 153 156 private int port = 8009; 157 158 159 163 private Stack processors = new Stack (); 164 165 166 170 private String scheme = "http"; 171 172 173 177 private boolean secure = false; 178 179 180 183 private ServerSocket serverSocket = null; 184 185 186 189 private StringManager sm = 190 StringManager.getManager(Constants.PACKAGE); 191 192 193 196 private boolean started = false; 197 198 199 202 private boolean stopped = false; 203 204 205 208 private Thread thread = null; 209 210 211 214 private ThreadGroup threadGroup = null; 215 216 217 220 private String threadName = null; 221 222 223 226 private DebugThread debugThread = null; 227 228 229 232 private Object threadSync = new Object (); 233 234 private Ajp13Logger logger = new Ajp13Logger(); 235 236 239 private Service service = null; 240 241 private String secret = null; 242 243 244 248 private boolean tomcatAuthentication = true; 249 250 251 253 254 257 public int getConnectionTimeout() { 258 259 return (connectionTimeout); 260 261 } 262 263 264 269 public void setConnectionTimeout(int connectionTimeout) { 270 271 this.connectionTimeout = connectionTimeout; 272 273 } 274 275 278 public int getConnectionLinger() { 279 280 return (connectionLinger); 281 282 } 283 284 285 290 public void setConnectionLinger(int connectionLinger) { 291 292 this.connectionLinger = connectionLinger; 293 294 } 295 296 public void setSecret( String s ) { 297 secret=s; 298 } 299 300 public String getSecret() { 301 return secret; 302 } 303 304 305 308 public int getAcceptCount() { 309 310 return (acceptCount); 311 312 } 313 314 315 320 public void setAcceptCount(int count) { 321 322 this.acceptCount = count; 323 324 } 325 326 327 328 331 public String getAddress() { 332 333 return (this.address); 334 335 } 336 337 338 343 public void setAddress(String address) { 344 345 this.address = address; 346 347 } 348 349 350 353 public boolean isAvailable() { 354 355 return (started); 356 357 } 358 359 360 363 public int getBufferSize() { 364 365 return (this.bufferSize); 366 367 } 368 369 370 375 public void setBufferSize(int bufferSize) { 376 377 this.bufferSize = bufferSize; 378 379 } 380 381 382 386 public Container getContainer() { 387 388 return (container); 389 390 } 391 392 393 399 public void setContainer(Container container) { 400 401 this.container = container; 402 403 } 404 405 406 409 public int getCurProcessors() { 410 411 return (curProcessors); 412 413 } 414 415 416 419 public int getDebug() { 420 421 return (debug); 422 423 } 424 425 426 431 public void setDebug(int debug) { 432 433 this.debug = debug; 434 435 } 436 437 440 public boolean getEnableLookups() { 441 return this.enableLookups; 442 } 443 444 449 public void setEnableLookups(boolean enableLookups) { 450 this.enableLookups = enableLookups; 451 } 452 453 458 public int getRedirectPort() { 459 return this.redirectPort; 460 } 461 462 463 468 public void setRedirectPort(int redirectPort) { 469 this.redirectPort = redirectPort; 470 } 471 472 475 public ServerSocketFactory getFactory() { 476 477 if (this.factory == null) { 478 synchronized (this) { 479 this.factory = new DefaultServerSocketFactory(); 480 } 481 } 482 return (this.factory); 483 484 } 485 486 487 492 public void setFactory(ServerSocketFactory factory) { 493 494 this.factory = factory; 495 496 } 497 498 499 502 public String getInfo() { 503 504 return (info); 505 506 } 507 508 509 512 public int getMinProcessors() { 513 514 return (minProcessors); 515 516 } 517 518 519 524 public void setMinProcessors(int minProcessors) { 525 526 this.minProcessors = minProcessors; 527 528 } 529 530 531 534 public int getMaxProcessors() { 535 536 return (maxProcessors); 537 538 } 539 540 541 546 public void setMaxProcessors(int maxProcessors) { 547 548 this.maxProcessors = maxProcessors; 549 550 } 551 552 553 556 public int getPort() { 557 558 return (this.port); 559 560 } 561 562 563 568 public void setPort(int port) { 569 570 this.port = port; 571 572 } 573 574 575 579 public String getScheme() { 580 581 return (this.scheme); 582 583 } 584 585 586 592 public void setScheme(String scheme) { 593 594 this.scheme = scheme; 595 596 } 597 598 599 603 public boolean getSecure() { 604 605 return (this.secure); 606 607 } 608 609 610 616 public void setSecure(boolean secure) { 617 618 this.secure = secure; 619 620 } 621 622 623 626 public Service getService() { 627 return service; 628 } 629 630 631 634 public void setService(Service service) { 635 this.service = service; 636 } 637 638 639 642 public boolean getTomcatAuthentication() { 643 return tomcatAuthentication; 644 } 645 646 647 650 public void setTomcatAuthentication(boolean tomcatAuthentication) { 651 this.tomcatAuthentication = tomcatAuthentication; 652 } 653 654 655 657 658 662 public Request createRequest() { 663 664 Ajp13Request request = new Ajp13Request(this); 665 request.setConnector(this); 666 return (request); 667 668 } 669 670 671 675 public Response createResponse() { 676 677 Ajp13Response response = new Ajp13Response(); 678 response.setConnector(this); 679 return (response); 680 681 } 682 683 688 public void initialize() throws LifecycleException { 689 } 690 691 692 694 695 700 void recycle(Ajp13Processor processor) { 701 702 synchronized(processors) { 703 if (debug > 0) { 704 logger.log("added processor to available processors, available=" 705 + processors.size()); 706 } 707 processors.push(processor); 708 } 709 710 } 711 712 713 715 716 722 private Ajp13Processor createProcessor() { 723 724 synchronized (processors) { 725 if (processors.size() > 0) 726 return ((Ajp13Processor) processors.pop()); 727 if ((maxProcessors > 0) && (curProcessors < maxProcessors)) 728 return (newProcessor()); 729 else 730 return (null); 731 } 732 733 } 734 735 736 740 private Ajp13Processor newProcessor() { 741 742 Ajp13Processor processor = new Ajp13Processor(this, curProcessors++, threadGroup); 743 if (processor instanceof Lifecycle) { 744 try { 745 ((Lifecycle) processor).start(); 746 } catch (LifecycleException e) { 747 logger.log("newProcessor", e); 748 curProcessors--; 749 return (null); 750 } 751 } 752 created.addElement(processor); 753 return (processor); 754 755 } 756 757 758 765 private ServerSocket open() throws IOException { 766 767 ServerSocketFactory factory = getFactory(); 769 770 if (address == null) { 772 logger.log(sm.getString("ajp13Connector.allAddresses")); 773 try { 774 return (factory.createSocket(port, acceptCount)); 775 } catch(Exception ex ) { 776 ex.printStackTrace(); 777 return null; 778 } 779 } 780 781 try { 783 InetAddress is = InetAddress.getByName(address); 784 logger.log(sm.getString("ajp13Connector.anAddress", address)); 785 return (factory.createSocket(port, acceptCount, is)); 786 } catch (Exception e) { 787 try { 788 logger.log(sm.getString("ajp13Connector.noAddress", address)); 789 return (factory.createSocket(port, acceptCount)); 790 } catch( Exception e1 ) { 791 e1.printStackTrace(); 792 return null; 793 } 794 } 795 796 } 797 798 799 801 802 806 public void run() { 807 808 while (!stopped) { 810 811 Socket socket = null; 813 try { 814 if (debug > 0) { 815 logger.log("accepting socket..."); 816 } 817 818 socket = serverSocket.accept(); 819 820 if (debug > 0) { 821 logger.log("accepted socket, assigning to processor."); 822 } 823 824 837 838 if (connectionLinger < 0) 839 socket.setSoLinger(false, 0); 840 else 841 socket.setSoLinger(true, connectionLinger); 842 843 849 850 861 862 if (connectionTimeout >= 0) { 863 socket.setSoTimeout(connectionTimeout); 864 } 865 } catch (AccessControlException ace) { 866 logger.log("socket accept security exception: " 867 + ace.getMessage()); 868 continue; 869 } catch (IOException e) { 870 if (started && !stopped) 871 logger.log("accept: ", e); 872 try { 873 if (serverSocket != null) { 874 serverSocket.close(); 875 } 876 if (stopped) { 877 if (debug > 0) { 878 logger.log("run(): stopped, so breaking"); 879 } 880 break; 881 } else { 882 if (debug > 0) { 883 logger.log("run(): not stopped, " + 884 "so reopening server socket"); 885 } 886 serverSocket = open(); 887 } 888 } catch (IOException ex) { 889 logger.log("socket reopen: ", ex); 891 break; 892 } 893 continue; 894 } 895 896 if (debug > 0) { 898 synchronized(processors) { 899 logger.log("about to create a processor, available=" 900 + processors.size() + ", created=" + created.size() 901 + ", maxProcessors=" + maxProcessors); 902 } 903 } 904 Ajp13Processor processor = createProcessor(); 905 if (processor == null) { 906 try { 907 logger.log(sm.getString("ajp13Connector.noProcessor")); 908 socket.close(); 909 } catch (IOException e) { 910 ; 911 } 912 continue; 913 } 914 processor.assign(socket); 915 916 918 } 919 920 synchronized (threadSync) { 922 threadSync.notifyAll(); 923 } 924 925 } 926 927 928 931 private void threadStart() { 932 933 logger.log(sm.getString("ajp13Connector.starting")); 934 935 thread = new Thread (threadGroup, this, threadName); 936 thread.setDaemon(true); 937 thread.start(); 938 939 } 940 941 942 945 private void threadStop() { 946 947 logger.log(sm.getString("ajp13Connector.stopping")); 948 949 stopped = true; 950 synchronized (threadSync) { 951 try { 952 threadSync.wait(5000); 953 } catch (InterruptedException e) { 954 ; 955 } 956 } 957 thread = null; 958 959 } 960 961 962 964 965 970 public void addLifecycleListener(LifecycleListener listener) { 971 972 lifecycle.addLifecycleListener(listener); 973 974 } 975 976 980 public LifecycleListener[] findLifecycleListeners() { 981 return null; } 983 984 985 990 public void removeLifecycleListener(LifecycleListener listener) { 991 992 lifecycle.removeLifecycleListener(listener); 993 994 } 995 996 997 1002 public void start() throws LifecycleException { 1003 1004 if (started) 1006 throw new LifecycleException 1007 (sm.getString("ajp13Connector.alreadyStarted")); 1008 1009 if (debug > 0) { 1010 debugThread = new DebugThread(); 1011 debugThread.setDaemon(true); 1012 debugThread.start(); 1013 } 1014 1015 threadName = "Ajp13Connector[" + port + "]"; 1016 threadGroup = new ThreadGroup (threadName); 1017 threadGroup.setDaemon(true); 1018 logger.setConnector(this); 1019 logger.setName(threadName); 1020 lifecycle.fireLifecycleEvent(START_EVENT, null); 1021 started = true; 1022 1023 try { 1025 serverSocket = open(); 1026 } catch (IOException e) { 1027 throw new LifecycleException(threadName + ".open", e); 1028 } 1029 1030 threadStart(); 1032 1033 while (curProcessors < minProcessors) { 1035 if ((maxProcessors > 0) && (curProcessors >= maxProcessors)) 1036 break; 1037 Ajp13Processor processor = newProcessor(); 1038 recycle(processor); 1039 } 1040 1041 } 1042 1043 1044 1049 public void stop() throws LifecycleException { 1050 1051 if (!started) 1053 throw new LifecycleException 1054 (sm.getString("ajp13Connector.notStarted")); 1055 lifecycle.fireLifecycleEvent(STOP_EVENT, null); 1056 started = false; 1057 1058 for (int i = created.size() - 1; i >= 0; i--) { 1060 Ajp13Processor processor = (Ajp13Processor) created.elementAt(i); 1061 if (processor instanceof Lifecycle) { 1062 try { 1063 ((Lifecycle) processor).stop(); 1064 } catch (LifecycleException e) { 1065 logger.log("Ajp13Connector.stop", e); 1066 } 1067 } 1068 } 1069 1070 threadStop(); 1072 1073 if (serverSocket != null) { 1075 try { 1076 serverSocket.close(); 1077 } catch (IOException e) { 1078 ; 1079 } 1080 serverSocket = null; 1081 } 1082 1083 } 1084 1085 1089 private class DebugThread extends Thread 1090 { 1091 public void run() { 1092 while (true) { 1093 try { 1094 sleep(60 * 1000); 1095 } catch (InterruptedException e) { 1096 break; 1097 } 1098 logger.log("active threads=" + threadGroup.activeCount()); 1099 System.out.println("==================================="); 1100 System.out.println("Ajp13Connector active threads=" 1101 + threadGroup.activeCount()); 1102 threadGroup.list(); 1103 System.out.println("==================================="); 1104 } 1105 } 1106 } 1107 1108} 1109 | Popular Tags |