KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.web.connector.grizzly;
24
25 import com.sun.enterprise.web.connector.grizzly.handlers.NoParsingHandler;
26 import java.io.IOException JavaDoc;
27 import java.net.Socket JavaDoc;
28
29 /**
30  * Process a blocking socket. By default, SSL is using blocking mode.
31  *
32  * @author Jean-Francois Arcand
33  */

34 public class ReadBlockingTask extends ReadTask{
35     
36     /**
37      * The <code>PipelineStatistic</code> objects used when gathering statistics.
38      */

39     protected PipelineStatistic pipelineStat;
40             
41     
42     /**
43      * The <code>Handler</code> used to pre-process the request.
44      */

45     private NoParsingHandler handler;
46     
47             
48     public ReadBlockingTask(){
49         type = READ_TASK;
50         taskContext = new TaskContext();
51         taskEvent = new TaskEvent(taskContext);
52         taskEvent.setStatus(TaskEvent.START);
53     }
54     
55     
56     /**
57      * Force this task to always use the same <code>ProcessorTask</code>
58      * instance.
59      */

60     public void attachProcessor(ProcessorTask processorTask){
61         handler = new NoParsingHandler(selectorThread.getPort());
62         
63         this.processorTask = processorTask;
64         processorTask.addTaskListener(this);
65         addTaskListener(processorTask);
66         
67         processorTask.useAlternateKeepAlive(false);
68         processorTask.setHandler(handler);
69     }
70     
71     
72     /**
73      * Dispatch an Http request to a <code>ProcessorTask</code>
74      */

75     public void doTask() throws IOException JavaDoc {
76         handler.attachChannel(processorTask.getSocket().getChannel());
77
78         // Notify the ProcessorTask that we are ready to process the request.
79
fireTaskEvent(taskEvent);
80     }
81     
82     
83     /**
84      * Gracefully close the blocking socket.
85      */

86     protected void finishConnection(){
87         Socket JavaDoc socket = processorTask.getSocket();
88         
89         // Are we using SSL
90
if (selectorThread.getSSLImplementation() == null){
91             try{
92                 if (!socket.isInputShutdown()){
93                     socket.shutdownInput();
94                 }
95             } catch (IOException JavaDoc ioe){
96                 ;
97             }
98             try{
99                 if (!socket.isOutputShutdown()){
100                     socket.shutdownOutput();
101                 }
102             } catch (IOException JavaDoc ex){
103                 ;
104             }
105         }
106
107         try{
108             socket.close();
109         } catch (IOException JavaDoc ex){
110             ;
111         } finally {
112             if (isMonitoringEnabled()) {
113                 getRequestGroupInfo().decreaseCountOpenConnections();
114             }
115         }
116     }
117
118     
119     /**
120      * Receive notification from other <code>Task</code> and recycle this task.
121      */

122     public void taskEvent(TaskEvent event){
123         if ( event.getStatus() == TaskEvent.COMPLETED){
124             finishConnection();
125
126             // We must recycle only if we are sure ProcessorTask has completed its
127
// processing. If not,
128
if (recycle) {
129                 processorTask.recycle();
130                 selectorThread.returnTask(this);
131             }
132         }
133     }
134
135
136     /**
137      * Return the current <code>Socket</code> used by this instance
138      * @return socket the current <code>Socket</code> used by this instance
139      */

140     protected Socket JavaDoc getSocket(){
141         return processorTask.getSocket();
142     }
143     
144     
145     /**
146      * Set the <code>PipelineStatistic</code> object used to gather statistic;
147      */

148     public void setPipelineStatistic(PipelineStatistic pipelineStatistic){
149         this.pipelineStat = pipelineStatistic;
150     }
151     
152     
153     /**
154      * Return the <code>PipelineStatistic</code> object used
155      * to gather statistic;
156      */

157     public PipelineStatistic getPipelineStatistic(){
158         return pipelineStat;
159     }
160 }
161
Popular Tags