KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > connector > grizzly > GrizzlyHttpProtocol


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 /*
24  * Copyright 1999-2004 The Apache Software Foundation
25  *
26  * Licensed under the Apache License, Version 2.0 (the "License");
27  * you may not use this file except in compliance with the License.
28  * You may obtain a copy of the License at
29  *
30  * http://www.apache.org/licenses/LICENSE-2.0
31  *
32  * Unless required by applicable law or agreed to in writing, software
33  * distributed under the License is distributed on an "AS IS" BASIS,
34  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35  * See the License for the specific language governing permissions and
36  * limitations under the License.
37  */

38 package com.sun.enterprise.web.connector.grizzly;
39
40 import java.net.InetAddress JavaDoc;
41 import java.net.URLEncoder JavaDoc;
42 import java.util.Enumeration JavaDoc;
43 import java.util.logging.Level JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45 import javax.management.ObjectName JavaDoc;
46
47 import com.sun.org.apache.commons.modeler.Registry;
48 import org.apache.coyote.http11.Http11Protocol;
49 import org.apache.tomcat.util.net.SSLImplementation;
50 import org.apache.tomcat.util.net.ServerSocketFactory;
51
52 /**
53  * Abstract the protocol implementation, including threading, etc.
54  * Processor is single threaded and specific to stream-based protocols.
55  * This is an adaptation of org.apache.coyote.http11.Http11Processor except here
56  * NIO is used.
57  *
58  * @author Jean-Francois Arcand
59  */

60 public class GrizzlyHttpProtocol extends Http11Protocol {
61  
62     private int socketBuffer = 9000;
63     
64     /**
65      * The <code>SelectorThread</code> used by this object.
66      */

67     private SelectorThread selectorThread;
68
69     /**
70      * Compression value.
71      */

72     private String JavaDoc noCompressionUserAgents = null;
73     private String JavaDoc restrictedUserAgents = null;
74     private String JavaDoc compressableMimeTypes = "text/html,text/xml,text/plain";
75     private int compressionMinSize = 2048;
76         
77     // ------------------------------------------------------- Constructor --//
78

79     /**
80      * This method is called by the constructor of the Http11Protocol
81      * superclass.
82      */

83     protected void create() {
84         
85         // 1 Selector thread per Connector.
86
selectorThread = new SelectorThread();
87         
88         setSoLinger(-1);
89         setSoTimeout(Constants.DEFAULT_CONNECTION_TIMEOUT);
90         setServerSoTimeout(Constants.DEFAULT_SERVER_SOCKET_TIMEOUT);
91         setTcpNoDelay(Constants.DEFAULT_TCP_NO_DELAY);
92     }
93
94     
95     // ---------------------------------------------------- Public methods --//
96
/**
97      * Start the protocol
98      */

99     public void init() throws Exception JavaDoc {
100         try {
101             checkSocketFactory();
102         } catch( Exception JavaDoc ex ) {
103             SelectorThread.logger().log(Level.SEVERE,
104                        "grizzlyHttpProtocol.socketfactory.initerror",ex);
105             throw ex;
106         }
107
108         if( socketFactory!=null ) {
109             Enumeration JavaDoc attE=attributes.keys();
110             while( attE.hasMoreElements() ) {
111                 String JavaDoc key=(String JavaDoc)attE.nextElement();
112                 Object JavaDoc v=attributes.get( key );
113                 socketFactory.setAttribute( key, v );
114             }
115         }
116         
117         try {
118              selectorThread.setAdapter(adapter);
119              selectorThread.initEndpoint();
120         } catch (Exception JavaDoc ex) {
121             SelectorThread.logger().log(Level.SEVERE,
122                        "grizzlyHttpProtocol.endpoint.initerror", ex);
123             throw ex;
124         }
125     }
126     
127     
128     public void start() throws Exception JavaDoc {
129         try {
130             if ( this.oname != null ) {
131                 try {
132                     Registry.getRegistry().registerComponent
133                         (selectorThread, this.domain, "selectorThread",
134                          "type=Selector,name=http" + selectorThread.getPort());
135                 } catch (Exception JavaDoc ex) {
136                     SelectorThread.logger().log(Level.SEVERE,
137                       "grizzlyHttpProtocol.selectorRegistrationFailed", ex);
138                 }
139             } else {
140                 SelectorThread.logger().log(Level.INFO,
141                            "grizzlyHttpProtocol.selectorRegisterProtocol");
142             }
143
144             selectorThread.start();
145         } catch (Exception JavaDoc ex) {
146             SelectorThread.logger().log(Level.SEVERE,
147                        "grizzlyHttpProtocol.endpoint.starterror",
148                        ex);
149             throw ex;
150         }
151         
152         SelectorThread.logger().log(Level.INFO,
153                    "grizzlyHttpProtocol.start", String.valueOf(getPort()));
154     }
155
156     public void pause() throws Exception JavaDoc {
157         try {
158             selectorThread.pauseEndpoint();
159         } catch (Exception JavaDoc ex) {
160             SelectorThread.logger().log(Level.SEVERE,
161                        "grizzlyHttpProtocol.endpoint.pauseerror",
162                        ex);
163             throw ex;
164         }
165         SelectorThread.logger().log(Level.INFO,
166                    "grizzlyHttpProtocol.pause", String.valueOf(getPort()));
167     }
168
169     public void resume() throws Exception JavaDoc {
170         try {
171             selectorThread.resumeEndpoint();
172         } catch (Exception JavaDoc ex) {
173             SelectorThread.logger().log(Level.SEVERE,
174                        "grizzlyHttpProtocol.endpoint.resumeerror",
175                        ex);
176             throw ex;
177         }
178         SelectorThread.logger().log(Level.INFO,
179                    "grizzlyHttpProtocol.resume",
180                                 String.valueOf(getPort()));
181     }
182
183     public void destroy() throws Exception JavaDoc {
184         SelectorThread.logger().log(Level.INFO,
185                    "grizzlyHttpProtocol.stop",
186                                 String.valueOf(getPort()));
187         
188         if ( domain != null ){
189             Registry.getRegistry().
190                     unregisterComponent(new ObjectName JavaDoc(domain,"type", "Selector"));
191         }
192         selectorThread.stopEndpoint();
193     }
194     
195     
196     // -------------------- Pool setup --------------------
197

198
199     public int getMaxThreads() {
200         return selectorThread.getMaxThreads();
201     }
202     
203     public void setMaxThreads( int maxThreads ) {
204         selectorThread.setMaxThreads(maxThreads);
205         setAttribute("maxThreads", "" + maxThreads);
206     }
207     
208
209     public int getProcessorThreadsIncrement() {
210         return selectorThread.threadsIncrement;
211     }
212
213     
214     public void setProcessorThreadsIncrement( int threadsIncrement ) {
215         selectorThread.threadsIncrement = threadsIncrement;
216         setAttribute("threadsIncrement", "" + threadsIncrement);
217     }
218     
219     
220     public void setProcessorWorkerThreadsTimeout(int timeout){
221         selectorThread.threadsTimeout = timeout;
222         setAttribute("threadsTimeout", "" + timeout);
223     }
224     
225     
226     public int getProcessorWorkerThreadsTimeout(){
227         return selectorThread.threadsTimeout;
228     }
229     // -------------------- Tcp setup --------------------
230

231     public int getBacklog() {
232         return selectorThread.ssBackLog;
233     }
234     
235     public void setBacklog( int i ) {
236         ;
237     }
238     
239     public int getPort() {
240         return selectorThread.getPort();
241     }
242     
243     public void setPort( int port ) {
244         selectorThread.setPort(port);
245         setAttribute("port", "" + port);
246         //this.port=port;
247
}
248
249     public InetAddress JavaDoc getAddress() {
250         return selectorThread.getAddress();
251     }
252     
253     public void setAddress(InetAddress JavaDoc ia) {
254         selectorThread.setAddress( ia );
255         setAttribute("address", "" + ia);
256     }
257     
258     public String JavaDoc getName() {
259         String JavaDoc encodedAddr = "";
260         if (getAddress() != null) {
261             encodedAddr = "" + getAddress();
262             if (encodedAddr.startsWith("/"))
263                 encodedAddr = encodedAddr.substring(1);
264             encodedAddr = URLEncoder.encode(encodedAddr) + "-";
265         }
266         return ("http-" + encodedAddr + selectorThread.getPort());
267     }
268     
269     public boolean getTcpNoDelay() {
270         return selectorThread.getTcpNoDelay();
271     }
272     
273     public void setTcpNoDelay( boolean b ) {
274         selectorThread.setTcpNoDelay( b );
275         setAttribute("tcpNoDelay", "" + b);
276     }
277
278     public boolean getDisableUploadTimeout() {
279         return disableUploadTimeout;
280     }
281     
282     public void setDisableUploadTimeout(boolean isDisabled) {
283         disableUploadTimeout = isDisabled;
284     }
285
286     public int getSocketBuffer() {
287         return socketBuffer;
288     }
289     
290     public void setSocketBuffer(int valueI) {
291         socketBuffer = valueI;
292     }
293
294     public void setMaxHttpHeaderSize(int maxHttpHeaderSize) {
295         super.setMaxHttpHeaderSize(maxHttpHeaderSize);
296         selectorThread.setMaxHttpHeaderSize(maxHttpHeaderSize);
297     }
298
299     public String JavaDoc getRestrictedUserAgents() {
300         return restrictedUserAgents;
301     }
302     
303     public void setRestrictedUserAgents(String JavaDoc valueS) {
304         restrictedUserAgents = valueS;
305         setAttribute("restrictedUserAgents", valueS);
306     }
307
308     public String JavaDoc getNoCompressionUserAgents() {
309         return noCompressionUserAgents;
310     }
311     
312     public void setNoCompressionUserAgents(String JavaDoc valueS) {
313         noCompressionUserAgents = valueS;
314         setAttribute("noCompressionUserAgents", valueS);
315     }
316
317     public String JavaDoc getCompressableMimeType() {
318         return compressableMimeTypes;
319     }
320     
321     public void setCompressableMimeType(String JavaDoc valueS) {
322         compressableMimeTypes = valueS;
323         setAttribute("compressableMimeTypes", valueS);
324     }
325
326     public int getCompressionMinSize() {
327         return compressionMinSize;
328     }
329     
330     public void setCompressionMinSize(int valueI) {
331         compressionMinSize = valueI;
332         setAttribute("compressionMinSize", "" + valueI);
333     }
334
335     public int getSoLinger() {
336         return selectorThread.getSoLinger();
337     }
338     
339     public void setSoLinger( int i ) {
340         selectorThread.setSoLinger( i );
341         setAttribute("soLinger", "" + i);
342     }
343
344     public int getSoTimeout() {
345         return selectorThread.getSoTimeout();
346     }
347     
348     public void setSoTimeout( int i ) {
349         selectorThread.setSoTimeout(i);
350         setAttribute("soTimeout", "" + i);
351     }
352     
353     public int getServerSoTimeout() {
354         return selectorThread.getServerSoTimeout();
355     }
356     
357     public void setServerSoTimeout( int i ) {
358         selectorThread.setServerSoTimeout(i);
359         setAttribute("serverSoTimeout", "" + i);
360     }
361     
362     public void setSecure( boolean b ) {
363         super.setSecure(b);
364         selectorThread.setSecure(b);
365     }
366
367     /**
368      * Set the maximum number of Keep-Alive requests that we will honor.
369      */

370     public void setMaxKeepAliveRequests(int mkar) {
371         selectorThread.setMaxKeepAliveRequests(mkar);
372     }
373    
374
375     /**
376      * Sets the number of seconds before a keep-alive connection that has
377      * been idle times out and is closed.
378      *
379      * @param timeout Keep-alive timeout in number of seconds
380      */

381     public void setKeepAliveTimeoutInSeconds(int timeout) {
382         selectorThread.setKeepAliveTimeoutInSeconds(timeout);
383     }
384
385
386     /**
387      * Sets the number of keep-alive threads.
388      *
389      * @param threadCount Number of keep-alive threads
390      */

391     public void setKeepAliveThreadCount(int threadCount) {
392         selectorThread.setKeepAliveThreadCount(threadCount);
393     }
394
395
396     /**
397      * The minimun threads created at startup.
398      */

399     public void setMinThreads(int minThreads){
400         selectorThread.setMinThreads(minThreads);
401     }
402     
403
404     /**
405      * Set the request input buffer size
406      */

407     public void setBufferSize(int requestBufferSize){
408         super.setBufferSize(requestBufferSize);
409         selectorThread.setBufferSize(requestBufferSize);
410     }
411     
412     
413     /**
414      * Set the <code>Selector</code> times out value.
415      */

416     public void setSelectorTimeout(int selectorTimeout){
417         selectorThread.selectorTimeout = selectorTimeout;
418     }
419     
420     
421     /**
422      * Return the <code>Selector</code> times out value.
423      */

424     public int getSelectorTimeout(){
425         return selectorThread.selectorTimeout;
426     }
427     
428    
429     /**
430      * Set the <code>reader-thread</code> from domain.xml.
431      */

432     public void setMaxReadWorkerThreads(int maxReadWorkerThreads){
433         selectorThread.maxReadWorkerThreads = maxReadWorkerThreads;
434     }
435     
436     
437     /**
438      * Return the <code>read-thread</code> used by this <code>Selector</code>
439      */

440     public int getMaxReadWorkerThreads(){
441         return selectorThread.maxReadWorkerThreads;
442     }
443
444     
445     public void setDisplayConfiguration(boolean displayConfiguration){
446         selectorThread.displayConfiguration = displayConfiguration;
447     }
448         
449     
450     public boolean getDisplayConfiguration(){
451         return selectorThread.displayConfiguration;
452     }
453
454     
455     /**
456      * Set the <code>recycle-tasks</code> by this <code>Selector</code>
457      */

458     public void setRecycleTasks(boolean recycleTasks){
459         selectorThread.recycleTasks = recycleTasks;
460     }
461     
462     
463     /**
464      * Return the <code>recycle-tasks</code> used by this
465      * <code>Selector</code>
466      */

467     public boolean getRecycleTasks(){
468         return selectorThread.recycleTasks;
469     }
470     
471      
472     public void setUseByteBufferView(boolean useByteBufferView){
473         selectorThread.useByteBufferView = useByteBufferView;
474     }
475     
476             
477     public boolean getUseByteBufferView(){
478         return selectorThread.useByteBufferView ;
479     }
480
481     
482     /**
483      * Set the <code>processor-thread</code> from domain.xml
484      */

485     public void setMaxProcessorWorkerThreads(int maxProcessorWorkerThreads){
486         selectorThread.maxProcessorWorkerThreads = maxProcessorWorkerThreads;
487     }
488     
489     
490     /**
491      * Return the <code>processor-thread</code> used by this <code>Selector</code>
492      */

493     public int getMaxProcessorWorkerThreads(){
494         return selectorThread.maxProcessorWorkerThreads;
495     }
496  
497    
498     /**
499      * Set the <code>reader-queue-length</code> value
500      * on this <code>Selector</code>
501      */

502     public void setMinReadQueueLength(int minReadQueueLength){
503         selectorThread.minReadQueueLength = minReadQueueLength;
504     }
505
506     
507     /**
508      * Return the <code>reader-queue-length</code> value
509      * on this <code>Selector</code>
510      */

511     public int getMinReadQueueLength(){
512         return selectorThread.minReadQueueLength;
513     }
514  
515   
516     /**
517      * Set the <code>processor-queue-length</code> value
518      * on this <code>Selector</code>
519      */

520     public void setMinProcessorQueueLength(int minProcessorQueueLength){
521         selectorThread.minProcessorQueueLength = minProcessorQueueLength;
522     }
523  
524      
525     /**
526      * Return the <code>processor-queue-length</code> value
527      * on this <code>Selector</code>
528      */

529     public int getMinProcessorQueueLength(){
530         return selectorThread.minProcessorQueueLength;
531     }
532     
533     
534     /**
535      * Set the <code>use-nio-non-blocking</code> by this <code>Selector</code>
536      */

537     public void setUseDirectByteBuffer(boolean useDirectByteBuffer){
538         selectorThread.useDirectByteBuffer = useDirectByteBuffer;
539     }
540     
541     
542     /**
543      * Return the <code>use-nio-non-blocking</code> used by this
544      * <code>Selector</code>
545      */

546     public boolean getUseDirectByteBuffer(){
547         return selectorThread.useDirectByteBuffer;
548     }
549     
550     
551     /**
552      * Set the maximum pending connection this <code>Pipeline</code>
553      * can handle.
554      */

555     public void setQueueSizeInBytes(int maxQueueSizeInBytes){
556         selectorThread.maxQueueSizeInBytes = maxQueueSizeInBytes;
557     }
558     
559     
560     /**
561      * Set the <code>SocketServer</code> backlog.
562      */

563     public void setSocketServerBacklog(int ssBackLog){
564         selectorThread.ssBackLog = ssBackLog;
565     }
566     
567     /**
568      * Set the number of <code>SelectorThread</code> Grizzly will uses.
569      */

570     public void setSelectorReadThreadsCount(int selectorReadThreadsCount){
571         selectorThread.selectorReadThreadsCount = selectorReadThreadsCount;
572     }
573
574  
575     /**
576      * Set the default response type used. Specified as a semi-colon
577      * delimited string consisting of content-type, encoding,
578      * language, charset
579      */

580     public void setDefaultResponseType(String JavaDoc defaultResponseType){
581          selectorThread.defaultResponseType = defaultResponseType;
582     }
583
584
585     /**
586      * Return the default response type used
587      */

588     public String JavaDoc getDefaultResponseType(){
589          return selectorThread.defaultResponseType;
590     }
591     
592     
593     /**
594      * Set the forced response type used. The response type to be forced
595      * if the content served cannot be matched by any of the MIME mappings
596      * for extensions. Specified as a semi-colon delimited string consisting of
597      * content-type, encoding, language, charset
598      */

599     public void setForcedResponseType(String JavaDoc forcedResponseType){
600         selectorThread.forcedResponseType = forcedResponseType;
601     }
602     
603         
604     /**
605      * Return the default response type used
606      */

607     public String JavaDoc getForcedResponseType(){
608         return selectorThread.forcedResponseType;
609     }
610     
611     
612     //------------------------------------------------- FileCache config -----/
613

614    
615     /**
616      * The timeout in seconds before remove a <code>FileCacheEntry</code>
617      * from the <code>fileCache</code>
618      */

619     public void setSecondsMaxAge(int sMaxAges){
620         selectorThread.secondsMaxAge = sMaxAges;
621     }
622     
623     
624     /**
625      * Set the maximum entries this cache can contains.
626      */

627     public void setMaxCacheEntries(int mEntries){
628         selectorThread.maxCacheEntries = mEntries;
629     }
630
631     
632     /**
633      * Return the maximum entries this cache can contains.
634      */

635     public int getMaxCacheEntries(){
636         return selectorThread.maxCacheEntries;
637     }
638     
639     
640     /**
641      * Set the maximum size a <code>FileCacheEntry</code> can have.
642      */

643     public void setMinEntrySize(long mSize){
644         selectorThread.minEntrySize = mSize;
645     }
646     
647     
648     /**
649      * Get the maximum size a <code>FileCacheEntry</code> can have.
650      */

651     public long getMinEntrySize(){
652         return selectorThread.minEntrySize;
653     }
654      
655     
656     /**
657      * Set the maximum size a <code>FileCacheEntry</code> can have.
658      */

659     public void setMaxEntrySize(long mEntrySize){
660         selectorThread.maxEntrySize = mEntrySize;
661     }
662     
663     
664     /**
665      * Get the maximum size a <code>FileCacheEntry</code> can have.
666      */

667     public long getMaxEntrySize(){
668         return selectorThread.maxEntrySize;
669     }
670     
671     
672     /**
673      * Set the maximum cache size
674      */

675     public void setMaxLargeCacheSize(long mCacheSize){
676         selectorThread.maxLargeFileCacheSize = mCacheSize;
677     }
678
679     
680     /**
681      * Get the maximum cache size
682      */

683     public long getMaxLargeCacheSize(){
684         return selectorThread.maxLargeFileCacheSize;
685     }
686     
687     
688     /**
689      * Set the maximum cache size
690      */

691     public void setMaxSmallCacheSize(long mCacheSize){
692         selectorThread.maxSmallFileCacheSize = mCacheSize;
693     }
694     
695     
696     /**
697      * Get the maximum cache size
698      */

699     public long getMaxSmallCacheSize(){
700         return selectorThread.maxSmallFileCacheSize;
701     }
702
703     
704     /**
705      * Is the fileCache enabled.
706      */

707     public boolean isFileCacheEnabled(){
708         return selectorThread.isFileCacheEnabled;
709     }
710
711     
712     /**
713      * Is the file caching mechanism enabled.
714      */

715     public void setFileCacheEnabled(boolean isFileCacheEnabled){
716         selectorThread.isFileCacheEnabled = isFileCacheEnabled;
717     }
718    
719     
720     /**
721      * Is the large file cache support enabled.
722      */

723     public void setLargeFileCacheEnabled(boolean isLargeEnabled){
724         selectorThread.isLargeFileCacheEnabled = isLargeEnabled;
725     }
726    
727     
728     /**
729      * Is the large file cache support enabled.
730      */

731     public boolean getLargeFileCacheEnabled(){
732         return selectorThread.isLargeFileCacheEnabled;
733     }
734    
735     
736     /**
737      * Set the documenr root folder
738      */

739     public void setWebAppRootPath(String JavaDoc rootFolder){
740         selectorThread.rootFolder = rootFolder;
741     }
742     
743     
744     /**
745      * Return the folder's root where application are deployed.
746      */

747     public String JavaDoc getWebAppRootPath(){
748         return selectorThread.rootFolder;
749     }
750     
751     
752     /**
753      * Set the logger
754      */

755     public void setLogger(Logger JavaDoc logger){
756         if ( logger != null )
757             selectorThread.logger = logger;
758     }
759     
760     
761     /**
762      * Return the logger used by the Grizzly classes.
763      */

764     public Logger JavaDoc getLogger(){
765         return selectorThread.logger;
766     }
767     
768     
769     /**
770      * Return the instance of SelectorThread used by this class.
771      */

772     public SelectorThread selectorThread(){
773         return selectorThread;
774     }
775     
776     
777     // --------------------------------------------------------- Private method
778

779     /** Sanity check and socketFactory setup.
780      * IMHO it is better to stop the show on a broken connector,
781      * then leave Tomcat running and broken.
782      * @exception TomcatException Unable to resolve classes
783      */

784     private void checkSocketFactory() throws Exception JavaDoc {
785         if (secure) {
786             // The SSL setup code has been moved into
787
// SSLImplementation since SocketFactory doesn't
788
// provide a wide enough interface
789
sslImplementation =
790                 SSLImplementation.getInstance(sslImplementationName);
791             socketFactory = sslImplementation.getServerSocketFactory();
792             selectorThread.setSSLImplementation(sslImplementation);
793         } else if (socketFactoryName != null) {
794             socketFactory = string2SocketFactory(socketFactoryName);
795         }
796         if(socketFactory==null)
797             socketFactory=ServerSocketFactory.getDefault();
798         selectorThread.setServerSocketFactory(socketFactory);
799     }
800 }
801
802
Popular Tags